Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the ls command under Linux

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to use the ls command under Linux. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

If we have such a folder, we use the tree command to look at its directory structure:

Usage 1: list the details of all files and directories in the / home/alvin/test_dir directory

Command:

Ls-lR / home/alvin/test_dir/

Results:

[alvin@VM_0_16_centos test_dir] $ls-lR / home/alvin/test_dir/ / home/alvin/test_dir/: total 28-rw-rw-r-- 1 alvin alvin 37 Nov 18 09:12 atb_aux.c-rw-rw-r-- 1 alvin alvin 8 Nov 18 09:12 atb_can.c-rw-rw-r-- 1 alvin alvin 24 Nov 18 09:12 atb_orch.c-rw-rw-r-- 1 alvin alvin 5 Nov 18 09:12 atb_ota.c drwxrwxr-x 2 alvin alvin 4096 Nov 18 09:12 include-rw-rw-r-- 1 alvin alvin 0 Nov 18 09:12 Makefile drwxrwxr-x 2 alvin alvin 4096 Nov 18 09:12 output drwxrwxr-x 2 alvin alvin 4096 Nov 18 09:12 src / home/alvin/test_dir/include: total 0-rw-rw-r-- 1 alvin alvin 0 Nov 18 09:12 a.h-rw-rw-r-- 1 alvin alvin 0 Nov 18 09:12 B.H-rw-rw-r-- 1 alvin alvin 0 Nov 18 09:12 c.h / home/alvin/test_dir/output: total 0-rwxrwxr-x 1 alvin alvin 0 Nov 18 09:12 app / home/alvin/test_dir/src: total 0-rw-rw-r-- 1 alvin alvin 0 Nov 18 09:12 a.c-rw-rw-r-- 1 alvin alvin 0 Nov 18 09:12 b.c-rw-rw-r-- 1 alvin alvin 0 Nov 18 09:12 c.c

Here, the-l option should be familiar to you, which means to display the results in the form of a list. For the-R option, it means recursive processing, which processes all files and subdirectories under the specified directory together.

Usage 2: list the details of all files that begin with atb in the / home/alvin/test_dir directory

Command:

Ls-l atb*

Results:

[alvin@VM_0_16_centos test_dir] $ls-l atb*-rw-rw-r-- 1 alvin alvin 37 Nov 18 09:12 atb_aux.c-rw-rw-r-- 1 alvin alvin 8 Nov 18 09:12 atb_can.c-rw-rw-r-- 1 alvin alvin 24 Nov 18 09:12 atb_orch.c-rw-rw-r-- 1 alvin alvin 5 Nov 18 09:12 atb_ota.c

Usage 3: list only the subdirectories under the directory

(1) method 1:

Command

Ls-F / home/alvin/test_dir | grep / $

Results:

[alvin@VM_0_16_centos test_dir] $ls-F / home/alvin/test_dir | grep / $include/ output/ src/

The-F option indicates that a character is appended to each file name to indicate the type of file changed. "*": executable ordinary file; "/": directory; "@": symbolic link; "|": FIFOs; "=": socket.

/ $is actually a regular expression that ends with /. Grep / $means to filter out the results that end with /, that is, subdirectories.

(2) method 2:

Command:

Ls-p / home/alvin/test_dir | grep / $

Results:

[alvin@VM_0_16_centos test_dir] $ls-p | grep / $include/ output/ src/

The-p option is similar to the-F option by appending a character to each file name to indicate the type of modified file.

(3) method 3:

Command:

Ls-l / home/alvin/test_dir | grep "^ d"

Results:

[alvin@VM_0_16_centos test_dir] $ls-l / home/alvin/test_dir | grep "^ d" drwxrwxr-x 2 alvin alvin 4096 Nov 18 09:12 include drwxrwxr-x 2 alvin alvin 4096 Nov 18 09:12 output drwxrwxr-x 2 alvin alvin 4096 Nov 18 09:12 src

Where ^ d is also a regular expression that starts with d. Grep "^ d" means to filter out results that start with d, while the results listed in ls-l, if d first, indicate that the file is a directory, so that subdirectories can be filtered out.

(4) method 4:

Command:

Ls-d * /

Results:

[alvin@VM_0_16_centos test_dir] $ls-d * / include/ output/ src/

Where: the-d option means that the directory will be displayed like a file instead of the files under it.

Usage 4: list the files in the directory in chronological order, the newer they are, the later they are.

Command:

Ls-ltr

Results:

[alvin@VM_0_16_centos test_dir] $ls-lrt total 28 drwxrwxr-x 2 alvin alvin 4096 Nov 18 09:12 src drwxrwxr-x 2 alvin alvin 4096 Nov 18 09:12 output-rw-rw-r-- 1 alvin alvin 0 Nov 18 09:12 Makefile drwxrwxr-x 2 alvin alvin 4096 Nov 18 09:12 include-rw-rw-r-- 1 alvin alvin 5 Nov 18 09:12 atb_ota.c-rw-rw-r-- 1 alvin alvin 24 Nov 18 09:12 atb _ orch.c-rw-rw-r-- 1 alvin alvin 8 Nov 18 09:12 atb_can.c-rw-rw-r-- 1 alvin alvin 37 Nov 18 09:12 atb_aux.c

