ls

Show directory contents list

Supplementary instructions

ls command is the abbreviation of list, which is used to display the target list. It is a frequently used command in Linux. The output information of the ls command can be displayed with color highlighting to partition different types of files.

grammar

ls [options] [filename...]
    [-1abcdfgiklmnopqrstuxABCDFGLNQRSUX] [-w cols] [-T cols] [-I pattern] [--full-time]
    [--format={long,verbose,commas,across,vertical,single-col‐umn}]
    [--sort={none,time,size,extension}] [--time={atime,access,use,ctime,status}]
    [--color[={none,auto,always}]] [--help] [--version] [--]

Options

-C #Multiple column output, vertical sorting.
-F # Add "/" suffix to each directory name, add "|" suffix to each FIFO name, and add "*" suffix to each runnable name.
-R # Recursively list encountered subdirectories.
-a # List all files, including implicit files starting with ".".
-c # Use "state change time" instead of "file modification time" to sort (when using the "-t" option) or list (when using the "-l" option).
-d # List directory names like other files instead of listing their contents.
-i # Output the file serial number (i.e. i-node number: i-node number) before outputting the file. -l list (in single column format) file modes
        # (file mode), the number of links to the file, owner name, group name, file size (in bytes), time information, and file name.
        # By default, the time information displays the latest modification time; you can choose to display the other two time information with options "-c" and "-u". For device files,
        # The area that originally displayed the file size usually displayed the major and minor device numbers (major and minor device numbers).
-q # Output non-printing characters in file names as question marks. (This is the default for output to the terminal.)
-r # Sort in reverse order.
-t # Sort by time information.
-u # Use the most recent access time instead of the most recent modification time to sort (when using the "-t" option) or list (when using the "-l" option).
-1 # Single column output.
-1, --format=single-column # Output one file per line (single column output). If standard output is not to a terminal, this option is the default.
-a, --all # List all files in the directory, including files starting with ".".
-b, --escape # List non-exportable characters in the file name using backslashes and character numbers (just like in C language).
-c, --time=ctime, --time=status
       # Sort by file status change time (ctime in i node) and output in the directory
       # Allow. If long format output is used (option "-l"), use the file's status change
       # Change the time to replace the file modification time. [Translation Note: The so-called file status change (section i
       # The ctime mark in the dot) includes both file modification and changes in file attributes (such as owner, group, number of links, etc.)]
-d, --directory
       # List directory names like other files, rather than listing their contents.
-f # Do not sort directory contents; list them in the order they are stored on disk. At the same time, start the "-a" option, if there is "-l",
       # "--color" or "-s" disable them.
-g # Ignore, for UNIX compatibility.
-i, --inode
       # Print the i node number (also called file serial number and index num) on the left side of each file: file serial number and index num‐
       #ber). The i-node number is unique within each specific file system.
-k, --kilobytes
       # If file sizes are listed, they are in kilobytes KB.
-l, --format=long, --format=verbose
       #The output information from left to right includes file name, file type, permissions, number of hard links, owner name, group name, size (byte)
       # , and time information (if no other time is specified, it refers to the modification time). For files older than 6 months or beyond the future
       # For 1-hour files, the hours and minutes in the time information will be replaced by the era.
       #Before each directory is listed, there is a line of "Total Block Count" that displays the disk space occupied by all files in the directory. The block default is 1024 bytes;
       # If the POSIXLY_CORRECT environment variable is set, the default block size is 512 bytes unless the "-k" option is used.
       # Each hard link counts towards the total number of blocks (and therefore may be counted twice), which is definitely a disadvantage.

# Listed permissions are similar to specifications for symbolic (file) modes. But ls
       # Combine multiple bits of information in the third character of each set of permissions, as follows: s If setuid is set
       # bit or setgid bit, and the corresponding executable bit is also set. S if setuid bit or setgid is set
       # bit, but the corresponding executable bit is not set. t If the sticky bit is set, and the corresponding executable bit is also set. T
       # If the sticky bit is set, but the corresponding executable bit is not set. x
       # If only the executable bit is set instead of the above four situations. - Other cases (i.e. the executable bit is not set).
