Directories & Files
The course is part of this learning path
This course will get you up to speed with the fundamentals of Linux and prepare you for further study. We'll look at the essentials of Linux including the directory structure, basic commands, the shell, and using the command line.
You'll learn how to navigate your way through the Linux directory structure and the permissions for doing so. We'll also cover files and the multitude of ways in which they can be created, managed, and deleted. This course is part of the Linux Administration Bootcamp learning path, designed to get you up and running with Linux.
Gain a foundational understanding of:
- Linux directories and permissions
- Shell scripting and the command line
- How to create and modify files in Linux
- Anyone with little to no knowledge of Linux who wants to learn more about the operating system
- Professionals who want to learn about Linux to enhance their career prospects
This is a beginner-level course so there are no prerequisites, but an interest in Linux and programming knowledge in general would be beneficial.
In today's lesson, we'll be talking about the components of the long listing format of LS. We'll talk about hidden files and directories. We're going to list files by type and sort them by time. And we're gonna talk about handling spaces and file names and directory names. And also, we're going to talk about what symbolic links are.
Here's some sample output from LS use with a dash L option. If you just want to see what files or directories exist you can run LS without any options. But if you want to see detailed information, use LS space dash L. The first bit of information that is displayed is a series of characters which represent the files permissions. We won't go into detail what every character in that string means but we'll be covering that in a later lesson.
So the first group of characters is the permissions. The second bit of information is the number of links to that file or directory. Next we see who the owner of that file is and what group that file belongs to, the size of that file. And the last time that file or directory was modified. And of course at the very end is the name of the file or directory itself.
Files or directories that begin with a period or a dot are considered hidden files. So if you run LS, it will not display hidden files by default. In order to see hidden files you need to use the dash A option to LS. If we want to see a long listing output that includes hidden files, we could run LS space dash L space dash A. There's also a kind of a shortcut for this. You can just include one hyphen and then follow it by all the options that you need. For instance, LS space, dash LA is the same as LS space dash L space dash A.
Let's run LS in this directory. You notice that there are no files that begin with dot. So if we want to see if there are any hidden files we can run LS with a dash A option. And now we see a few more files that show up dot bash history, dot bash RC. We've already talked about dot which means this directory and dot dot which is the parent directory.
If you run an LS dash L again, by default hidden files are not displayed. You could run dash L for a long listing and dash A for all files. And now we see the dot files or the hidden files. Doesn't matter which order that we specify these command line arguments as, I mean dash A, dash L does the same thing as dash L dash A and then also know that you can combine the arguments with LS space dash L for long listing and A for all files. And again, the order doesn't matter.
So we can do LS space, dash AL, and we get the same output. When you perform an LS by itself, you may or may not know what is a file and what is the directory. You can use LS space dash capital F to reveal file types. LS space dash capital F apins, a character to a file or directory name that tells you what it is. So if it ends in slash or forward slash, then it's a directory. If it ends in an ampersand, that's a link, a star or an asterisk means that it's executable.
You can think of a symbolic link as a pointer. It looks like a file or a directory but it just points to where the actual file or directory is. And you would operate on the symbolic link or SIM link for short the same as if it were the file that it's pointing to. SIM links are often used to create shortcuts to long file names or have long directory paths.
Another common use of SIM links is to indicate the current version of an application. Let's change into this directory. When we perform an LS, we don't know if current is a blink, if it's a directory, if it's a regular file ever an executable file. We can get this information by running LS dash capital F and we see that current is a link. The two, my apps are directories and read me as a regular file. And we can confirm by running LS dash L capital F for a long listing format. And we see current points to my dash App dash two dot four dot seven.
Here we see a program. It's an executable cause it ends in an asterics or a star. And we can execute this by doing dot slash myapp If you want to list files sorted by time use LS space dash T, use the dash R option to reverse the order. And a common command that I like to use is LS space dash latr. This provides a long listing that includes all files sorted by time in reverse.
Let's look at this directory with an LS dash L it sorts the files and directories by name. If we want to reverse this we can use long listing and R for reverse. Now we'll see it's sorted in reverse videos comes first as where with LS dash L, videos was last. Let's do a long listing, including all files sorted by time. And we see bash history is at the top. It's the most recently modified file.
If we do L S dash latr, that will reverse this output. And we'll see the most recently modified files at the bottom. And this can help if you have a long list of files and they can scroll off the screen. So you'll get the most recent files displaying on your screen. To list directory contents recursively, use LS space dash capital R. This will display all the contents of the current directory and any sub-directories below that.
The tree command does a similar thing to LS space dash capital R, but it's more of a visual output. You can give tree a dash D option and that will only list directories. And if you use a dash capital C that will colorize the output. Tree is on a lot of Linux distributions. It may not be installed by default. So you may or may not have access to this command.
Like the tree command, LS has a dash D option that only lists directory names and not their contents. By default, when you run LS space directory name, LS will provide the contents of that directory. You can also tell LS to colorize output by using LS space dash dash color. This is just another option to distinguish file types. You can do LS space, dash capital F, or LS space dash dash color, or even combine the two.
If we want to display recursive output, we can use LS space dash capital R, and a lot of the information has scrolled past the screen, but that's okay. The tree command is something similar and it displays things in a visual output and you see files and directories in this example. If you only want to look at the directory structure you can use tree with the dash D option. And if you want to colorize the output you can use tree dash C or capital C and then you could even combine the two.
If we run LS against a directory, such as music you can see that it displays the contents of music which in this case is jazz. If we only want to look at the directory itself, we can use LS dash D. That may not make much sense in this case. So maybe let's use LS dash L music and you see long listing inside of music, which is jazz LS dash LD long listing only show the directory of music and so now we can see, for instance the last modification time of the music directory.
You can use dash dash color for LS to get color output. And sometimes this is done by default on some Linux distributions and the color coding here, like in this configuration, blue are directories and we can confirm this by using dash F. If you want to make your life a little bit easier when working at the command line, do not use spaces in file and directory names. Instead, get in the habit of using maybe a hyphen or a dash where you would use a space or maybe an underscore or even something called camel case where you can capitalize the first letter of each word.
Sooner or later, you're going to encounter a file name that has a space in it. It may be a file that you didn't create and someone else did. You have no control over that. How you can deal with this is wrapping the file name in quotes, be it single quotes or double quotes. Another thing you can do is escape the space character. To escape something you proceed it with a backslash.
In this output you can see a file named my space notes dot text. If we were to try to run an LS against that you'll see the LS treats it as two different files not one file name with a space. So to get around this, we can use quotes. That works using double quotes. Also single quotes will work. What we could have done instead was maybe name the file my dash notes dot text, or maybe my underscore notes dot texts or even my notes like this, or even my notes dot text.
In today's lesson, we've talked about some of the more common and useful LS options. We've learned about SIM links. I talked about hidden files and directories and how they start with a dot. And we talked about a couple of strategies for dealing with spaces in file names. Here are the LS options we covered today. Dash A to list all files, dash dash color to list files with colorized output, dash D to show directory names and not directory contents, dash L for the long listing format, dash R for reverse order, dash capital R to list files recursively and dash T to sort files by time.
Jason is the founder of the Linux Training Academy as well as the author of "Linux for Beginners" and "Command Line Kung Fu." He has over 20 years of professional Linux experience, having worked for industry leaders such as Hewlett-Packard, Xerox, UPS, FireEye, and Amazon.com. Nothing gives him more satisfaction than knowing he has helped thousands of IT professionals level up their careers through his many books and courses.