8 Examples of Advanced Usage of ls Command in Linux
Under Linux, everyone must be familiar with the ls command. Liang Xu believes that as long as he is a Linux engineer, he will be inseparable from this command every day, and will use it hundreds of times a day. But besides ls -l, what advanced uses of ls do you know? Liang Xu will introduce 8 advanced usages of the ls command today.
If we have such a folder, we use the tree command to view its directory structure:
Usage 1: List the details of all files and directories in the /home/alvin/test_dir directory
Order:
ls -lR /home/alvin/test_dir/
result:
[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 everyone, which means to display the results in the form of a list. For the -R option, it means recursive processing, processing all files and subdirectories in the specified directory together.
Usage 2: List the details of all files starting with atb in the /home/alvin/test_dir directory
Order:
ls -l atb*
result:
[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 subdirectories under a directory
- method 1:
Order
ls -F /home/alvin/test_dir | grep /$
result:
[alvin@VM_0_16_centos test_dir]$ ls -F /home/alvin/test_dir | grep /$ include/ output/ src/
Among them: the -F option means that a character is appended to each file name to indicate the type of the file. "*": Indicates executable ordinary file; "/": Indicates directory; "@": Indicates symbolic link; "|": Indicates FIFOs; "=": Indicates socket.
/$ is actually a regular expression that ends with /. grep /$ means to filter out the results ending with /, that is, subdirectories.
- Method 2:
Order:
ls -p /home/alvin/test_dir | grep /$
result:
[alvin@VM_0_16_centos test_dir]$ ls -p | grep /$ include/ output/ src/
Among them: the -p option is similar to the -F option, and also appends a character to each file name to indicate the type of the file to be changed.
- Method 3:
Order:
ls -l /home/alvin/test_dir | grep "^d"
result:
[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 the results starting with d, and the results listed by ls -l, if the first one is d, it means that the file is a directory, so that you can filter out subdirectories.
- Method 4:
Order:
ls -d */
result:
[alvin@VM_0_16_centos test_dir]$ ls -d */ include/ output/ src/
Among them: the -d option indicates that the directory is displayed as a file, but the files under it are not displayed.
Usage 4: List the files in the directory in chronological order, the newer the later.
Order:
ls -ltr
result:
[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
Among them: the -t option indicates that the files are sorted by modification time, and the newer is the first. The -r option indicates that the results are sorted in reverse. If the two are combined, they are sorted by modification time. The newer the later, the later.
Usage 5: Sort by file size
Order:
ls -lhS
result:
[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
Among them: the -h option indicates that it is displayed as a readable option, otherwise the file size is displayed in bytes by default, for example: 4873 bytes, do you know how much it is? Add the -h option and the system will help you convert the result in k or other units.
The -S option means to sort by file size, the larger the file, the higher. If you want the smallest file to be first, just add the -r option.
Usage 6: Count the number of files and directories in the current directory
- Statistics files:
Order:
ls -l | grep "^-" | wc -l
result:
[alvin@VM_0_16_centos test_dir]$ ls -l | grep "^-" | wc -l 5
Among them: ^- means that it starts with -, that is, ordinary files, ls -l | grep "^-" filters out ordinary files, and then uses wc -l to count the number of ordinary files filtered out.
- Statistics directory number:
Order:
ls -l | grep "^d" | wc -l
result:
[alvin@VM_0_16_centos test_dir]$ ls -l | grep "^d" | wc -l 3
Among them: ^d means starting with d, that is, the directory, ls -l | grep "^d" filters out the directory, and then uses wc -l to count the number of filtered directories.
Usage 7: List absolute paths of all files
Order:
ls | sed "s:^:`pwd`/:"
result:
[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
Among them: sed "s:^:``pwd``/:" means adding pwd (that is, the path where the file is located) at the beginning of the line, and combining it 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, hidden files (that is, files starting with . ) are not processed. If we need to process hidden files, we need to use the following command:
find $PWD -maxdepth 1 | xargs ls -ld
result:
[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 to the current level (that is, not recursive), and then pass the find result to ls -ld as a parameter, so that all files in the current directory are filled with the path.
For more exciting content, please pay attention to the public account Liangxu Linux. The public can reply to 1024 to get 5T technical materials for free, including: Linux, C/C++, Python, Raspberry Pi, embedded, Java, artificial intelligence, etc. Reply to the group in the official account, and invite you to join the expert Ruyun technology exchange group.
Public number: Liangxu Linux
