1) [  Linux commands series ]  ls

1) [ Linux commands series ] ls

An overview of ls command with examples

ยท

5 min read

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]
  1. Square brackets [] mean that the section is optional and can be omitted.
  2. [OPTION(s)] is the section where various options can be put.
  3. [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: Screenshot from 2022-09-25 15-12-49.png

  1. I haven't specified any option for ls.
  2. I haven't specified a particular directory name for ls. It means that ls will print out the content of current directory. This is a default behavior.

Let's see what does directory Pictures contains: Screenshot from 2022-09-25 15-13-41.png

For me there are 5 screenshots and a folder. I will check the content of summer2015 by typing:

ls Pictures/summer2015

the output: Screenshot from 2022-09-25 15-15-54.png

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, -dis 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: Screenshot from 2022-09-25 15-19-37.png Now I will check the directory Pictures again: Screenshot from 2022-09-25 15-21-45.png 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: Screenshot from 2022-09-25 15-26-20.png 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: Screenshot from 2022-09-25 15-52-47.png From the output above we can see the following information:

  1. Directories Desktop, Documents, Downloads, Music, Public, Templates, Videos are empty;
  2. 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.

  1. Viewing home directory with long listing format including hidden files and directories: Screenshot from 2022-09-25 17-37-31.png
  2. Viewing directory Pictures with long listing format including hidden files and directories: Screenshot from 2022-09-25 15-37-06.png
  3. Recursively viewing content of directory Pictures with long listing format: Screenshot from 2022-09-25 17-40-41.png
  4. Recursively viewing content of directory Pictures with long listing format including hidden files and directories: Screenshot from 2022-09-25 17-04-41.png
  5. 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): Screenshot from 2022-09-25 18-40-23.png

3.3 Bonus

You can specify particular directories for viewing: Screenshot from 2022-09-25 17-46-44.png

and ,of course, different options can be added: Screenshot from 2022-09-25 17-47-46.png

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! =)

ย