Lessons‎ > ‎Linux Lessons‎ > ‎

Linux Lesson 04 - Linux File System


In this lesson you'll learn about files and directories.  You'll learn how to move around in the file system using the shell, you'll learn how to create, move and delete directories and files, and how to search for data.  And you'll learn some special characters you can use when using the shell.

Root Directories

The Linux file system provides a method for you to organize your data into files and directories.  In Linux, everything is a file, including directories, and your computer's hardware.  The hierarchically structure starts at the root of the file system, or /.  Typing in ls / will show you the contents of the root of your system.

The dark blue files in the image above are a special type of file called a directory.  They are capable of storing files within them creating the hierarchical structure.  We are used to other operating systems where directories and files are two separate entities, but in linux they are the same.  Directories are files that are marked as type "Directory".  Files that are marked as type "Directory" will be referred to as directories for the remainder of this lesson.  Each directory has it's own purpose outlined below.
  • /bin - Contains the built in user applications like ls and cp.  Short for binaries.
  • /boot - Contains the files need to start up the system or booting the system.
  • /dev - Contains all the device files for your computer.  Each hardware component is represent by a file.
  • /etc - Contains all the configuration files for the OS and your applications.
  • /home - Contains the user's home directories.  This is where users can store all their data.
  • /lib or /lib64 - Contains all the libraries need to run.  A library is code run by another program, and not the user.
  • /lost+found - Contains data that was recovered after a kernel panic, or system crash.
  • /media or /mount - This is the where access to removable media will start.  
  • /opt - Contains optional, or add on programs. Not uses as much, replaced by /usr/local.
  • /proc - Contains all the processes that are active in the kernel.  
  • /root - Home directory for the super user.
  • /run - A location that mounts early in the boot process and is used by tools that need early access.
  • /sbin - Contains the built in system application like ifconfig and shutdown.  Short for system binaries.
  • /srv - Contains data for specific services like www or ftp.
  • /sys - A mounted RAM based filesystem that shows hardware and process in a structured way.
  • /tmp - A temporary storage location.
  • /usr - Contains all the user applications. 
  • /var - Contains variable data, data that changes over time, like print spools, or log files.

Linux Paths

Each file in Linux can be referenced by it's path.  The path is a listing of each directory you access to get to the file.  Each directory is separated by the forward slash /.  If a file named budget.txt is stored in the desktop of Mike Smith's home directory the path would be /home/msmith/Desktop/budget.txt.  Path locations are case sensitive, /home/msmith/Desktop is a different path then /home/msmith/desktop.  You can use the ls command to list the contents of a directory.  

When a path contains a space it needs to be written differently.  We can see this when we use a command like the touch command.  The touch command takes a file's path as an argument and creates an empty file with that name, or updates the date modified if it already exists.  If we use the command touch My Budget.txt to create a file named "My Budget.txt" it won't work.  The command will see two different arguments, "My" and "Budget.txt" and it will create two separate files.

There are a couple different ways to handle the space.  The first method would be to escape the space.  Escaping is using a special character to tell the command interpreter that the next character is part of the argument.  Putting a backslash \ in front of the space tells the command interpreter that the next character is part of the argument and not to treat it as part of the command.  When we use the command touch My\ Budget.txt it will create the file we want.

Alternatively you can use quotes to surround the file name and the command interpreter will see it as one argument.  Using the command touch "My Budget.txt" will create the file we want.

Each user has their own home directory to store data.  The home directories are stored in /home and are named using your user's usernames.  Since your home directory is where you store most of your files there is a special character used to represent it.  The tilde ~ is used to represent your home directory. You can use the tilde when writing paths as arguments.  If you use the command touch ~/Documents/Budget.txt it will create a file in /home/mhull/Documents.  This can be a time saver when writing paths.

When you're using the shell you have a working directory.  This is the directory you're in when running commands.  When we use the touch command followed by a filename without a path it will place the file in the working directory, or the directory you're currently in. You can tell where you are in the file system by using the command pwd.  The command pwd will print your working directory to the screen. You can also get an idea where you are by looking at the shell prompt, at the end it will show you the current path.  In the image below we can see the ~ at the end of the prompt, that tells us we're in our home directory.

The current directory can be referenced by using a period or dot.  The cp command is used to copy data from one location to another.  If you want to copy a file from the desktop directory to the documents directory and your working directory is the documents you can use the cp command cp /home/mhull/Desktop/Budget.txt /home/mhull/Documents.  You can shorten this command using the tilde and period.  The command cp ~/Desktop/Budget.txt . will do the same thing.  (Note: The period is part of the command.)

