The ls command in linux USES elaboration

  • 2020-05-13 04:02:44
  • OfStack

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

1. Command format:
ls [option] [directory name]

2. Command function:

Lists all subdirectories and files in the target directory.

3. Common parameters:

-a, and the copyrighted files list all the files in the directory, including the hidden files beginning with
-A is the same as -a, but does not list ". "and".." Represents the parent directory of the current directory.
-c collation -lt: collation by ctime and display ctime (when the file status was last changed) collation -l: display ctime but collation by name otherwise: collation by ctime
-C lists items in each column from top to bottom
The ambien 39 en [=WHEN] controls whether to use color - resolved files. WHEN can be either 'never', 'always' or 'auto'
-d, and directory display the image of the video file 1 instead of the file below it.
-D, wokdired produces results suitable for Emacs's dired mode usage
-f does not sort the output files, the -aU option takes effect, and the -lst option fails
-g is similar to -l, but does not list the owner
-G, the wokno-group does not list any information about groups
-h, WSD - human-readable - list the file sizes in an easy-to-understand format (e.g. 1K 234M 2G)
WSD 72en is like -h, but the file size is 1000 to the power instead of 1024
Use symbolic links in the command line to indicate the actual destination
The WSD 80en-style = method specifies that each item name is followed by an indicator < way > : none (default), classify (-F), file-type (-p)
-i, the copyright holder prints the inode number for each file
-I, and ignore= style does not print any shell characters < style > The project of
-k, i.e., hang block-size =1K, represents the size of the file in k bytes.
-l lists the file permissions, owner, file size, and other details in addition to the file name.
When displaying the file information of a symbolic link, display the information of the object indicated by the symbolic link instead of the symbolic link itself
-m all items are separated by commas and fill the entire line width
-o is similar to -l, showing details of the file in addition to group information.
-r, hang reverse in reverse order
-R, and the copyright holder displays all the subdirectory layers at the same time
-s, the size of all files is listed in block size
-S sorted by file size
The following are the available WORD and the corresponding options they represent:
extension -X status -c
none -U time -t
size -S atime -u
time -t access -u
version -v use -u
-t sorted by file modification time
-u -lt: displays access times and orders by access times
Match -l: displays access times but sorts by name
Otherwise: sort by access time
-U does not sort; List items in the original order of the file system
-v sort by version
-w, and width=COLS specify the width of the screen without using the current value
-x lists items row by row instead of column by column
-X sorted by extension
-1 only one file is listed per line
The help message is displayed and left
The version information is displayed and left

4. Common examples:

Example 1: lists details of all files and directories in the /home/peidachang folder

Command: ls-l-R /home/peidachang
Should pay attention to when using ls commands command format: after the command prompt, first of all, is the key to the command word, followed by the command parameters, before command parameters to have 1 dash "-", all of the command parameters has a specific role, you can choose one or more parameters according to need, in the back of the order parameter is the order of operations. In the above command "ls-l-R /home/peidachang", "ls" is the command keyword, "-l-R" is the parameter, and "/home/peidachang" is the operation object of the command. In this command, two parameters are used, "l" and "R", of course, you can also use them in 1, as shown below:
Command: ls-lR /home/peidachang
The result of this form and the above command form is exactly the same. In addition, if the operation object of the command is located in the current directory, you can directly operate on the operation object; If you are 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 peidachang folder. I want to operate the peidachang files in the home folder, I can directly enter ls-lR peidachang, or ls-home /home/peidachang.

Example 2: list the details of all directories in the current directory that begin with "t" using the following command:

Command: ls -l t*
You can view information on all files in the current directory whose file names begin with "t." In fact, in the command format, the contents inside the square brackets can be omitted. For the command ls, if you omit the command parameters and operation objects and directly enter "ls", a list of the contents 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
Output:
[root@localhost opt]# ls -F /opt/soft |grep /$
jdk1.6.0_16/
subversion-1.6.1/
tomcat6.0.32/

Command: ls-l /opt/soft | grep "^d"
List the subdirectories below the /opt/soft file
Output:
[root@localhost opt]# ls -l /opt/soft | grep "^d"
drwxr-xr-x 10 root root 4096 09-17 18:17 jdk1.6.0_16
drwxr-xr-x 16 1016 1016 4096 10-11 03:25 subversion-1.6.1
drwxr-xr-x 9 root root 4096 2011-11-01 tomcat6.0.32

Example 4: list all files in the current working directory whose names begin with s. The newer the file, the later the file, you can use the following command:

Command: ls -ltr s*
Output:
[root@localhost opt]# ls -ltr s*
src:
A total of zero
script:
A total of zero
soft:
A total of 350644
drwxr-xr-x 9 root root 4096 2011-11-01 tomcat6.0.32
-rwxr-xr-x 1 root root 81871260 09-17 18:15 jdk-6u16-linux-x64.bin
drwxr-xr-x 10 root root 4096 09-17 18:17 jdk1.6.0_16
-rw-r--r-- 1 root root 205831281 09-17 18:33 apache-tomcat-6.0.32.tar.gz
-rw-r--r-- 1 root root 5457684 09-21 00:23 tomcat6.0.32.tar.gz
-rw-r--r-- 1 root root 4726179 10-10 11:08 subversion-deps-1.6.1.tar.gz
-rw-r--r-- 1 root root 7501026 10-10 11:08 subversion-1.6.1.tar.gz
drwxr-xr-x 16 1016 1016 4096 10-11 03:25 subversion-1.6.1

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

Command: ls -AF
Output:
[root@localhost opt]# ls -AF
log/ script/ soft/ src/ svndata/ web/

Example 6: count 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 to the file in ls

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

Example 8: lists the absolute path to all files (including hidden files) in the current directory, without recursion to the directory
Order: find $PWD-maxdepth 1 | xargs ls-ld
Output:
[root@localhost opt]# find $PWD -maxdepth 1 | xargs ls -ld
drwxr-xr-x 8 root root 4096 10-11 03:43 /opt
drwxr-xr-x 2 root root 4096 2012-03-08 /opt/log
drwxr-xr-x 2 root root 4096 2012-03-08 /opt/script
drwxr-xr-x 5 root root 4096 10-11 03:21 /opt/soft
drwxr-xr-x 2 root root 4096 2012-03-08 /opt/src
drwxr-xr-x 4 root root 4096 10-11 05:22 /opt/svndata
drwxr-xr-x 4 root root 4096 10-09 00:45 /opt/web

Example 9: recursively lists the absolute path to all files (including hidden files) in the current directory

Command: find $PWD | xargs ls -ld

Example 10: specify the file time output format
Command:
ls -tl --time-style=full-iso
Output:

[root@localhost soft]# ls -tl --time-style=full-iso
A total of 350644
drwxr-xr-x 16 1016 1016 4096 2012-10-11 03:25:58.000000000 +0800 subversion-1.6.1

ls -ctl --time-style=long-iso
Output:
[root@localhost soft]# ls -ctl --time-style=long-iso
A total of 350644
drwxr-xr-x 16 1016 1016 4096 2012-10-11 03:25 subversion-1.6.1

Extension:
1. Display a list of color catalogs
Open /etc/bashrc and add the following line:
alias ls="ls --color"
The next time you launch bash, you will be able to display a list of colored directories as you did in Slackware. The colors have the following meanings:
1. The blue - > directory
2. Green -- -- > Executable file
3. The red - > The compressed file
4. Light blue -- > Link to the file
5. - gray > Other documents


Related articles: