Ls

From Unix SME

ls


Description:

  Lists directory's contents. By default, the command does not list hidden files. Use the flag -a in order to include hidden files and elements.

Syntax:

  ls [options] [directory]

Use Case:

  The ls command is used to list the contents of both files or directories of specified regions

Options:

  -l
  • Known as a long format that displays detailed information about files and directories
  -a
  • Represent all files include hidden files and directories in the listing
  -t 
  • Sort files and directories by their last modification time, displaying the most recently modified ones first
  -r
  • Known as reverse order which is used to reverse the default order of listing
  -S 
  • Sort files and directories by their sizes, listing the largest ones first
  -R
  • List files and directories recursively, including subdirectories.
  -i
  • Known as inode which displays the index number (inode) of each file and directory.
  -g
  • Known as group which displays ownership of files and directories instead of the owner.
  -h
  • Print file sizes in human-readable format (eg., 1K, 234M, 2G).
  -d
  • List directories themselves, rather than their contents.

Examples:


ls -a (all)

output: . .. .test_txt test.txt

explanation: lists all files including hidden ones in the current working directory


ls -l (long listing format)

output:

total 4

-rw-r--r-- 1 test test 4 Jan 29 19:34 test.txt

explanation: lists the permissions (read, write, execute), creator of the file, size of the file(s) in bytes if there is no unit, the group that created the file, timestamp of creation, and the file name


ls -H (command line) [directory]


explanation: lists the number of hard links for the current or listed directory


ls -t (time) [directory]

explanation: lists the time of creation for the files in the current directory


ls -m [directory]

explanation: lists the last time a file was modified or files in the current working directory


ls -A (almost all) [directory]

explanation: lists everything but the directories: . and ..


ls -d [directory]

explanation: lists the directories in the current directory


ls -U (do not sort) [directory]

explanation: lists the directories in directory order.


ls --zero

explanation: will not print a new line after listing but will output with NUL


ls -1 [directory]

explanation: will list one file per line and will create a new line after output of one file