Practice Exams:

LPI 010-160 – Directories and Files

  1. Introduction to Linux Filesystem

It’s time for us to start navigating our way around the Linux file system. In this lesson we’re going to cover how to learn what files are on the hard disk, how to change directories, and how to refer to files that aren’t in your current directory. Now, to manipulate files, it’s helpful to know what they are. If you use the LS command, which is short for list, this is going to provide you some information. If there’s no options used, it’s just going to show you files in the current directory. However, additional options in files or directory specifications can be displayed with more information. For example, if you use the LSA option, it’s going to display all of the files in the directory whether they’re hidden or dot files or not.

Now on the other hand, if you’re using something like LS l, this is going to display an output which includes permission strings, ownership, file sizes and file creation dates in addition to the file names themselves. To learn all of the options for LS, just type man space LS at your prompt and hit Enter to read through its man page.

The next command we have is known as CD, which is short for change directory. This allow you to change from one directory into another. Now, although your current directory doesn’t matter much when you’re issuing commands, it does matter when you’re trying to do things to certain files. For example, if you want to copy files you need to know what directory you’re in and what files are sitting in that directory. When you’re working with the CD command, you have to remember that Linux uses the forward slash as your directory separator, not the backslash like you’re used to in Windows. These things cannot be interchanged either.

You have to use that forward slash in Linux because that backslash, if you remember, is actually an escape character or the quote character that we use inside of our commands. So if we put something like slash space or slash dollar sign, it’s going to treat that space or the dollar sign as plain text or part of the file name. Now if you type CD followed by a space, then the directory you want, such as home usersdocuments, it’ll move you into that directory path. This will switch you to make that your current directory. Now it’s important for you to know that depending on your distribution, some shells are not going to show you what directory you’re currently sitting in as part of your command prompt.

If this is the case for you, you can always use the command PWD or present working directory and this will be able to tell you what directory you’re in once you hit Enter. Now in a previous lesson we discussed how Linux uses a unified directory tree. This means that all the files can be located relative to a single root directory which is referred to as the single forward slash. Now, files and directories can be referenced in three different ways. We have absolute references, home directory references and relative references.

When we talk about the absolute references, these are file references that are relative to the root directory. So for example, if I use forward slash homeuser one file TXT, this refers to the file TXT file inside of the user one’s home directory in relation to the root drive. Now, such references are always going to start with that first forward slash. Now instead, if I wanted to use a home directory reference, I’m going to use the tilde character, which is that little squiggly line. This is going to tell you that you’re talking about the user’s home directory. So if a file name begins with that character and I have tilde file TXT, for instance, this is going to say this is the path of the user’s home directory with file TXT laying inside of it. Now, if I have a relative reference, these are file references that are relative to the current directory you’re in. Every Linux directory includes two special hidden subdirectories. One is known as dot dot and one is known as dot. Dot refers to this directory dot. Dot refers to your parent directory, the one that is one level higher than you. Let’s pretend I’m sitting here inside dur one of user one’s home folder. So I’m sitting in slash home user one, dur one.

Now there’s another directory called slashhome user one, dur two, and there’s a file in there I want to reference. Using a relative reference from dur one’s directory, I can use dur two afile TXT, which allows me to reference the parent directory which is user one and then back down into dirtwo and then naming the file. That’s the idea here when we’re dealing with a relative reference because it takes into account where I am in the file structure and then allows me to go up and down the file structure by using the to go up a directory up to the parent or the forward slash to go down into a new directory.

  1. Linux Filesystem Key Features

So now that we covered some of the basics of the Linux file system, let’s dig a little bit deeper and look at some other differences. One of the big crucial differences between Windows and Linux is the concept of mounting and unmounting partitions drives and directories. Windows detects partitions and drives during the boot process and assigns a drive layer to them. Now, in Linux, partitions and devices are usually not visible in the directory tree unless they’ve been mounted and that means they have to be integrated into the file system at a specific location in the directory structure. A normal user cannot access data on a partition or a device unless it’s been mounted. Most of the time users do not have to mount partitions or devices manually though.

