Listing Files in Linux

0

The ls command is used to list the contents of a directory. You've already seen it used a few times before in examples, but this page will help ensure you are comfortable with its use.

ls [OPTIONS] [FILE]


By default, when the ls command is used with no options or arguments, it will list the files in the current directory:


To learn the details about a file, such as the type of file, the permissions, ownerships or the timestamp, perform a long listing using the -l option to the ls command. Below, a listing of the /var/log directory is used as an example, since it provides a variety of output:


Each line corresponds to a file contained within the directory. The information can be broken down into fields separated by spaces. The fields are as follows:

  1. File Type

    -rw-r--r--  1 root      root  29746 Apr 21      03:33 alternatives.log.1

    drwxr-x---  2 root      adm   4096  May 15      06:26 apache2

    The first field actually contains ten characters, where the first character indicates the type of file and the next nine specify permissions. The file types are:

    SymbolFile TypeDescription
    ddirectoryA file used to store other files.
    -regular fileIncludes readable files, images files, binary files, and compressed files.
    lsymbolic linkPoints to another file.
    ssocketAllows for communication between processes.
    ppipeAllows for communication between processes.
    bblock fileUsed to communicate with hardware.
    ccharacter fileUsed to communicate with hardware.

    The first file alternatives.log is a regular file -, while the second file apache2 is a directory d.

    Permissions

    drwxr-xr-x  2 root      root    4096 Mar 31 12:41 vmware

    Permissions indicate how certain users can access a file. Keep reading to learn more about permissions.

    Hard Link Count

    -rw-r-----  1 syslog    adm    1346 Mar 11 02:40 auth.log

    This number indicates how many hard links point to this file.

    User Owner

    -rw-r-----  1 syslog    adm    106 Mar 16 11:31 kern.log

    User syslog owns this file. Every time a file is created, the ownership is automatically assigned to the user who created it.

    Group Owner

    -rw-rw-r-- 1 root   utmp 292584 Oct  2 19:57 lastlog

    Indicates which group owns this file

    File Size

    -rw-r----- 1 syslog adm   19573 Oct  2 22:57 syslog

  2. Directories and larger files may be shown in kilobytes since displaying their size in bytes would present a very large number. Therefore, in the case of a directory, it might actually be a multiple of the block size used for the file system. Block size is the size of a series of data stored in the filesystem.

  3. Timestamp

    drwxr-xr-x 2 root   root   4096 Dec  7  2017 fsck

    This indicates the time that the file's contents were last modified.

    Filename

    -rw-r--r-- 1 root   root  47816 Dec  7  2017 bootstrap.log

  4. The final field contains the name of the file or directory.

    Consider This

    In the case of symbolic links, a file that points to another file, the link name will be displayed along with an arrow and the pathname of the original file.

    lrwxrwxrwx. 1 root root 22 Nov 6 2012 /etc/grub.conf -> ../boot/grub/ grub.conf


Sorting

By default the output of the ls command is sorted alphabetically by filename. It can sort by other methods as well.

Follow Along

The options in examples below will be combined with the -l option so the relevant details of the files are displayed. Notice fields corresponding to the search option.

The -t option will sort the files by timestamp:


The -S option will sort the files by file size:


The -r option will reverse the order of any type of sort. Notice the difference when it is added to the previous example:


The numbers in file size field switch from descending to ascending.

Used alone the -r option with list the files in reverse alphabetical order:



Post a Comment

0Comments
Post a Comment (0)