-m, --format=commas
       # List files horizontally, with as many lines per line as possible, separated by commas and a space.
-n, --numeric-uid-gid
       # List numeric UIDs and GIDs instead of usernames and groupnames.
-o # List directory contents in long format, but do not display group information. Equivalent to using " --format=long --no-group
       # ” option. This option is provided for compatibility with other versions of ls.
-p # Append a character after each file name to indicate the type of file. Similar to "-F" option but does not mark executable files.
-q, --hide-control-chars
       # Use question marks to replace non-printing characters in file names. This is the default option.
-r, --reverse
       # Arrange directory contents in reverse order.
-s, --size
       # Output to the left of each file name the size of that file, in 1024-byte blocks. If POSIXLY_CORRECT is set
       # environment variable, unless the " -k " option is used, the block size is 512 bytes.
-t, --sort=time
       # Sort by the file's latest modification time (mtime in the i node) rather than by file name dictionary, with new files first.
-u, --time=atime, --time=access, --time=use
       # Similar to option "-t", but uses the file's most recent access time (atime in the i node) instead of file modification
       # Time change. If long format listing is used, the time printed is the most recent access time.
-w, --width cols
        # Assume screen width is cols (cols replaced with actual numbers) columns. If this option is not used, the default value is this
        # Obtained in this way: try to get it from the terminal driver first if possible, otherwise try to get it from the environment variable COLUMNS (if set
        # If set), if it doesn't work, take 80.

-x, --format=across, --format=horizontal
        #Multi-column output, sorted horizontally.

-A, --almost-all
        # Display all files except "." and "..".

-B, --ignore-backups
        # Do not output backup files ending with "~" unless given on the command line.

-C, --format=vertical
        #Multi-column output, sorted vertically. This is the default when standard output is a terminal. When using the command names dir and d, it is always the default.

-D, --dired
        # When outputting in long format ("-l" option), print an additional line after the main output: //DIRED// BEG1 END1 BEG2
        #END2...

# BEGn and ENDn are unsigned integers, recording the starting and ending positions of each file name in the output (
# byte offset). This makes it easy for Emacs to find filenames even if they contain irregularities such as spaces or newlines.
# No special search is required for normal characters.
#
# If directories are listed recursively ("-R" option), list each subdirectory with a line similar to:
        # //SUBDIRED// BEG1 END1 ... [Annotation: I tested TurboLinux4.0 and RedHat6.1 and found that they are both in "
        # //DIRED// BEG1... " is listed after " //SUBDIRED// BEG1... ", that is, there is only one
        # instead of after each subdirectory. And "//SUBDIRED//BEG1..." lists the offsets of each subdirectory name. 】

-F, --classify, --file-type
        # Append a character after each file name to indicate the type of file. "*" represents an ordinary executable file; "/" represents a directory; "
        # @ ” represents a symbolic link; “ | ” represents FIFOs; “ = ” represents sockets; nothing represents an ordinary file.

-G, --no-group
        # Do not display group information when listing directories in long format.

-I, --ignorepattern
        # Unless given on the command line, do not list matching shell file name patterns (pattern, not the general
        # expression) file. In the shell, file names starting with "." do not match the file name pattern (pattern)
        Matches wildcard characters starting with #.

-L, --dereference
        # List information about the file pointed to by the symbolic link, not the symbolic link itself.

-N, --literal
        # Do not use quotes around filenames.

-Q, --quote-name
        # Enclose the file name in double quotes, and non-printing characters are represented in C language.

-R, --recursive
        # List the contents of all directories recursively.

-S, --sort=size
        # Sort directory contents by file size instead of lexicographically, with large files first.

-T, --tabsize cols
        # Assume each tab width is cols . The default is 8. For efficiency, ls may use tab characters in the output. If cols is
        0, tab characters are not used.

