Linux common commands: ls command

  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 file permissions (including directories, folders, and file permissions), view directory information, and more. 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 does not list "." (representing the current directory) and ".." (representing the parent directory of the current directory).

-c with -lt: sort by ctime and display ctime (the time the file status was last changed) with -l: display 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 color-resolved files. WHEN can be one of 'never', 'always' or 'auto'

-d, –directory

       Display the directory like a file, rather than the files under it.

-D, --dired produce results suitable for use in Emacs' dired mode

-f does not sort the output files, the -aU option is valid, and the -lst option is invalid

-g is like -l, but does 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 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 symlink in the command line

--indicator-style=style specifies an 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 does not print any items matching the shell wildcard <style>

-k means --block-size=1K, which indicates 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 symlink file information, display the object indicated by the symlink instead of the symlink itself

-m all items are comma separated and fill the entire line width

-o Similar to -l, displays detailed information about the file in addition to group information.   

-r, --reverse reverse order

-R, --recursive list all subdirectory levels simultaneously

-s, --size list the size of all files in block size

-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 sort by name

else: sort by access time

-U do not sort; list items in their original filesystem order

-v sort by version

-w, --width=COLS specify screen width without using current value

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

-X sort by extension

-1 list 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 first is the keyword of the command, followed by the command parameters, there should be a dash "-" before the command parameters, all command parameters have specific 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 follows:

Command: ls -lR /home/peidachang

  This form is exactly the same as 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 -l t*   

  You can view information about all files in the current directory whose file names begin with "t". 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 subdirectories under /opt/soft files

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 details of subdirectories under /opt/soft files

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 the files whose names begin with s in the current working directory. The newer the later, the later, 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 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

Order:

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

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

Example 7: List absolute paths of files 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 recursion to the 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

Order:

 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 directory listing

    Open /etc/bashrc and add the following line:

    alias ls="ls --color"

    The next time you start bash, you can display a colored directory listing like in Slackware, where the colors mean the following:

    1. Blue --> Catalog

    2. Green --> Executable

    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=325074915&siteId=291194637