Skip to content

Cheatsheet of most useful commands

The filesystem is organized in a tree-like structure of directories, which can contain other subdirectorie or files. (almost) Each directory is found inside another directory, which is called its parent directory. Each directory contains two special directories, namely . which points to the directory itself, and .. which points to the parent directory, note that the second is crucial in order to navigate the filesystem upstream. The root directory of the tree structure is denoted by /, and it is the only directory that has no other parent directory - in facts, it is its own parent!.

Each directory and file is uniquely identified by its absolute path, which is the path one has to follow to reach it starting from the root directory, and is denoted by /absolute/path/to/object. You can also identify uniquely any object with by its relative path from a given directory, which is a path in the form of ./relative/path/to/object (oftentimes, ./ can be actually omitted). Given a relative path, its corresponding absolute path can be retrieved using the command realpath ./path/to/object. When you write code, consider using relative paths, hardcoded absolute paths will make your code non portable on different machines.

In a shell session (that thing that starts when you open a terminal) you are always in a directory. By default, you start in your home directory, which has path /home/username. As a user, you have freedom to do whatever you want with directories and files downstream from your home, this is where you will build your projects. There are a few shortcuts for the absolute path to your home, such as ~ and the environment variable $HOME, so, if you have to specify a path somewhere downstream your home, you can use ~/relative/path/from/your/home. The directory you find yourself in at any given moment is called working directory. You can find the absolute value of your working directory using command pwd (print working directory). In a shell session, you can use relative paths from your working directory to specify an object in the filesystem. You can change your working directory using the command cd [dir] (change directory). This command takes one argument (I omitted the options, but you can check them through cd --help) that specifies the target directory you want to change to, it can be either an absolute or relative path, and has as default value the path to your home.

You can visualize the tree-like structure of a directory and its subdirectories using the tree command. For example, consider the output of tree /home/username/tree, which shows the structure of the /home/username/tree directory:

tree
├── subdir_0
   ├── file_0
   ├── subdir_0
   ├── subdir_1
      ├── file_0
      └── file_1
   ├── subdir_2
   └── subdir_3
├── subdir_1
   ├── subdir_0
   ├── subdir_1
   ├── subdir_2
   └── subdir_3
├── subdir_2
   ├── subdir_0
   ├── subdir_1
   ├── subdir_2
   └── subdir_3
└── subdir_3
    ├── subdir_0
    ├── subdir_1
    ├── subdir_2
    └── subdir_3

20 directories, 3 files
/tree has 4 subdirectories, each of which has 4 subdirectories of its own. Note that, as long as objects are locted in different directories, they can have the same name, that is because the absolute path is actually different. Say you want to specify the path to file_1, and your current directory is /home/username/tree/subdir_2/subdir_3, here are a few equivalent ways to specify such path: - absolute path: /home/username/tree/subdir_0/subdir_1/file_1 - using home aliases ~/tree/subdir_0/subdir_1/file_1 $HOME/tree/subdir_0/sundir_1/file_1 - ../../subdir_0/subdir_1/file_1 Note that they all basically have the same downstream path subdir_0/sundir_1/file_1, but different ways of specifying the /home/username/tree/ path.

cd (Change Directory)

Change the working directory. Names can be both absolute (full path from root directory) or relative to the current one. Most common uses:

cd ..
to go to parent directory.

cd
to go to home directory.

cd /

pwd (Print Working Directory)

Tells the user which one is the working directory.

ls (List)

List all the objects (files and directories) contained in a given directory. If no arguments are given, content of working directory is displayed. Objects with name starting by "." (e.g. ".ssh") are hidden by default.
Common uses:

ls -a
Use -a (all) flag to show hidden objects as well.

ls [dir]
Shows content of directory with name [dir].

File manipulation

touch [file]
commonly used to create a new, empty file with name [file].

mkdir [dir]
used to create an empty directory with name [dir].

cp [source] [destination] (copy)
copies file with name [source] in a file with name [destination]. If [destination] doesn't exist, it is created.
Variations:

cp -r [source] [destination]
Copies the entire content of the directory [source] in the directory [destination].

mv [source] [destination] (move)
Moves the content of the [source] file in the [destination] one. If [destination] doesn't exist, it gets created. The difference with cp is that the [source] file gets removed. Standard way to rename a file (just mv its content to a file with a different name in the same directory).

rm (remove) [file]
Removes definitely a file from the file system. BE CAREFUL! There is no going back from rm. No trash bin, no Undo: once a file is removed, it's lost forever.
Variations:

rm -r [dir]
Removes all contents of directory [dir].

rm -f [file]
Some files might be protected and require permission to be deleted: "-f" (force) forces deletion, overriding safety measures; be careful, because if a file is protected, it is usually important for the correct functioning of the system. DON'T USE THIS UNLESS YOU'RE SURE OF WHAT YOU'RE DOING.