ls command
-
Function description: Lists a list of files (including subdirectories) in a specified directory, but does not display the contents of the files
-
Usage: ls [OPTION]... [FILE]...
option Effect -a Show all files, including hidden files -A Show all files except.And.. -l Show detailed properties of the file -h Converts the file size in units and displays them in a human-readable manner, resulting in inaccurate values -d View the directory itself instead of its internal file list, which is often used in conjunction with -l and -d. -r Reverse order display -R Recursively shows that if there are files in the directory, it will also print out the files in the directory
Example 1: Display all files in the current directory, including hidden files.
[root@node1 ~]# ls -a . anaconda-ks.cfg .bash_history .bash_profile .cache .cshrc .viminfo .. a.sh .bash_logout .bashrc .config .tcshrc
Example 2: Display all files except.And..In the current directory.
[root@node1 ~]# ls -A anaconda-ks.cfg .bash_history .bash_profile .cache .cshrc .viminfo a.sh .bash_logout .bashrc .config .tcshrc
Example 3: ls command displays the contents of the current directory without any parameters.
[root@node1 ~]# ls anaconda-ks.cfg a.sh
Example 4:ls command follows a path, displaying the contents under the specified path.
[root@node1 ~]# ls /var/ adm cache crash db empty ftp games gopher kerberos lib local lock log mail nis opt preserve run spool tmp yp
Example 5:ls command uses the -l option to display detailed attribute information for a file.
[root@node1 ~]# ls -l total 68 -rw-------. 1 root root 1652 Jul 25 19:40 anaconda-ks.cfg -rw-r--r--. 1 root root 48866 Jul 25 19:40 install.log -rw-r--r--. 1 root root 9451 Jul 25 19:37 install.log.syslog ...
File type:
Symbol | Representational Meaning |
---|---|
- | Normal file |
d | directory file |
b | Block device file (block) |
c | character device files |
l | Symbolic link file |
p | Command pipe file |
s | socket file |
Example 6:ls command uses the -h option to convert the file size into units and displays them in a human-readable manner, resulting in imprecise values.
[root@node1 ~]# ls -lh total 68K -rw-------. 1 root root 1.7K Jul 25 19:40 anaconda-ks.cfg -rw-r--r--. 1 root root 48K Jul 25 19:40 install.log -rw-r--r--. 1 root root 9.3K Jul 25 19:37 install.log.syslog ...
Example 7:ls uses the -ld option to view the properties of the directory itself, not the properties of its internal file list.Usually -l and -d are used together.
[root@node1 ~]# ls -ld /var/log/ drwxr-xr-x. 8 root root 4096 May 28 11:21 /var/log/
Example 8:ls command uses the -r option to display the contents of the current directory in reverse order.
[root@www ~]# mkdir /tmp/test [root@www ~]# cd /tmp/test/ [root@www test]# mkdir {a..f} [root@www test]# ls -l total 0 drwxr-xr-x 2 root root 6 Apr 8 03:53 a drwxr-xr-x 2 root root 6 Apr 8 03:53 b drwxr-xr-x 2 root root 6 Apr 8 03:53 c drwxr-xr-x 2 root root 6 Apr 8 03:53 d drwxr-xr-x 2 root root 6 Apr 8 03:53 e drwxr-xr-x 2 root root 6 Apr 8 03:53 f [root@www test]# ls -lr total 0 drwxr-xr-x 2 root root 6 Apr 8 03:53 f drwxr-xr-x 2 root root 6 Apr 8 03:53 e drwxr-xr-x 2 root root 6 Apr 8 03:53 d drwxr-xr-x 2 root root 6 Apr 8 03:53 c drwxr-xr-x 2 root root 6 Apr 8 03:53 b drwxr-xr-x 2 root root 6 Apr 8 03:53 a
Example 9:ls command uses the -R option to display recursively. If there are files in the directory, it will print the files in the directory as well.
[root@www test]# cd a [root@www a]# mkdir {a..d} [root@www a]# cd .. [root@www test]# ls -lR .: total 0 drwxr-xr-x 6 root root 38 Apr 8 03:56 a drwxr-xr-x 2 root root 6 Apr 8 03:53 b drwxr-xr-x 2 root root 6 Apr 8 03:53 c drwxr-xr-x 2 root root 6 Apr 8 03:53 d drwxr-xr-x 2 root root 6 Apr 8 03:53 e drwxr-xr-x 2 root root 6 Apr 8 03:53 f ./a: total 0 drwxr-xr-x 2 root root 6 Apr 8 03:56 a drwxr-xr-x 2 root root 6 Apr 8 03:56 b drwxr-xr-x 2 root root 6 Apr 8 03:56 c drwxr-xr-x 2 root root 6 Apr 8 03:56 d ./a/a: total 0