Linux command-ls command

The ls command is the most commonly used command under 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, the list of files and folders in the specified directory will be displayed. Through the ls command, you can not only view the files contained in the linux folder, but also view file permissions (including directories, folders, 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 hidden files starting with.

-A Same as -a, but does not list "." (For the current directory) and ".." (for the parent directory of the current directory).

-c coordination -lt: sort and display ctime according to ctime (time when the file status was last changed) -l: display ctime but sort by name otherwise: sort by ctime

-C List items from top to bottom in each column

–Color [= WHEN] Controls whether to use color discrimination files. WHEN can be one of 'never', 'always' or 'auto'

-d, –directory Display the directory as a file, not the files under it.

-D, --dired produces results suitable for dired mode of Emacs

-f does not sort the output files, -aU option takes effect, -lst option becomes invalid

-g is like -l, but does not list the owner

-G, --no-group do not list any information about the group

-h, --human-readable lists file sizes in a format that is easy to understand (e.g. 1K 234M 2G)

--Si is like -h, but the file size is raised to the power of 1000 instead of 1024

-H, --dereference-command-line use the real destination indicated by the symbolic link in the command line

–Indicator-style = mode Specify the indicator <mode> after each project name: none (default), classify (-F), file-type (-p)

-i, --inode print out the inode number of each file

-I, --ignore = style does not print any items that match the shell wildcard <style>

-k is -block-size = 1K, which means the size of the file in k bytes.

-l In addition to the file name, list the file permissions, owner, file size and other information in detail.

-L, --dereference When displaying the file information of the 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 the detailed information of the file except the group information.   

-r, --reverse in reverse order

-R, --recursive list all subdirectory levels at the same time

-s, --size List all file sizes in block size units

-S sort by file size

–Sort = WORD The following are the optional 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 sort by file modification time

-u cooperation -lt: display access time and sort by access time

With -l: display access time but sort 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, --width = COLS Specify the screen width without using the current value

-x List items line by line instead of column by column

-X sort by extension

-1 only one file per line

--Help show this help message and leave

--Version display 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 keyword of the command is first, followed by the command parameters, and there must be a short dash "-" before the command parameters, all command parameters are specific You can choose one or more parameters according to your needs. After the command parameters are the operation objects 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, namely "l" and "R", of course, you can also use them together, as follows:

Command: ls -lR / home / peidachang

This form is exactly the same as the result of the above command form. In addition, if the operation object of the command is 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 under the home folder. I can directly input ls -lR peidachang, or I can use ls -lR / home / peidachang. 

Example 2: List the detailed contents 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: List only the subdirectories under the file

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

List subdirectories under / 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 details of the subdirectories under 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 whose name starts with s in the current working directory. The newer the files after the newer, you can use the following command:

Command: ls -ltr s *

Output :

[root@localhost opt]# ls -ltr s*

src:

Total 0

 

script:

Total 0

 

soft:

Total 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 under the current working directory; add "/" after the name to the directory, and add "*" after the name to the executable file 

Command : ls -AF

Output :

[root@localhost opt]# ls -AF

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

Example 6: Calculate the number of files and directories under 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

命令: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 recursive directory

命令: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 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 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 color catalog list

    Open / etc / bashrc and add the following line:

    alias ls="ls --color"

    The next time you start bash, you can display a colored directory list like in Slackware. The meaning of the colors is 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 www.cnblogs.com/gezp/p/12713391.html