1) [ Linux commands series ] ls
An overview of ls command with examples
Hi there! Hope you're doing good. Today I would like to start my overview of different Linux commands. Linux is one of the most popular OS nowadays. It was made for developers by developers.
To make the journey comfortable I will write separate articles for each command. Please keep in mind that the knowledge from the series can be applied not only in Linux. Historically, many operational systems have a similar interface for interacting. Also, I would like to point out that practice is a crucial element for learning new things. So, follow the article and repeat the commands in your terminal. It will help you a lot. Let's begin =)
The first command of series will be ls
.
1. What is ls
command about?
The command ls
stands for list
. Basically we use this command to list the content of directories on file system.
2. Usage of ls
ls [OPTION(s)] [DIRECTORY NAME]
- Square brackets
[]
mean that the section is optional and can be omitted. [OPTION(s)]
is the section where various options can be put.[DIRECTORY NAME]
is the command's section where you can specify a particular directory for viewing its content.
3. Practice
3.1 Viewing content of directories without options
I will open my terminal and simply execute:
ls
I'm in home directory and there's my output:
- I haven't specified any option for
ls
. - I haven't specified a particular directory name for
ls
. It means thatls
will print out the content of current directory. This is a default behavior.
Let's see what does directory Pictures
contains:
For me there are 5 screenshots and a folder. I will check the content of summer2015
by typing:
ls Pictures/summer2015
the output:
3.2 Viewing content of directories with options
As I mentioned above we can add different options to ls
. To get the list of available options execute:
ls --help
The output will be the following:
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.
Mandatory arguments to long options are mandatory for short options too.
-a, --all do not ignore entries starting with .
-A, --almost-all do not list implied . and ..
--author with -l, print the author of each file
-b, --escape print C-style escapes for nongraphic characters
--block-size=SIZE with -l, scale sizes by SIZE when printing them;
e.g., '--block-size=M'; see SIZE format below
-B, --ignore-backups do not list implied entries ending with ~
-c with -lt: sort by, and show, ctime (time of last
modification of file status information);
with -l: show ctime and sort by name;
otherwise: sort by ctime, newest first
-C list entries by columns
--color[=WHEN] colorize the output; WHEN can be 'always' (default
if omitted), 'auto', or 'never'; more info below
-d, --directory list directories themselves, not their contents
...
...
...
Pay attention that the there are short and long variants of the same options:
-a
is the same as --all
, -d
is the same as --directory
and so on...
Let's cover some of the options:
3.2.1 Option -a or --all
This option can be used to show hidden files and directories that start with dot. The usage is pretty simple:
ls -a
the above command means: show absolutely all files and directories in the current directory. By absolutely all files and directories
I mean literally all records including hidden ones.
The output for me:
Now I will check the directory Pictures
again:
Oh, there's a secret directory inside it ๐
3.2.2 Option -l
Another useful option is -l
. It shows content in a long listing format:
Also, you will see file permissions, the number of links, owner name, owner group, file size, time of last modification, and the file or directory name. The d
at the beginning of row means directory ,-
means file.
3.2.3 Option -R
-R
is also a very powerful option. It shows content of a directory recursively:
From the output above we can see the following information:
- Directories
Desktop
,Documents
,Downloads
,Music
,Public
,Templates
,Videos
are empty; - The content of the directory
Pictures
.
By using only one command ls -R
we can observe the following structure:
๐/the_home_of_user
|_๐ Desktop(Empty)
|_๐ Documents(Empty)
|_๐ Downloads(Empty)
|_๐ Music(Empty)
|_๐ Pictures
|_๐ผ๏ธ Screenshot_2022-09-25_07_05_40.png
|_๐ผ๏ธ Screenshot_2022-09-25_07_06_20.png
|_๐ผ๏ธ Screenshot_2022-09-25_07_45_04.png
|_๐ผ๏ธ Screenshot_2022-09-25_07_06_15.png
|_๐ผ๏ธ Screenshot_2022-09-25_07_21_07.png
|_๐ summer2015
|_๐ผ๏ธ 1.png
|_๐ผ๏ธ 2.png
|_๐ผ๏ธ 3.png
|_๐ Public(Empty)
|_๐ Templates(Empty)
|_๐ Videos(Empty)
Pretty cool, right?
3.2.4 Combining options
Options can be combined. We can define them separately or together:
ls -l -a
is a full equivalent to
ls -la
Let's see combined options in action.
- Viewing home directory with long listing format including hidden files and directories:
- Viewing directory
Pictures
with long listing format including hidden files and directories: - Recursively viewing content of directory
Pictures
with long listing format: - Recursively viewing content of directory
Pictures
with long listing format including hidden files and directories: - Viewing the content of directory
Pictures
with long listing format including hidden files and directories with corresponding author for each directory and file (option--author
doesn't have a short variant):
3.3 Bonus
You can specify particular directories for viewing:
and ,of course, different options can be added:
4. Conclusion
In the article we have observed usage of ls
. It's very impressive and simple command for viewing content of directories. Of course, I haven't covered the remaining options but this can be your home task. Hope you have learned something new. Thanks for reading! =)