During the installation of Linux, users can define partitions that will be automatically mounted whenever your system is started. Now, when you’re dealing with removable devices like external hard drives or USB drives, these are usually going to be detected and mounted automatically by your system. Desktop environments such as KDE and Gnome are going to also notify you about the appearance of a new device. Linux is going to distinguish between uppercase and lowercase letters in the file system. For example, if you name a file test TXT, test TXT or test TXT, all three of those are three different files. On Windows, those are all treated the exact same as one file. But on Linux case sensitivity does matter. This is also true for directories.

You cannot access a directory named letters by using the lowercase version letters because those are two different directories. Now, when you deal with Windows, files might have a file extension but in Linux they don’t have to have one. So in Windows you would see things like exe telling you it’s an executable or PNG to tell you that it’s a picture that doesn’t hold true. In Linux you can have a file name photo with no extension and it will still open up as a PNG file if it was saved in that format. That three letter extension doesn’t matter when you’re dealing with Linux. Now, similar to Windows though, Linux does distinguish between normal files and hidden files. Now, hidden files generally are going to be things like configuration files. In Linux, we hide a file by putting a dot in front of its name.

So if I wanted to have this file hidden file be hidden, I can do that by calling it dot hidden file. In order to access a hidden file you have to switch the viewing your file manager or use certain commands in the shell to be able to see those. By default they will be hidden from your view though, because Linux is a multi user system. Every file in a Linux file system is going to belong to some user and some group. Only the owner of that file or directory is going to be able to grant other users access to that file. Now, there is an exception to this and that’s the root user. Because the root user has Godlike permissions on this system, they can do whatever they want and they can grant permission to other users for anybody’s files linux basically distinguishes between three different types of access permissions. There are write permissions, read permissions and execute permissions. In order for a user to be able to access a file or folder, they have to have at least read permissions to it.

Now, there are lots of different ways to change the access permissions for files and folders. You can do this through the shell or you can use the graphical file system to do it there too. Although each Linux distribution has its own way of doing certain things, all Linux developers recognize the need for some standardization in the layout of their directories. To address this need, the File System Hierarchy Standard, or FHS, was created and it’s used by most distributions and Linux. The FHS makes an important distinction between shareable files and unsharable files.

When you have a shareable file, this will be reasonably shared between different computers such as user data files, program binary files and things like that. unsharable files are going to contain things that are system specific things like configuration files. Now, the FHS also makes a second important distinction between static files and variable files. Most program executables are good examples of static files. Now, on the other hand, we have variable files and variable files are things that can be changed.

These are going to be things that are changed by users, automated scripts or servers. For example, any of the files you store in your user’s home directory like documents and photos. Those are things that are going to be variable files because they can be changed whenever they’re needed to by that user.

  1. Navigating Files and Directories

It’s time for us to start navigating our way around the Linux file system. In this lesson we’re going to cover how to learn what files are on the hard disk, how to change directories, and how to refer to files that aren’t in your current directory. Now, to manipulate files, it’s helpful to know what they are. If you use the LS command, which is short for list, this is going to provide you some information. If there’s no options used, it’s just going to show you files in the current directory. However, additional options in files or directory specifications can be displayed with more information. For example, if you use the LSA option, it’s going to display all of the files in the directory whether they’re hidden or dot files or not. Now on the other hand, if you’re using something like LS l, this is going to display an output which includes permission strings, ownership, file sizes and file creation dates in addition to the file names themselves.

To learn all of the options for LS, just type manspace LS at your prompt and hit enter to read through its man page. The next command we have is known as CD, which is short for change directory. This allow you to change from one directory into another. Now, although your current directory doesn’t matter much when you’re issuing commands, it does matter when you’re trying to do things to certain files. For example, if you want to copy files, you need to know what directory you’re in and what files are sitting in that directory. When you’re working with the CD command, you have to remember that Linux uses the forward slash as your directory separator, not the backslash like you’re used to in Windows. These things cannot be interchanged either.

You have to use that forward slash in Linux because that backslash, if you remember, is actually an escape character or the quote character that we use inside of our commands. So if we put something like slash space or slash dollar sign, it’s going to treat that space or the dollar sign as plain text or part of the file name. Now if you type CD followed by a space, then the directory you want such ashomeusersdocuments it’ll move you into that directory path. This will switch you to make that your current directory. Now it’s important for you to know that depending on your distribution, some shells are not going to show you what directory you’re currently sitting in as part of your command prompt. If this is the case for you, you can always use the command PWD or present working directory and this will be able to tell you what directory you’re in once you hit enter. Now in a previous lesson we discussed how Linux uses a unified directory tree.

