Common usage of ls command in Linux

  sonic0002        2018-12-16 03:00:42       6,561        0    

In Linux, ls might be one of the most frequently used commands. It is used to list files in a specific directory. In most cases, we may just use ls -l to list the files under a directory. But have you explored other usages of this command. In this post, we will summarise some other common usages of ls command

Assuming we have below directory structure in our system, let's see them using tree command.

List details of the files

If we want to list the details of the files in /home/alvin/test_dir, we can fun

ls -lR /home/alvin/test_dir/

Output:

[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 -l is to display the details in list format. But how about -R? It tells the command to list the files recursively which will show all the files and the files in sub directories as well.

List files starting with atb

To list files starting with some characters, we could use the commonly used regular expression wildcards.

ls -l atb*

Output:

[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

List only sub directories

Method 1

If in some cases we only want to know what sub directories a specified directory has, we can run below command.

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

Output:

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

-F option is to append a character to the file to indicate the type of the file.

  • * indicates the file is a normal file
  • / indicates the file is a directory
  • @ indicates the file is a symbolic link
  • | indicates the file is FIFOs
  • = indicates the files is pipe

The grep /$ is actually to list all output ending with /.

Method 2

Using -p option. 

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

Output:

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

-p is similar to -F which also appends the file type after file name.

Method 3

Most commonly used is to find d in the list.

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

Output:

[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

^d means that the output which starts with d which is a directory.

Method 4

Using -d option will list directories like files.

ls -d */

Output:

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

List files in reversed time order

This is also a frequently used command to check the order of files by modification time.

ls -ltr

Output:

[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

-t is to list file in time order, -r is to sort the files in reverse order. 

List files by file size

ls -lhS

Output:

[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

-h means listing files in human readable format, otherwise file size will be displayed in byte size such as 4873 bytes. -S is to sort file files by file size. By default, larger files will be in the top,if want to reverse the order, i.e, small files come first, using -r option.

Count file numbers and directory numbers

Counting files

ls -l | grep "^-" | wc -l

Output:

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

^- is for listing normal files. wc -l will count the lines of the output. By combining them, the number of files can be counted.

Counting directories

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

Output:

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

^d is to listing directories.

List absolute path of files

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

Output:

[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

The command sed "s:^:``pwd``/:" is to prepend the current directory path of the file. It will be the absolute path of the file when combined together. 

List absolute path of all files in directory including hidden files

In above command, hidden files will not be listed. In cases if want to list hidden files as well, can run below.

find $PWD -maxdepth 1 | xargs ls -ld

Output:

[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

Command find $PWD -maxdepth 1 will only find the current directory, but skipping the sub directories.  

Reference: https://blog.csdn.net/yychuyu/article/details/84898983

LS  SHELL  LINUX 

       

  RELATED


  0 COMMENT


No comment for this article.



  RANDOM FUN

Mathematician loses to barman