-U, --sort=none
        # Directory contents are not sorted; listed in the order they are stored on disk. (Options "-U" and "-f" are not
        # The former does not enable or disable related options. ) This is especially useful when listing very large directories because there is no sorting
        # Can significantly speed up.

-X, --sort=extension
        # Sort lexicographically by file extension (consisting of characters after the last "."). Those without extensions are listed first.

--color[=when]
        # Specify whether to use color to distinguish file categories. The environment variable LS_COLORS specifies the colors used. How to set this variable see dir‐
        # colors(1) . when can be omitted or one of the following:
none # Do not use color, this is the default.
        # auto Use only when standard output is a terminal. always always uses color. Specifying --color and omitting when is equivalent to
        # --color=always .

--full-time
        # List full times instead of using standard abbreviations. The format is as in the default format of date(1); this format
        # cannot be changed, but you can use cut(1) to extract the date string and send the result to the command "date -d".

# It is useful to include seconds in the output time. (Unix file systems store file time information accurate to the second.
        # So this option already gives all the information the system knows. ) For example, when you have a Makefile
        # This option helps when the file cannot be generated properly.

Parameters

Directory: Specify the directory to display the list, or it can be a specific file.

Example

$ ls # Only list visible files in the current directory
$ ls -l # List visible file details in the current directory
$ ls -hl # List details and display file size in readable size
$ ls -al # List detailed information of all files (including hidden)
$ ls --human-readable --size -1 -S --classify # Sort by file size
$ du -sh * | sort -h # Sort by file size (same as above)

Display a list of all files in the current directory including hidden files

[root@localhost ~]# ls -a
. anaconda-ks.cfg .bash_logout .bashrc install.log .mysql_history satools .tcshrc .vimrc
.. .bash_history .bash_profile .cshrc install.log.syslog .rnd .ssh .viminfo

Output long format list

[root@localhost ~]# ls -1

anaconda-ks.cfg
install.log
install.log.syslog
satools

Display the inode information of the file

Index node (index inode, referred to as "inode") is a special concept in Linux. Two texts with the same index node number are essentially the same file (except for different file names).

[root@localhost ~]# ls -i -l anaconda-ks.cfg install.log
2345481 -rw------- 1 root root 859 Jun 11 22:49 anaconda-ks.cfg
2345474 -rw-r--r-- 1 root root 13837 Jun 11 22:49 install.log

Horizontal output file list

[root@localhost /]# ls -m

bin, boot, data, dev, etc, home, lib, lost+found, media, misc, mnt, opt, proc, root, sbin, selinux, srv, sys, tmp, usr, var

Modify the last edited file

The most recently modified files are shown at the top.

[root@localhost /]# ls -t

tmp root etc dev lib boot sys proc data home bin sbin usr var lost+found media mnt opt selinux srv misc

Show recursive files

[root@localhost ~]# ls -R
.:
anaconda-ks.cfg install.log install.log.syslog satools

./satools:
black.txt freemem.sh iptables.sh lnmp.sh mysql php502_check.sh ssh_safe.sh

Print the UID and GID of a file

[root@localhost /]# ls -n

total 254
drwxr-xr-x 2 0 0 4096 Jun 12 04:03 bin
drwxr-xr-x 4 0 0 1024 Jun 15 14:45 boot
drwxr-xr-x 6 0 0 4096 Jun 12 10:26 data
drwxr-xr-x 10 0 0 3520 Sep 26 15:38 dev
drwxr-xr-x 75 0 0 4096 Oct 16 04:02 etc
drwxr-xr-x 4 0 0 4096 Jun 12 10:26 home
drwxr-xr-x 14 0 0 12288 Jun 16 04:02 lib
drwx------ 2 0 0 16384 Jun 11 22:46 lost+found
drwxr-xr-x 2 0 0 4096 May 11 2011 media
drwxr-xr-x 2 0 0 4096 Nov 8 2010 misc
drwxr-xr-x 2 0 0 4096 May 11 2011 mnt
drwxr-xr-x 2 0 0 4096 May 11 2011 opt
dr-xr-xr-x 232 0 0 0 Jun 15 11:04 proc
drwxr-x--- 4 0 0 4096 Oct 15 14:43 root
drwxr-xr-x 2 0 0 12288 Jun 12 04:03 sbin
drwxr-xr-x 2 0 0 4096 May 11 2011 selinux
drwxr-xr-x 2 0 0 4096 May 11 2011 srv
drwxr-xr-x 11 0 0 0 Jun 15 11:04 sys
drwxrwxrwt 3 0 0 98304 Oct 16 08:45 tmp
drwxr-xr-x 13 0 0 4096 Jun 11 23:38 usr
drwxr-xr-x 19 0 0 4096 Jun 11 23:38 var

