Let's dive deeper into the ls command which is probably the most used command in Linux. As I'm 1
sure you already know, ls used to list the contents of a directory together with lots of other information 2
about files and sub-directories. 3
Even though ls is a common command, it's a complex one, with plenty of options. 4
Read its help or its man page if you are not sure what a specific option does. 5
The syntax of the ls command is as follows: ls [OPTION] and [FILE] As option and file is option and file are between brackets 6
they are optional. 7
Let's see some practical examples. 8
If I simply ran a ls with no options or arguments, it will print the contents of the current working 9
directory at the terminal. 10
The files are listed in alphabetical order. To list the files in a specific directory, pass 11
the path to that directory as argument to the ls 12
command. 13
And If you want to have your listing produced in a single column, use the -1 option; 1 means 14
one file per line. 15
And you can specify as many arguments you want. 16
In this example, ls will display the contents of /etc/, /var and the current working directory 17
which is by convention, a single dot. 18
The real power of the ls command comes from its options. 19
Let's take a look at the most frequently used options. 20
-l will display the output in a long listing format which means lots of information about 21
files and directories. 22
This is the current working directory and this is the contents of /etc in long listing format. 23
By the way, if you want to see information about a directory like /etc and not about its contents, 24
then use the -d option. 25
It's displaying information about the etc directory and not about its contents; let's return to the output 26
of ls-l and let me explain to you what you see. 27
The first character indicates the file type: A hyphen (-) as the 1st character indicates a normal file, 28
‘d’ indicates a directory and “l” indicates a symbolic link which is sort of a shortcut. 29
The nine characters that follow indicate the file permissions for the owner, the group owner and the 30
others. 31
Then comes the number of hardlinks, the owner, 32
and the group owner of the file; the next field is the size and finally the modification time and the 33
file name. 34
Will dive deeper into each concept, like links, file types and so on in the next lectures. For the 35
moment, it's enough if you know what each field means. 36
When you run ls, it’s common to group together more options in order to get the desired output. For example 37
adding the -h option, ls will show the file size in human readable format. 38
Take a look at this column. 39
It's in human readable format; by default, the ls command will not show hidden files. In Linux 40
a hidden file or directory is any file or director whose name begins with a dot. To display all files, including 41
the hidden ones, use the -a option. 42
These are hidden files and this is a hidden directory. 43
They all begin with a dot. Another useful option is capital -S which sorts the files by size and the largest 44
file will come first. 45
Files are sorted by size. 46
I adding the -h option to see the size in human readable format. 47
This is the largest file in /etc. Note that ls does not display the real size of a directory as 48
you'd expect. It displays only the size of the inode structure. 49
To see the size of a directory use the du command instead. 50
du comes from disk usage. 51
in this example ls command says that the /etc directory has only 12 KB, but this is not 52
the size of all its contents. To see its size run: du -sh and the path to the directory. 53
This is a real size. 54
By the way, in /etc, there are some files that cannot be read by an unprivileged user and that's 55
why I've got these permission denied errors. Run the command as root to get rid of them and see 56
the total space occupied. 57
So sudo and the command and the command will run as root. There is no error and this is its size. 58
To sort by extension, use the -X option, written in uppercase. 59
Files are sorted by extension. 60
And if you want to omit certain files from the listing, use the --hide option. 61
If you don't want to see the files which end in that conf in the listing, you can run this command: 62
ls --hide=*.conf /etc/ the star means any string of characters and all 63
of those files that end in .conf will be omitted from the listing. 64
There is no file that ends in .conf. With -R option you can list directories recursively. 65
Recursively means that ls works its way through the entire directory tree below the starting directory, 66
and lists the files in each subdirectory. 67
I'm listing the contents of /etc recursively. 68
At the end, let me tell you something that will definitely surprise you. Up until now in the course, 69
we've never actually run the original ls command. 70
If you run type ls, you'll see that there's an alias called ls to ls --color=auto 71
. 72
The --color=auto is the option that provides the different colors for the different 73
file types in the listing. That’s why when you run ls -l you see directories in blue, 74
regular files in black, symlinks in cyan, or device files in yellow on a black background. 75
If you want to run the original ls command without adding --color=auto, run 76
\ls and you’ll see everything displayed in black. The \ char cancels the 77
alias.