On ls command in Linux (analysis of 20 practical examples)

A basic command in Linux is ls. Without this command, we will encounter difficulties in browsing directory entries. This command must be known to everyone learning Linux.

What is ls

The ls command is used to list files and directories. By default, he will list the contents of the current directory. With the parameters, we can do more with ls. Here are some examples of ls usage used in daily operations.

On ls command in Linux (analysis of 20 practical examples)

1. Run ls without parameters

Running ls with no arguments will only list files or directories. Do not see any other information output (Annotation: Sometimes you find the ls command without parameters and described here is different, it may be your ls command actually takes the ls alias of the parameter).

$ ls

On ls command in Linux (analysis of 20 practical examples)

2. Use long list mode

Using the -l character (lowercase L character), a long list of the contents of the current directory is displayed. In the following example, we will combine the -l parameter (which is often used) to get better results.

$ ls -l

On ls command in Linux (analysis of 20 practical examples)

Here is how to read the output:

Column 1

The first letter d means that the content is a directory or a file. In the above screenshot, Desktop, Documents, Downloads, and Lynis-1.3.8 are directories. If it is '-' (minus sign), it means that its contents are files. When it is l (lowercase l characters), this means that the content is a link file.

The following 9 characters are about file permissions. The first three rwx characters are the permissions of the owner of the file, the second group 3rwx is the permissions of all the groups of the file, and the last rwx is the authority to access files for others.

Column 2 This line tells us how many links point to this file.

Column 3 This line tells us who is the owner of this file/folder.

Column 4 This line tells us who is the group for this file/folder.

Column 5 This line tells us the size of the file/folder in bytes. The size of the directory is always 4096 bytes.

Column 6 This tells us the last modification time of the file.

Column 7 This tells us the file name or directory name.

3. Show the file size

It may be inconvenient to look at the size in bytes. 6.5M is easier to read than 6,727,680 bytes. To do this, we can use the combined arguments of -h and -l. The -h parameter means that it is easy for people to identify.

$ ls -lh

On ls command in Linux (analysis of 20 practical examples)

Another parameter that can be done is -si. This parameter is similar to the -h parameter, but -si is in units of 1000, and -h is in units of 1024.

$ ls -si

On ls command in Linux (analysis of 20 practical examples)

4. Sort the file size

After we can display the file size, we want to sort by file size. We can use the -S parameter to do this. This list will be sorted from big to school.

$ ls -lhS

On ls command in Linux (analysis of 20 practical examples)

5. Measurement size

Ls can change the unit size by using -block-size=SIZE. The SIZE here is:

K = Kilobyte

M = Megabyte

G = Gigabyte

T = Terabyte

P = Petabyte

E = Exabyte

Z = Zettabyte

Y = Yottabyte

For example, we want to use MB as the unit size. So the syntax will look like this:

$ ls -l -block-size=M

On ls command in Linux (analysis of 20 practical examples)

6. Show hidden files

In Linux, files beginning with "." (dot) are hidden files. To display it in the ls command, we can use the -a option.

$ ls -a

On ls command in Linux (analysis of 20 practical examples)

7. List only directory entries

If we want to list only directories, we can use the -d option.

$ ls -d */

8. Does not print owner information

To do this, we use the -g option.

$ ls -g

On ls command in Linux (analysis of 20 practical examples)

9. Do not print group information

-g hides owner information, -G hides group information.

$ ls -lG

On ls command in Linux (analysis of 20 practical examples)

10. Print UID and GID

If you want to numerically list the owner and all groups of items (ie UID and GID), we can use the -l option with the ls command. Here is an example.

$ ls -n

From the above example, we know that user pungki has a UID of 100, a GID of 1000, and a root group with a GID of 0.

11. No color printing

Some Linux distributions have colored the ls command. This will make ls print the list in various colors. If you don't want this, you can use the -color=never parameter.

$ ls –color=never

12. Print the index number of each file

To print the index or commonly known inode number, we can use the -i option. The index number is displayed in the first column.

$ ls -li

On ls command in Linux (analysis of 20 practical examples)

13. Increase / (slash) mark directory

To do this, use the -p option.

$ ls -p

14. Reverse order when sorting

You may need to reverse the order when listing items. To do this, you can use the -r option.

$ ls -r

15. Recursively list subdirectories

With the -R parameter, you can list the directories that contain its subdirectories.

$ ls -R

16. Sorting Extensions

You can use the -X parameter or the -sort=extension to sort by extension (Annotation: This is useful for filtering different types of files).

$ ls -lX

or

$ ls –sort=extension

17. Listed by modification time

Use the -t option to sort by modification time, new files first.

$ ls -lt

18. List your home directory

To list your home directory, you can use "~" (tilde) to represent it. This way you don't have to enter the full directory name. Let's assume that the home file is named /home/pungki, so the tilde will make sense for /home/pungki.

$ ls ~

On ls command in Linux (analysis of 20 practical examples)

19. List parent directories

No matter where you are in that directory, you can list the parent directory without having to enter the full path. This is an example.

$ ls ../

This time lists the contents of the directory above the first floor.

$ ls ../../

This time lists the contents of the directory above the 2nd layer (Annotation: does not support "..." to represent the 2 layer above).

20. Print the ls command version

Print it using the –version parameter.

$ ls –version

On ls command in Linux (analysis of 20 practical examples)

to sum up

These are the parameters that will be used in daily operations. Of course you can always enter man ls or ls --help to query ls's man page.


12 Inch Coaxial Speaker

12 Coaxial Speaker,Coaxial Speaker 12 Inch,12 Inch Coaxial Speaker,Pro Audio Coaxial Speakers

Guangzhou Yuehang Audio Technology Co., Ltd , https://www.yhspeakers.com

Posted on