List file and folder details

[root@localhost /]# ls -l

total 254
drwxr-xr-x 2 root root 4096 Jun 12 04:03 bin
drwxr-xr-x 4 root root 1024 Jun 15 14:45 boot
drwxr-xr-x 6 root root 4096 Jun 12 10:26 data
drwxr-xr-x 10 root root 3520 Sep 26 15:38 dev
drwxr-xr-x 75 root root 4096 Oct 16 04:02 etc
drwxr-xr-x 4 root root 4096 Jun 12 10:26 home
drwxr-xr-x 14 root root 12288 Jun 16 04:02 lib
drwx------ 2 root root 16384 Jun 11 22:46 lost+found
drwxr-xr-x 2 root root 4096 May 11 2011 media
drwxr-xr-x 2 root root 4096 Nov 8 2010 misc
drwxr-xr-x 2 root root 4096 May 11 2011 mnt
drwxr-xr-x 2 root root 4096 May 11 2011 opt
dr-xr-xr-x 232 root root 0 Jun 15 11:04 proc
drwxr-x--- 4 root root 4096 Oct 15 14:43 root
drwxr-xr-x 2 root root 12288 Jun 12 04:03 sbin
drwxr-xr-x 2 root root 4096 May 11 2011 selinux
drwxr-xr-x 2 root root 4096 May 11 2011 srv
drwxr-xr-x 11 root root 0 Jun 15 11:04 sys
drwxrwxrwt 3 root root 98304 Oct 16 08:48 tmp
drwxr-xr-x 13 root root 4096 Jun 11 23:38 usr
drwxr-xr-x 19 root root 4096 Jun 11 23:38 var

List readable file and folder details

[root@localhost /]# ls -lh

total 254K
drwxr-xr-x   2 root root 4.0K Jun 12 04:03 bin
drwxr-xr-x   4 root root 1.0K Jun 15 14:45 boot
drwxr-xr-x   6 root root 4.0K Jun 12 10:26 data
drwxr-xr-x  10 root root 3.5K Sep 26 15:38 dev
drwxr-xr-x  75 root root 4.0K Oct 16 04:02 etc
drwxr-xr-x   4 root root 4.0K Jun 12 10:26 home
drwxr-xr-x  14 root root  12K Jun 16 04:02 lib
drwx------   2 root root  16K Jun 11 22:46 lost+found
drwxr-xr-x   2 root root 4.0K May 11  2011 media
drwxr-xr-x   2 root root 4.0K Nov  8  2010 misc
drwxr-xr-x   2 root root 4.0K May 11  2011 mnt
drwxr-xr-x   2 root root 4.0K May 11  2011 opt
dr-xr-xr-x 235 root root    0 Jun 15 11:04 proc
drwxr-x---   4 root root 4.0K Oct 15 14:43 root
drwxr-xr-x   2 root root  12K Jun 12 04:03 sbin
drwxr-xr-x   2 root root 4.0K May 11  2011 selinux
drwxr-xr-x   2 root root 4.0K May 11  2011 srv
drwxr-xr-x  11 root root    0 Jun 15 11:04 sys
drwxrwxrwt   3 root root  96K Oct 16 08:49 tmp
drwxr-xr-x  13 root root 4.0K Jun 11 23:38 usr
drwxr-xr-x  19 root root 4.0K Jun 11 23:38 var