The directory above your current location is called the parent directory.  The parent directory can be referred to using two periods in a row or dot dot.  We can change our working directory with the cd command or change directory.  Typing in cd followed by a directory path will change to that directory. If you are in /home/mhull/Documents and you want to change to your home directory you can type in cd .. 

When you're typing path names you can do an absolute reference, or a relative reference.  An absolute reference is path that starts with a forward slash /.  It includes the entire path, from the root, to the file or directory.  A relative reference is a path that doesn't start with a forward slash / and starts in your current directory.  It's the path in relationship to where you are in the file system.  When you use a command that uses a path as an argument you can type your path as an absolute or relative path.

Using cd and ls

The command cd (change directory) is used to change your working directory.  If you type cd followed by the name of a sub directory your working directory will change to that sub directory. The directory you were in will become the parent directory.  You can move back up a directory by typing in cd ..  This ability to move through the file system is very powerful and a common thing to do.  As you move into directories you can use the tab key to complete the name of the directory.  If the tab key completes the directory name you know it's correct.  At any point you can type cd without an argument and it will return you to your home folder.

As you move around the file system you'll want to view a list of files in the directories.  The command ls (list) will list the contents of a directory.  If you type in ls by itself without an argument it will show the contents of the working directory.  If you type in a path as an argument it will list the files in that path.

When using the ls command there are some options you can use to change the way the files are displayed.  Typing in ls -F will add a forward slash / to the end of any directory.  Usually the directories are color coded so this isn't needed, but if you are in an environment that is missing the colors, or if you are redirecting the list to a file this command will help tell the difference between a file and directory.

In Linux you can hide a file by starting the file name with a period.  If you want to display hidden files you can use the -a option on the ls command.

You can choose to list the files in a long format, when you do you'll see more information about each file.  The -l option will display the files in a long format.  There is a lot of information displayed with this option.  We will learn more about what's there later.

The options can be combined together.  If you type in ls -al it will display all files, including hidden, in a long format.

We saw typing in ls -l will show information about the files in a directory.  If you want to find out information about your working directory you can use the d options.  Typing in ls -ld will show you information about the working directory.

And if you want some fun you can list all the directory's contents recursively.  This will list all files in all directories below the directory you used as an argument.  For fun try typing in ls -R /  you can use Ctrl + C to cancel.

Shell Metacharacters

Some characters are reserved as special characters in Linux.  The first metacharacter is the asterisk *.  The asterisk acts like a wildcard.  If you type in the command ls t* it will list all the files in the working directory that start with the letter t regardless of their length.

While the asterisk is a wildcard that could represent any number of characters, the question mark ? metacharacter is a wildcard that represents one character.  If you type in ls test?  it will display all files that start with test and have one additional character.

The next two metacharacters are similar.  Each represent a single character, but instead of representing any possible character they define what characters are represented.  The first is called a set.  You define a set of characters that the character could represent.  Each possible character is listed between square brackets.  If you wanted to list the test files 14, 18, 19 you would use the following command. ls test1[489] 

The last metacharacter is called a range and is very similar to a set.  You define the range of values the character could represent. If you want to display test 1, 2, 3, 4, and 5 you can type the command ls test[1-5]

Working With Files and Directories

You can make your own directories in Linux using the mkdir command.  The mkdir command takes a directory path as an argument. If you list multiple directories as arguments it will create each.  You can only make a directory if you have permission to do so in the parent directory.

The touch command takes a file as an argument and if the file doesn't exist it will create it, if it does it will update the timestamp.  If you add multiple files as arguments it will create or modify each one.

You can copy files from one location to another.  When you do the data will exist on both place.  The cp (copy) command is used to copy data from one location to another.  The cp command takes two arguments, the first is the source and the second is the destination.  

When copying a directory you need to use the -r option to recursively copy the data.  

All the rules we learned about paths apply with the cp command.  We can see in the image four different commands that all do the same thing.

You can move data using the mv (move) command.  The mv command follows all the same rules as the cp command except it is natively recursive.  You don't have to use - r when moving directories.  Linux doesn't have a rename command, so if you find yourself needing to rename a file or directory you move it to the new name.

If you want to delete something you can use the rm (remove) command.  If you add the -i it will ask you if you want to delete each file, and -r will remove directories.  Once you delete something with the rm command it's gone.

Finding Files

You can search for files using the locate command.  The locate command uses a built in database, or index of files.  The index is updated automatically, but if you want to force it to update you can type the command updatedb.  In the image below the results of the locate command are sent to the grep command to only display the results in the home directory. 

The find command will search the files system for the file or directory you specify.  

1 | 2 | 3 | 4 | 5