MTI TEK
  • Home
  • About
  • LLMs
  • Docker
  • Kubernetes
  • Java
  • All Resources
Linux-Ubuntu | ls -- list directory contents
  1. Notes
  2. Examples
  3. Command Help (man ls)

  1. Notes
    Usage:
    ls [options] [file ...]
    
    • For files, ls displays the name and information of each file.

    • For directories, ls displays the names and information of files/directories contained within each directory.

    • If no file/directory is given, the contents of the current directory is displayed.

    • If more than one file/directory is given, files are displayed first, then directories in alphabetical order.
  2. Examples
    • Basic listing of current directory contents:
      $ ls
      Documents  Downloads  Music  Pictures  Videos
      
    • (Option -1) List the name of each file and directory in one line:
      $ ls -1 ~/folder
      folder1
      folder2
      file1.txt
      file2.txt
      
    • (Option -l) List files and directories in long format:
      $ ls -l ~/folder
      total 8
      drwxrwxr-x  2 mtitek mtitek 4096 Nov 27 12:51 folder1
      drwxrwxr-x  2 mtitek mtitek 4096 Nov 27 12:51 folder2
      -rw-rw-r--  1 mtitek mtitek   15 Nov 27 12:52 file1.txt
      -rw-rw-r--  1 mtitek mtitek   23 Nov 27 12:52 file2.txt
      
    • (Option -a) List all files including hidden files (starting with "."):
      $ ls -la ~/folder
      total 16
      drwxrwxr-x  4 mtitek mtitek 4096 Nov 27 12:51 .
      drwxr-xr-x 26 mtitek mtitek 4096 Nov 27 12:51 ..
      -rw-rw-r--  1 mtitek mtitek   12 Nov 27 12:50 .hidden_file
      drwxrwxr-x  2 mtitek mtitek 4096 Nov 27 12:51 folder1
      drwxrwxr-x  2 mtitek mtitek 4096 Nov 27 12:51 folder2
      -rw-rw-r--  1 mtitek mtitek   15 Nov 27 12:52 file1.txt
      
      Note: for a directory, the number of bytes (for example: 4096) represents the size of the meta file that contains information about the directory. It doesn't give any information about the real size of files and directories within that directory.

    • (Option -h) Display file sizes in human-readable format:
      $ ls -lh ~/folder
      total 8.0K
      drwxrwxr-x  2 mtitek mtitek 4.0K Nov 27 12:51 folder1
      drwxrwxr-x  2 mtitek mtitek 4.0K Nov 27 12:51 folder2
      -rw-rw-r--  1 mtitek mtitek  1.2K Nov 27 12:52 file1.txt
      -rw-rw-r--  1 mtitek mtitek  2.3K Nov 27 12:52 file2.txt
      
    • List files and sub-directories information with their contents (including ".", "..", and files and directories starting with "."):
      $ ls -al ~/folder/*
      /home/mtitek/folder/folder1:
      total 8
      drwxrwxr-x  2 mtitek mtitek 4096 Nov 27 12:51 .
      drwxrwxr-x  4 mtitek mtitek 4096 Nov 27 12:51 ..
      
      /home/mtitek/folder/folder2:
      total 8
      drwxrwxr-x  2 mtitek mtitek 4096 Nov 27 12:51 .
      drwxrwxr-x  4 mtitek mtitek 4096 Nov 27 12:51 ..
      
    • (Option -d) List files and sub-directories information without their contents (excluding ".", "..", and files and directories starting with "."):
      $ ls -ld ~/folder/*
      drwxrwxr-x  2 mtitek mtitek 4096 Nov 27 12:51 /home/mtitek/folder/folder1
      drwxrwxr-x  2 mtitek mtitek 4096 Nov 27 12:51 /home/mtitek/folder/folder2
      -rw-rw-r--  1 mtitek mtitek   15 Nov 27 12:52 /home/mtitek/folder/file1.txt
      
    • (Option -S) Sort files and directories by size (largest first):
      $ ls -lS ~/folder
      total 8
      drwxrwxr-x  2 mtitek mtitek 4096 Nov 27 12:51 folder1
      drwxrwxr-x  2 mtitek mtitek 4096 Nov 27 12:51 folder2
      -rw-rw-r--  1 mtitek mtitek   23 Nov 27 12:52 file2.txt
      -rw-rw-r--  1 mtitek mtitek   15 Nov 27 12:52 file1.txt
      
    • (Option -t) Sort files and directories by time modified (most recent first):
      $ ls -lt ~/folder
      total 8
      -rw-rw-r--  1 mtitek mtitek   23 Nov 27 12:52 file2.txt
      -rw-rw-r--  1 mtitek mtitek   15 Nov 27 12:52 file1.txt
      drwxrwxr-x  2 mtitek mtitek 4096 Nov 27 12:51 folder1
      drwxrwxr-x  2 mtitek mtitek 4096 Nov 27 12:51 folder2
      
    • (Option -R) Recursively list subdirectories:
      $ ls -R ~/folder
      /home/mtitek/folder:
      folder1  folder2  file1.txt  file2.txt
      
      /home/mtitek/folder/folder1:
      subfolder1  subfile1.txt
      
      /home/mtitek/folder/folder1/subfolder1:
      deep_file.txt
      
    • (Option -F) Display file type indicators:
      $ ls -F ~/folder
      folder1/  folder2/  file1.txt  script.sh*  symlink@
      
    • (Option -i) Display inode numbers:
      $ ls -i ~/folder
      1234567 folder1   1234568 folder2   1234569 file1.txt   1234570 file2.txt
      
  3. Command Help (man ls)
    The following options can be used:
    -1
    |Force output to be one entry per line.
    
    -R
    |Recursively list subdirectories encountered.
    
    -l
    |List files/directories in long format.
    
    -a
    |List all files/directories including "." and ".."
    
    -A
    |List all files/directories except for "." and ".."
    
    -h
    |When used with the -l option, use unit suffixes:
    |Byte, Kilobyte, Megabyte, Gigabyte, Terabyte and Petabyte in order to reduce the number of digits to three or less using base 2 for sizes.
    
    -e
    |Print the Access Control List (ACL) associated with the file, if present, in long (-l) output.
    
    -S
    |Sort files by size (largest first).
    
    -t
    |Sort by time modified (most recently modified first) before sorting the operands by lexicographical order.
    
    -u
    |Use time of last access, instead of last modification of the file for sorting (-t) or long printing (-l).
    
    -U
    |Use time of file creation, instead of last modification for sorting (-t) or long output (-l).
    
    -r
    |Reverse the order of the sort to get reverse lexicographical order or the oldest entries first (or largest files last, if combined with sort by size).
    
    -T
    |When used with the -l option, display complete time information for the file, including month, day, hour, minute, second.
    
    -F
    |Display a slash ('/') immediately after each pathname that is a directory,
    |an asterisk ('*') after each file that is executable,
    |an at sign ('@') after each symbolic link,
    |an equals sign ('=') after each socket,
    |a percent sign ('%') after each whiteout,
    |and a vertical bar ('|') after each file that is a FIFO.
    
    -i
    |For each file, print the file's file serial number (inode number).
    
    -n
    |Display user and group IDs numerically, rather than converting to a user or group name in a long (-l) output.
    |This option turns on the -l option.
    
    -O
    |Include the file flags in a long (-l) output.
    
    -G
    |Enable colorized output.
    
    --color=WHEN
    |Colorize the output; WHEN can be 'always', 'auto', or 'never'.
    
    --group-directories-first
    |Group directories before files.
    
    --time-style=STYLE
    |Show times using style STYLE: full-iso, long-iso, iso, locale, or +FORMAT.
    
    Option "-l" (long display):
    The "-l" provides more information about files and directories.
    The first line of the output displays the total number of blocks contained within the directory.
    The following lines list details of each file and directory.
    Each line provides the following information:
    - The type of the file.
    - Permissions of the file.
    - The number of file hard links.
    - The user name of the owner of the file.
    - The group name of the primary group of the file.
    - The byte size of the file.
    - The last time the file was modified.
    - The file name.

    • The option "-l" shows the following characters for the type of the file:
      -: Regular file.
      d: Directory.
      l: Symbolic link (linked file).
      b: Block special file (block device).
      c: Character special file (character device).
      s: Socket link.
      p: FIFO (named pipe).
      
    • The option "-l" shows the following characters for the permissions of the file:
      r: the file is readable (or you can open the directory).
      w: the file is writable (or you can add/delete files or directories to/from the directory).
      x: the file is executable (or the directory is searchable).
      -: the file is | not readable | not writable | not executable.
      
      For a specific command file, you might see the s character instead of the x character (-rwsrwsr--). The s character can be set for the user permissions (setuid: set user id on execution) or/and group permissions (setgid: set group id on execution). This allows a user to execute the command but the ownership of the running command will be held by the user or group owning the command.

      For some directories, you might see the t character instead of the x character in other's permissions (drwxrwxr-t). This indicates that the sticky bit was enabled for them, which means that users can add or delete files in those directories but they cannot delete each other's files.

      For some files and directories, you might see the + character at the end of the permissions bits (-rwsrwsr--+). This means that those files and directories have extra attributes assigned to them (i.e. ACLs).
© 2025 mtitek