Show folder information

[root@localhost /]# ls -ld /etc/

drwxr-xr-x 75 root root 4096 Oct 16 04:02 /etc/

List file and folder details by time

[root@localhost /]# ls -lt

total 254
drwxrwxrwt 3 root root 98304 Oct 16 08:53 tmp
drwxr-xr-x 75 root root 4096 Oct 16 04:02 etc
drwxr-x--- 4 root root 4096 Oct 15 14:43 root
drwxr-xr-x 10 root root 3520 Sep 26 15:38 dev
drwxr-xr-x 14 root root 12288 Jun 16 04:02 lib
drwxr-xr-x 4 root root 1024 Jun 15 14:45 boot
drwxr-xr-x 11 root root 0 Jun 15 11:04 sys
dr-xr-xr-x 232 root root 0 Jun 15 11:04 proc
drwxr-xr-x 6 root root 4096 Jun 12 10:26 data
drwxr-xr-x 4 root root 4096 Jun 12 10:26 home
drwxr-xr-x 2 root root 4096 Jun 12 04:03 bin
drwxr-xr-x 2 root root 12288 Jun 12 04:03 sbin
drwxr-xr-x 13 root root 4096 Jun 11 23:38 usr
drwxr-xr-x 19 root root 4096 Jun 11 23:38 var
drwx------ 2 root root 16384 Jun 11 22:46 lost+found
drwxr-xr-x 2 root root 4096 May 11 2011 media
drwxr-xr-x 2 root root 4096 May 11 2011 mnt
drwxr-xr-x 2 root root 4096 May 11 2011 opt
drwxr-xr-x 2 root root 4096 May 11 2011 selinux
drwxr-xr-x 2 root root 4096 May 11 2011 srv
drwxr-xr-x 2 root root 4096 Nov 8 2010 misc

List file and folder details by modification time

[root@localhost /]# ls -ltr

total 254
drwxr-xr-x 2 root root 4096 Nov 8 2010 misc
drwxr-xr-x 2 root root 4096 May 11 2011 srv
drwxr-xr-x 2 root root 4096 May 11 2011 selinux
drwxr-xr-x 2 root root 4096 May 11 2011 opt
drwxr-xr-x 2 root root 4096 May 11 2011 mnt
drwxr-xr-x 2 root root 4096 May 11 2011 media
drwx------ 2 root root 16384 Jun 11 22:46 lost+found
drwxr-xr-x 19 root root 4096 Jun 11 23:38 var
drwxr-xr-x 13 root root 4096 Jun 11 23:38 usr
drwxr-xr-x 2 root root 12288 Jun 12 04:03 sbin
drwxr-xr-x 2 root root 4096 Jun 12 04:03 bin
drwxr-xr-x 4 root root 4096 Jun 12 10:26 home
drwxr-xr-x 6 root root 4096 Jun 12 10:26 data
dr-xr-xr-x 232 root root 0 Jun 15 11:04 proc
drwxr-xr-x 11 root root 0 Jun 15 11:04 sys
drwxr-xr-x 4 root root 1024 Jun 15 14:45 boot
drwxr-xr-x 14 root root 12288 Jun 16 04:02 lib
drwxr-xr-x 10 root root 3520 Sep 26 15:38 dev
drwxr-x--- 4 root root 4096 Oct 15 14:43 root
drwxr-xr-x 75 root root 4096 Oct 16 04:02 etc
drwxrwxrwt 3 root root 98304 Oct 16 08:54 tmp

Sort files by special characters

[root@localhost nginx-1.2.1]# ls -F

auto/ CHANGES CHANGES.ru conf/ configure* contrib/ html/ LICENSE Makefile man/ objs/ README src/

List files and mark color categories

[root@localhost nginx-1.2.1]# ls --color=auto

auto CHANGES CHANGES.ru conf configure contrib html LICENSE Makefile man objs README src

Expand knowledge

File types represented by different colors