Detailed usage of ls command in Linux

English full name: List is the meaning of list. When we learn something, we need to know why. When you know what this thing is probably, your mind will associate a lot of things with learning. soon.
 
1. ls -a lists all files under the file, including hidden files starting with "." (the hidden files of files under linux start with . If there is .., it means that there is a parent directory).
2. ls -l lists the detailed information of the file, such as creator, creation time, list of read and write permissions of the file, etc.
3. ls -F adds a character at the end of each file to indicate the type of the file. "@" means symbolic link, "|" means FIFOS, "/" means directory, "=" means socket.
4. ls -s prints the file size after each file. size (size)
5. ls -t sorts files by time Time (time)
6. ls -A Lists files other than "." and "..".
7. ls -R lists all the files in the subdirectories of the directory, which is equivalent to the "recursive" implementation in our programming.
8. ls -L lists the link names of the files. Link
9. ls -S sort by file size

The above are some usages of the ls command. Of course, there are many that are not listed. You can man it. Helping to learn English
ls can be combined with the pipe character "|" to perform complex operations. For example: ls | less is used to implement paging of file lists, ls  
 
calculates the number of files in the current directory
ls -|*|grep "^-"| wc -|
 
Finally, let's talk about the meaning of some file colors of files under linux
Green -- --> stands for executable file, (green stands for passport) Oh
red ----> stands for compressed file
dark blue ----> stands for directory
light blue ----> stands for link file
grey--- -> represents some other files

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

1. Command format:

ls [options] [directory name]

2. Command function:

List all subdirectories and files in the target directory.

3. Common parameters:

-a, --all List all files in the directory, including implicit files starting with .
-A Same as -a, but do not list "." (representing the current directory) and ".." (representing the parent of the current directory content).
-c with -lt: sort by ctime and display ctime (the time the file status was last changed) with -l: show ctime but sort by name else: sort by ctime
-C list items from top to bottom in each column
--color[=WHEN ] Controls whether to use a color-resolved file. WHEN can be one of 'never', 'always' or 'auto'
-d, --directory Display the directory as a file instead of the files under it.
-D, --dired produce results suitable for use in Emacs's dired mode
-f do not sort output files, -aU option is valid, -lst option is invalid
-g Like -l, but do not list owner
-G, -no -group do not list any information about groups
-h, --human-readable list file sizes in a human-readable format (eg 1K 234M 2G)
-si Like -h, but with file sizes to the power of 1000 instead of 1024
- H, --dereference-command-line use the real destination indicated by the symlink in the command line
--indicator-style=style specifies the indicator <style> after each item name: none (default), classify (-F ), file-type (-p)
-i, --inode print the inode number of each file
-I, --ignore=style do not print any items that match the shell wildcard <style>
-k ie --block-size=1K, the size of the file in k bytes.
-l In addition to the file name, it also lists the file permissions, owner, file size and other information in detail.
-L, --dereference When displaying the file information of a symbolic link, display the object indicated by the symbolic link instead of the information of the symbolic link itself
-m All items are separated by commas and fill the entire line width
-o Similar to -l, display Details of the file other than group information.   
-r, --reverse in reverse order
-R, --recursive list all subdirectory levels simultaneously

-s, --size List all file sizes in blocks
-S Sort by file size
--sort=WORD The following WORDs are available 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 sort by file modification time
-u with -lt: show access time and sort by access time
with -l: show access time but by name sort
otherwise: sort by access time
-U don't sort; list items in original file system order
-v sort by version
-w, --width=COLS specify screen width without using current value
-x row by row list items instead of column by column
-X sort by extension
-1 list only one file per line
--help show this help and leave
--version show version information and leave

4. Common examples:

Example 1: List the details of all files and directories under the /home/peidachang folder

Command: ls -l -R /home/peidachang
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 parameter, and there should be a dash before the command parameter "-", all command parameters have specific functions, you can choose one or more parameters according to your needs, after the command parameters is the operation object of the command. 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 together, as shown below:
Command: ls -lR /home/peidachang
This form The result is exactly the same as the execution of the above command form. In addition, if the operation object of the command is located in the current directory, you can directly operate the operation object; 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 peidachang folder, I want to operate the peidachang file in the home folder. I can directly input ls -lR peidachang, or use ls -lR /home/peidachang.

Example 2: To list the detailed contents of all directories starting with "t" in the current directory, you can use the following command:

Command: ls -lt*   
can view the information of all files whose file names start with "t" in the current directory. In fact, in the command format, the content in square brackets can be omitted. For the command ls, if the command parameter and operation object are omitted, and "ls" is directly input, the content list of the current working directory will be listed.

Example 3: List only subdirectories under the file

Command: ls -F /opt/soft |grep /$  
List the subdirectories under the /opt/soft file
Output:

 

copy code
code show as below:

[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"
lists the details of the subdirectories under the /opt/soft file.
Output:

 

copy code
code show as below:

[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 the files whose names start with s in the current working directory. The newer the later, the following command can be used:

Command: ls -ltr s*
output:
[root@localhost opt]# ls -ltr s*
src:
total 0

script:
total 0

soft:
总计 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 files and directories in the current working directory; add "/" after the directory name, and add "*" after the executable file name

Command: ls -AF
Output:
[root@localhost opt]# 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 files 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 9: List the absolute paths of all files (including hidden files) in the current directory, without recursion to the directory

命令:find $PWD -maxdepth 1 | xargs ls -ld

output:

 

copy code
code show as below:

[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 10: Recursively list the absolute paths of all files (including hidden files) in the current directory
: find $PWD | xargs ls -ld

Example 11: Specify the file time output format
Command:
 ls -tl --time-style=full-iso
Output:
[root@localhost soft]# ls -tl --time-style=full-iso 
Total 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
total 350644
drwxr-xr-x 16 1016 1016 4096 2012-10-11 03 :25 subversion-1.6.1

Extension:
1. Display the colored directory listing
    Open /etc/bashrc, add the following line:
    alias ls="ls --color" The
    next time you start bash, you can display the colored directory listing like in Slackware, where the meaning of the color As follows:
    1. blue --> directory
    2. green --> executable file
    3. red --> compressed file
    4. light blue --> link file
    5. gray --> other files

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326262909&siteId=291194637