20 practical examples of linuxls commands

Source: Internet
Author: User
In Linux, a basic command is ls. Without this command, we will encounter difficulties when browsing directory entries. This command must be known to anyone studying Linux.

In Linux, a basic command is ls. Without this command, we will encounter difficulties when browsing directory entries. This command must be known to anyone studying Linux.

What is ls?

The ls command is used to list files and directories. By default, it lists the contents of the current directory. After parameters are included, we can use ls to do more things. Here are some examples of ls usage in daily operations.

1. run ls without parameters

Running ls without parameters will only list files or directories. Other information output is not visible (Note: Sometimes you find that the ls command without 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 the long list mode

Use the-l character (lower-case L character) to display the long list of the contents of the current directory. In the following 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:

1st columns

First letter DIt means that the content is a directory or file. In the above, Desktop, Documents, Downloads and lynis-1.3.8 are directories. If it is '-'( Minus sign), Which means that its content is a file. When it is l ( Lowercase l characters), Which means the content is a linked file. The following nine characters are about file permissions. First 3 rwxCharacters are the permissions of the object owner, Group 2 3rwxIs the permission of all groups of files, Last rwxIs the permission for others to access files.

2nd columnsThis line tells us how many links point to this file.

3rd columnsThis line tells us who is the owner of this file/folder.

4th columnsThis line tells us who is the group of this file/folder.

5th columnsThis line shows the size of the file/folder in bytes. The directory size is always 4096 bytes.

6th columnsThis tells us the last modification time of the file.

7th columnsThis tells us the file name or directory name.

3. display file size

The size in bytes may be inconvenient. 6.5M reading is simpler than 6727680 bytes. To do this, we can use-h and-LParameters.-H parameter means easy for Human Identification.

$ Ls-lh

Another parameter that can be done is-- Si. This parameter is similar to the-h parameter,-Si is in the unit of 1000, while-h is in the unit of 1024.

$ Ls-si

4. sort file size

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

$ Ls-lhS

5. measurement size

Ls can be used-Block-size = SIZEChange the unit size. The SIZE here is:

K = KilobyteM = MegabyteG = GigabyteT = TerabyteP = PetabyteE = ExabyteZ = ZettabyteY = Yottabyte

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

$ Ls-l -- block-size = M

6. show hidden files

In Linux "."(Period. To display it in the ls command, we can use-.

$ Ls-

7. only list directory entries

If we want to list only directories, we can use-D.

$ Ls-d */

8. do not print owner information

To do this, we use-G.

$ Ls-g

9. do not print group information

-G hides the owner information,-GThe group information is hidden.

$ Ls-lG

10. print UID and GID

If you want to list the owner and all groups (UID and GID) of an item in numbers-NOption to use the ls command. Here is an example.

$ Ls-n

From the above example, we know thatUser pungki UIDIs 100,GID is 1000, AndThe GID of the root group is 0..

11. printing without color

Some Linux distributions have enabled color for ls commands. This causes ls to print the list in various colors. If you do not want this, you can use-- Color = neverParameters.

$ Ls -- color = never

12. print the index number of each file

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

$ Ls-li

13. Add/(slash) to mark the Directory

To do this, use-P option.

$ Ls-p

14. Reverse order during sorting

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

$ Ls-r

15. recursively list subdirectories

Tape-RYou can list the directories that contain its subdirectories.

$ Ls-R

16. sort extensions

You can use the-X parameter or -- sort = extension to sort objects by extension ).

$ Ls-lX

Or

17. use the modification time to list

The-t option is sorted by the modification time, and the new files are in the front.

$ Ls-lt

18. list your home directories

To list your home directory, you can use "~ "(Wave number. In this way, you do not need to enter the complete directory name. Let's assume that the home file name is/Home/pungki, ThenWave numberIt makes sense to/home/pungki.

$ Ls ~

19. list parent directories

No matter which Directory you are in, you can list the parent directory without entering the full path. This is an example.

$ Ls ../

List this time1Directory content on the layer.

$ Ls ../../

List this time2Directory content on the layer (note: "..." is not supported to represent the contents on the layer 2 ).

20. print the ls command version

Print it using the -- version parameter.

$ Ls -- version

Summary

These are the parameters used in daily operations. You can always enterMan lsOrLs -- helpTo query the ls manual page.

Via: http://linoxide.com/linux-command/linux-ls-command/

Translator: geekpi proofreader: wxy

This article was originally translated by LCTT and launched with the Linux honor in China

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.