The Unix filesystem

Author

Marie-Hélène Burle
Adapted from a Software Carpentry workshop

Bash allows to give instructions to a Unix operating system. The first thing you’ll need to know is how storage is organized on such as system.

Structure

The Unix filesystem is a rooted tree of directories. The root is denoted by /.

Several directories exist under the root. Here are a few:

  • /bin     This is where binaries are stored.
  • /boot    There, you can find the files necessary for booting the system.
  • /home    This directory contains all the users’ home directories.

These directories in turn can contain other directories. /home for instance contains the directories:

  • /home/user01
  • /home/user02
  • /home/user03

The home directory of each user contains many files and directories.

Creating files and directories

Files can be created with a text editor:

nano newfile.txt

This opens the text editor “nano” with a blank file. The file actually gets created when you save it from within the text editor.

or with the command touch:

touch newfile.txt

This creates an empty file.

touch can create multiple files at once:

touch file1 file2 file3

New directories can be created with mkdir. This command can also accept multiple arguments to create multiple directories at once:

mkdir dir1 dir2

Deleting

Files can be deleted with the command rm followed by their paths:

rm file1 file2

Directories can be deleted with rm -r (“recursive”) followed by their paths or—if they are empty—with rmdir:

rm -r dir1
rmdir dir2   # only works if dir2 is empty

Be careful that these commands are irreversible. By default, there is no trash in Linux systems.

Copying, moving, and renaming

Copying is done with the cp command:

cp thesis/src/script1 thesis/ms

Moving and renaming are both done with the mv command:

# rename script1 to script
mv thesis/src/script1 thesis/src/script

# move graph1 to the ms directory
mv thesis/results/graph1 thesis/ms
# this also works:
# mv thesis/results/graph1 thesis/ms/graph1

Your turn:

Why is there only one command to move and rename?