An example of 20 examples of ls usage used in Linux operations

A basic command in Linux is ls. Without this command, we will have difficulty browsing the directory entries. This command must be known to everyone who learns 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 everyday operations.

1. Run ls without parameters

Running ls with no arguments will only list files or directories. I can't see other information output. (Annotation: Sometimes you find that the ls command with no parameters is different from the one described here, it may be that your ls command actually has the ls alias with parameters).

$ ls

2. Use long list mode

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

$ ls -l

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 screenshot above, Desktop, Documents, Downloads, and lynis-1.3.8 are directories. If it is '-' (minus sign), this means that its content is a file. When it is l (lowercase l character), this means that the content is a link file.

The following 9 characters are about file permissions. The first 3 rwx characters are the permissions of the owner of the file, the second group 3rwx is the permissions of all groups of the file, and the last rwx is the permission to access files to 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 all the groups of this file/folder.

Column 5 This line tells us the size of this 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. Display file size

It may be inconvenient to look at the size in bytes. 6.5M is easier to read than 6727680 bytes. To do this, we can use the parameters combined with -h and -l. The -h parameter means easy identification.

$ ls -lh

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

4. Sort file size

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

$ ls -lhS

5. Measuring size

Ls can be resized 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 grammar will look like this:

$ ls -l –block-size=M

6. Show hidden files

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

$ ls -a

7. List only directory entries

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

$ ls -d */

8. Do not print owner information

To do this, we use the -g option.

$ ls -g

9. Do not print group information

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

$ ls -lG

10. Print UID and GID

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

$ ls -n

From the above example, we know that the UIP of the user pungki is 100, the GID is 1000, and the GID of the root group is 0.

11. Print without color

Some Linux distributions have enabled coloring for the ls command. This will cause ls to 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

In order to print the index or the commonly known inode number, we can use the -i option. The index number will be displayed in the first column.

$ ls -li

13. Add / (slash) tag directory

To do this, use the -p option.

$ ls -p

14. Reverse order when sorting

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

$ ls -r

15. Recursively listing subdirectories

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

$ ls -R

16. Extension Sorting

You can use the -X parameter or –sort=extension to sort by extension (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, with the new file 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 name is /home/pungki, then the tilde makes sense for /home/pungki.

$ ls ~

19. List parent directory

No matter where you are in the 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 1st floor.

$ ls ../../

This time, the contents of the directory above the 2nd layer are listed (Annotation: "..." is not supported to represent the 2nd layer).

20. Print the ls command version

Print it using the –version parameter.

$ ls –version

to sum up

These are the parameters that will be used in daily operations. Of course, you can always type man ls or ls –help to query the ls man page.

Thermostats

Heating Element Temperature Control,Electric Grill Temperature Controller,Thermostat For Heating Element,Oven Capillary Thermostat

Foshan City Jiulong Machine Co., Ltd , https://www.jlthermostat.com

Posted on