{:check ["true"]}

Index

The ls command

  • Looking around in the filesystem can be done by the ls command.

    $ ls
    assignments  README.md
    
  • The command comes with many options.

    split=8
    $ ls -F
    assignments/  README.md
    

    Command line options are very typical for UNIX commands. The -F option instructs ls to show directories with the / indicator.

  • Command line options can be combined.

    split=8
    \$ ls -a -F
    ./  ../  assignments/  README.md
    
    \$ ls -aF
    ./  ../  assignments/  README.md
    

    The -a option shows all files, including hidden files (which have names starting with .).

    Multiple options can be provided to the command.

  • We can show specific files.

    split=8
    \$ ls
    K.png  main  main.c  main.o  Makefile
    

    Here is the list of files in some directory.

    split=8
    \$ ls main*
    main  main.c  main.o
    

    We can use wildcards to selectively show certain files.

    split=8
    $ ls assignments/assignment_1 assignments/assignment_2
    assignments/assignment_1:
    K.png
    
    assignments/assignment_2:
    main  main.c  main.o  Makefile
    

    We can also list the contents of one or more specific directories.

  • Each file has many metadata. We can use the -l to indicate we want the long format.

    split=8
    $ ls -l
    total 32
    -rwxrwxr-x 1 kenpu kenpu 16688 Aug 13 13:28 main
    -rw-rw-r-- 1 kenpu kenpu    64 Aug 13 13:12 main.c
    -rw-rw-r-- 1 kenpu kenpu  1672 Aug 13 13:28 main.o
    -rw-rw-r-- 1 kenpu kenpu    66 Aug 13 13:13 Makefile
    
    • total 32 shows that there are 32 blocks consumed.
    • -rw-rw-r-- is the read-write-execute permissions of the file with respect to the user, the group, and everyone.
    • kenpu kenpu is the owner and the owner group.
    • 16688 is the file size in bytes.
    • Aug 13 13:28 is the last modification time.
  • We can show the entire subtree of a directory using the recursive option.

    $ ls -R
    .:
    assignments  README.md  reports
    
    ./assignments:
    assignment_1  assignment_2
    
    ./assignments/assignment_1:
    K.png
    
    ./assignments/assignment_2:
    main  main.c  main.o  Makefile
    
    ./reports:
    
  • Other useful options:

    1. -1: display in single column.
    2. -h: display in more human friendly format.
    3. -S: sort files from largest to smallest.
    4. ...

More on ls

You can refer to the documentation:

box

https://man7.org/linux/man-pages/man1/ls.1.html