ls command of Linux

ls command is the most commonly used command under linux. ls command is the abbreviation of list. By default, ls is used to print the list of the current directory. If ls specifies another directory, the list of files and folders in the specified directory will be displayed. With the ls} command, you can view not only the files contained in the linux folder, but also the file permissions (including directory, folder, file permissions), directory information, and so on. ls command is used a lot in daily linux operation!

1. Command format:

ls [options] [directory name]

2. Command function:

Lists all subdirectories and files in the target directory.

3. Common parameters:

4. Common examples:

Example 1: list the details of all files and directories in the / home/rumenz folder

Command: ls - l - R / home/rumenz

When using the - ls - command, pay attention to the format of the command: after the command prompt, the first is the keyword of the command, followed by the command parameters. There should be a short horizontal line "-" before the command parameters. All command parameters have specific functions. You can select one or more parameters as needed, and the operation object of the command is after the command parameters. In the above command "ls - l - R / home/rumenz", ls is the command keyword, "- l - R" is the parameter, and "/ home/rumenz" is the operation object of the command. In this command, two parameters are used, namely "l" and "R". Of course, you can also use them together, as shown below:

Command: ls - lR / home/rumenz

The result of this form is exactly the same as that of the command form above. In addition, if the operation object of the command is located in the current directory, the operation object can be operated directly; If it is not in the current directory, you need to give the full path of the operation object. For example, in the above example, my current folder is the rumenz folder. I want to operate the rumenz file under the home folder. I can directly enter {ls - lR} rumenz or use} ls - lR / home/rumenz.

Example 2: list the details of all directories starting with "t" in the current directory. You can use the following command:

Command: ls - l # t*

You can view the information of all files whose file names start with "t" in the current directory. In fact, in the command format, the contents in square brackets can be omitted. For the command ls, if you omit the command parameters and operation objects and directly enter "LS", the content list of the current working directory will be listed.

Example 3: only the subdirectories under the file are listed

Command: ls - F / opt/soft | grep / $

List the subdirectories under the / opt/soft} file

[root@localhost rumenz]# ls -F $PWD | grep /$

excache/
hsperfdata_deploy/
hsperfdata_root/
poifiles/

Command: ls - l / opt/soft | grep "^ d"

List the subdirectory details under the / opt/soft} file

[root@localhost rumenz]#  ls -l $PWD | grep "^d"

drwxr-xr-x 2 root root       21 Jan 13 14:33 app
drwxr-xr-x 4 root root      141 Jan 25 21:20 web
drwxr-xr-x 3 root root       57 Dec 18 22:17 web-test

Example 4: list all files in the current working directory whose names start with s , and the newer they are, the later they are. You can use the following command:

Command: ls - ltr} s*

[root@localhost rumenz]# ls -ltr w*

web-test:
total 4428
drwxr-xr-x 6 root root     108 Dec 18 22:17 static
-rw-r--r-- 1 root root     572 Dec 18 22:17 index.html
-rw-r--r-- 1 root root 4528805 Dec 18 22:17 dist.tar.gz

web:
total 81656
-rw-r--r-- 1 root root  5277607 Aug 16 00:12 dist815.zip
drwxr-xr-x 6 root root      108 Dec  8 14:21 static
-rw-r--r-- 1 root root      572 Dec  8 14:21 index.html
-rw-r--r-- 1 root root  4514510 Dec  8 14:21 dist.tar.gz

Example 5: list all files and directories under the current working directory; Add "/" after the name of the directory and "*" after the name of the executable

Commands: ls - AF

[root@localhost rumenz]# ls -AF

log/  script/  soft/  src/  svndata/  web/

Example 6: calculate the number of files and directories in the current directory

Command:

ls - l * |grep "^ -" |wc - l -- number of files

ls - l * |grep "^ d"|wc - l -- number of directories

Example 7: list the absolute path of the file in ls

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

[root@localhost rumenz]# ls | sed "s:^:`pwd`/:"
/opt/log
/opt/script
/opt/soft
/opt/src
/opt/svndata
/opt/web

Example 9: list the absolute paths of all files (including hidden files) in the current directory without recursion

Command: find $PWD - maxdepth | 1 | xargs | ls - ld

[root@localhost rumenz]# find $PWD -maxdepth 1 | xargs ls -ld

drwxrwxrwt. 78 root   root      4096 Jan 25 23:09 /tmp
drwx------   2 root   root         6 Jan  8 16:10 /tmp/20210108_161044-scantem.0ef7dea9d3
drwx------   3 root   root        33 Jan  8 16:12 /tmp/20210108_161248-scantem.62a5f98367
drwx------   2 root   root         6 Jan  8 16:57 /tmp/20210108_165745-scantem.f73926d239

Example 10: recursively list the absolute paths of all files (including hidden files) in the current directory

Command: find $PWD | xargs | ls - ld

Example 11: specify file time output format

Command:
ls -tl --time-style=full-iso

[root@localhost soft]# ls -lt --time-style=full-iso

total 0
drwxrwxr-x 3 deploy deploy 18 2021-01-25 15:35:57.075199271 +0800 tomcat.120562076922433750.8080
drwxrwxr-x 2 deploy deploy  6 2021-01-25 15:35:57.047196910 +0800 tomcat-docbase.6189031708285654679.8080
drwxr-xr-x 2 deploy deploy 56 2021-01-25 15:35:51.952767467 +0800 hsperfdata_deploy

Output:

[root@localhost soft]# ls -ctl --time-style=long-iso

total 0
drwxrwxr-x 3 deploy deploy 18 2021-01-25 15:35 tomcat.120562076922433750.8080
drwxrwxr-x 2 deploy deploy  6 2021-01-25 15:35 tomcat-docbase.6189031708285654679.8080
drwxr-xr-x 2 deploy deploy 56 2021-01-25 15:35 hsperfdata_deploy
drwxrwxr-x 3 deploy deploy 18 2021-01-25 10:28 tomcat.7686949051420446439.8080

Displays a list of color catalogs

Open / etc/bashrc and add the following line:

alias ls="ls --color"

The next time you start bash, you can display a colorful directory list like in Slackware. The meaning of color is as follows:

  • Blue -- > directory
  • Green -- > executable
  • Red -- > compressed file
  • Light blue -- > linked file
  • Gray -- > other files

Original link: https://rumenz.com/rumenbiji/...
WeChat official account: entry station

linux common command quick reference manual PDF download

Alicloud ECS O & M Linux system diagnosis PDF download

Docker quick reference manual PDF download

Linux learning notes [strong summary worth seeing] PDF download

shell concise tutorial PDF download

Keywords: Linux

Added by zalath on Fri, 10 Dec 2021 12:48:06 +0200