The-t option indicates that the file is sorted by modification time, and the newer the file is, the more advanced it is. The-r option indicates that the results are sorted in reverse, and the combination of the two means that the results are sorted by modification time, and the newer the results are, the later they are.

Usage 5: sort by file size

Command:

Ls-lhS

Results:

[alvin@VM_0_16_centos test_dir] $ls-lhS total 28K drwxrwxr-x 2 alvin alvin 4.0K Nov 18 09:12 include drwxrwxr-x 2 alvin alvin 4.0K Nov 18 09:12 output drwxrwxr-x 2 alvin alvin 4.0K Nov 18 09:12 src-rw-rw-r-- 1 alvin alvin 37 Nov 18 09:12 atb_aux.c-rw-rw-r-- 1 alvin alvin 24 Nov 18 09:12 atb_orch.c-rw-rw-r-- 1 Alvin alvin 8 Nov 18 09:12 atb_can.c-rw-rw-r-- 1 alvin alvin 5 Nov 18 09:12 atb_ota.c-rw-rw-r-- 1 alvin alvin 0 Nov 18 09:12 Makefile

The-h option indicates that it is displayed as a readable option, otherwise the file size is displayed by default in bytes, for example: 4873 bytes, do you know how much it is? Add the-h option system to help you convert to k or other units of the result.

The-S option indicates that the file is sorted by file size, and the larger the file is, the higher it is. If you want the person with a small file to come forward, just add the-r option.

Usage 6: count the number of files and directories in the current directory

(1) Statistics on the number of documents:

Command:

Ls-l | grep "^ -" | wc-l

Results:

[alvin@VM_0_16_centos test_dir] $ls-l | grep "^ -" | wc-l 5

Where: ^-means to start with -, that is, ordinary files, ls-l | grep "^ -" filter out ordinary files, and then use wc-l to count the number of ordinary files filtered out.

(2) number of statistical directories:

Command:

Ls-l | grep "^ d" | wc-l

Results:

[alvin@VM_0_16_centos test_dir] $ls-l | grep "^ d" | wc-l 3

Where: ^ d means to start with d, that is, directories, ls-l | grep "^ d" filter out directories, and then use wc-l to count the number of filtered directories.

Usage 7: list the absolute paths of all files

Command:

Ls | sed "s: ^: `pwd` /:"

Results:

[alvin@VM_0_16_centos test_dir] $ls | sed "s: ^: `pwd` /:" / home/alvin/test_dir/atb_aux.c / home/alvin/test_dir/atb_can.c / home/alvin/test_dir/atb_orch.c / home/alvin/test_dir/atb_ota.c / home/alvin/test_dir/include/home/alvin/test_dir/Makefile / home/alvin/test_dir/output/home/alvin/test_dir/src

Where: sed "s: ^: ``pwd`` /:" means to add pwd (that is, the path where the file is located) at the beginning of the line, which is combined with the file name to form an absolute path.

Usage 8: list the absolute paths of all files (including hidden files) in the current directory without recursion to the directory

In the previous usage, for hidden files (that is, with. The opening file) is not processed, and if we need to deal with hidden files, we need to use the following command:

Find $PWD-maxdepth 1 | xargs ls-ld

Results:

[alvin@VM_0_16_centos test_dir] $find $PWD-maxdepth 1 | xargs ls-ld drwxrwxr-x 5 alvin alvin 4096 Nov 18 17:30 / home/alvin/test_dir-rw-rw-r-- 1 alvin alvin 37 Nov 18 09:12 / home/alvin/test_dir/atb_aux.c-rw-rw-r-- 1 alvin alvin 8 Nov 18 09:12 / home/alvin/test_dir/atb_can.c-rw-rw-r-- 1 alvin alvin 24 Nov 18 09:12 / home/alvin/test_dir/atb_orch.c-rw-rw-r-- 1 alvin alvin 5 Nov 18 09:12 / home/alvin/test_dir/atb_ota.c drwxrwxr-x 2 alvin alvin 4096 Nov 18 09:12 / home/alvin/test_dir/include-rw-rw-r-- 1 alvin alvin 0 Nov 18 09:12 / home/alvin/test_dir/Makefile drwxrwxr-x 2 alvin alvin 4096 Nov 18 09:12 / home/alvin/ Test_dir/output drwxrwxr-x 2 alvin alvin 4096 Nov 18 09:12 / home/alvin/test_dir/src

Among them: find $PWD-maxdepth 1 is limited at the current level (that is, not recursive), and then the find result is passed to ls-ld as a parameter, so that all files in the current directory complete the path.

Thank you for reading! This is the end of the article on "how to use ls commands under Linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report