This means that all the files can be located relative to a single root directory which is referred to as the single forward slash. Now, files and directories can be referenced in three different ways. We have absolute references, home directory references and relative references. When we talk about the absolute references, these are file references that are relative to the root directory. So for example, if I use forward slash homeuser one file TXT, this refers to the file TXT file inside of the user one’s home directory in relation to the root drive. Now, such references are always going to start with that first forward slash. Now instead if I wanted to use a home directory reference, I’m going to use the tilde character, which is that little squiggly line.

This is going to tell you that you’re talking about the user’s home directory. So if a file name begins with that character and I have tilde file TXT, for instance, this is going to say this is the path of the user’s home directory with file TXT laying inside of it. Now, if I have a relative reference, these are file references that are relative to the current directory you’re in. Every Linux directory includes two special hidden subdirectories. One is known as dot dot and one is known as dot. Dot refers to this directory dot. Dot refers to your parent directory, the one that is one level higher than you. Let’s pretend I’m sitting here inside dur one of user one’s home folder.

 So I’m sitting in home user one, dur one. Now there’s another directory called home user one, dir two, and there’s a file in there I want to reference. Using a relative reference from dur one’s directory I can use dur two agile TXT, which allows me to reference the parent directory which is user one and then back down into dirtwo and then naming the file. That’s the idea here when we’re dealing with a relative reference because it takes into account where I am in the file structure and then allows me to go up and down the file structure by using the go up a directory up to the parent or the forward slash to go down into a new directory.

  1. File Creation and Management

Let’s discuss some of the basic commands when you’re working with files. Now, these files can be created and they can then be copied from one location or folder to another. They can be moved and they can even be renamed. Files are usually created by the programs that are going to manipulate them. For example, a graphics program like GIMP is going to be used to create a new graphics file. This, of course, is going to vary from one program to another. But most graphical user interface programs are typically going to have some menu option known as save or save as for you to be able to save a file. Text mode programs also provide similar functionality, but the details of how they do it specifically are going to vary greatly from one program to another.

Now, one program used to create files on the command line is known as touch. Now, if you type touch followed by the name of a file, for example touchnewfile. TXT, this will create an empty file known as newfile. TXT. Normally it’s not necessary for you to create a file of a particular file type, since the program is going to actually match the file type that it needs when it creates the data inside that file. But sometimes it’s helpful just to create a bunch of empty files so you can play with them when you’re trying to play with a different command or script.

As you’re going to see in our demo, I’m going to create a bunch of files using the touch command just so I can have them to move them around and copy them as we go through our lab together. Now, another command that’s useful for working with files is known as the CP command. This command, which is short for Copy, is going to copy a file. It can be used in one of three ways. You can pass it an option with a source file and a destination file name. You can give it a destination directory name, or you can give it both. The Copy command is also going to provide you with many other options that allow you to modify its behavior using certain switches. You can always refer to the Copy manual page to get a list of all of the options and corresponding functions by typing man space CP and hitting Enter at your terminal prompt.

Now, if you want to move or rename your files, you can do that using the exact same command. It’s known as MV, which stands for move. Now, its usage is very similar to the Copy or CP command. If you give it a file name and you specify the destination as well, the file will be renamed while it’s being moved to that new location. If you give it a file name that’s specified and a destination directory in the same directory, the file will be renamed, but it will not be moved because it’s already there in that directory. The MV commands effects are very much like the copy command, except the new file is going to be replacing rather than supplementing that original file.

So if I move something from the documents directory to the downloads directory, you’re only going to have one copy at the end of that move, and it’s going to be the one in downloads. If I was doing copy, you’d have a copy both in documents and in downloads. Now, when you have the source and the target on the same file system, the move command is going to rewrite the directory entries without actually moving the file’s data on the hard drive. It just puts a different pointer there. Also, when the file is moved from one file system to another, the move command is going to copy the file and delete the original file. The move command is going to take many of the same options as copy does, and if you want to find more information about it, go to manspace. MV and hit enter at your terminal prompt.