Search notes:

Shell command: ls

ls shows the contents of a directory and/or information about files in that directory.

Options

-a --all By default, ls does not list files whose name that start with a dot (such as for example .profile etc). This flag causes such files to be included.
-A --almost -all Like -a except that . (the current directory) and .. are not listed.
--author Used together with -l to print a file's author as well.
-b --escape Print non-graphic characters in file names with ocal escape numbers (\0…). Try for example with touch $'abc\x05def'; ls -b
--block-size =SIZE Used together with -l. SIZE is an integer followed by a unit. At least one of integer and the unit must be specified. Recognized units are powers of 1024: K, M, G, T, P, E, Z, Y or powers of 1000: KB, MB… . Binary prefixes are also recognized (KiB (=K), MiB (=M) etc.)
-B --ignore-backups Does not list files whose name end in a ~.
-C
--color [=WHEN] WHEN is one of always, auto or never
-d --directory Do not list the entries of a directory but the entry for the directory itself
-D --dired Create output suitable for Emacs' dired mode.
-F --classify Adds an indicator character to entry names: *, /, =, >, @ or |.
--file-type Similar to -F except that it does not add a *
--format =WORD WORD is one of across or horizontal (= -x), commas (= -m), long or verbose (= -l), single-column (= -1)
--full-time Same as -l --time-style=full-iso
-g Like -l but without listing entries' owners. Compare with -G
--group-directories-first
-G --no-group Used together with -l to suppress printing the entries' groups.
-h --human-readable With -l or -s: prints sizes human readable (for example 1.89G or 815M, same idea as also tree -h and numfmt
--si
-H --dereference-command-line-symlin-to-dir Follow symlinks
--hide =PATTERN Does not list entries that match PATTERN (For example: ls -l --hide=*.tmp does not list files with the extension .tmp)
--hyperlink [=WHEN]
--indicator-style =WORD
-i --inode Print files' inode numbers
-I --ignore =PATTERN Do not list files that match PATTERN
-k --kibibytes
-l Long listing. Possibly the most used option for ls. Same as ls --format=verbose
-L --dereference
-m Same as ls --format=commas
-n --numeric-uid-gid
-N --literal Prints entry-names without quoting; non printable characters as ?. Compare with -q
-o Like -l but without printing group information
-p --indicator-style =slash Append a / to directories
-q --hide-control-chars
--show-control-chars
-Q --quote-name
--quoting-style =WORD
-r --reverse Reverse sort order
-R --recursive List entries in subdirectories recusively
-s --size Prints allocated number of blocks rather than file sizes. Compare with -S (uppercase).
--sort =WORD WORD is none (equal to -U), size (-S), time (-t), version (-v), -extension (-X)
--time =WORD
--time-style =TIME_STYLE
-T --tabsize =COLS
-w --width =COLS
-x Same as ls --format=horizontal
-Z --context Print files' security contexts
-1 Emit one entry name per line. Same as ls --format=single-column
--help
--version Print version of ls. Compare with -v

Options controlling sorting

-c With -lt: sorts by and shows ctime. With -l: sorts by name and shows ctime.
-f Don't sort, enable -aU, disable -ls --color
-t Sort by time
-u With -lt: show and sort by access time, with -l: show access time and sort by name, otherwise: sort by access time
-v Sort according to version numbers detected in file names. Compare with --version.
-S Sorts by file size, largest to smallest. Compare with -s
-U Do not sort (entries are listed in directory order)
-X Sort by extension

Showing atime, ctime and mtime

-l shows a file's mtime, -lu a file's atime and -lc a file's ctime.
In the following example, a files atime and mtime are changed with the -t option of touch and then shown again with ls -l….
touch                    a-file
touch -a -t 201508282223 a-file
touch -m -t 200101010101 a-file

ls -l  a-file
#
#  -rw------- 1 rene rene 0 Jan  1  2001 a-file

ls -lu a-file
#
#  -rw------- 1 rene rene 0 Aug 28  2015 a-file

ls -lc a-file
#
#  -rw------- 1 rene rene 0 Jun 22 13:55 a-file
Github repository shell-commands, path: /ls/time

-I, --ignore

ls -I (or ls --ignore) does not list files that match the specified pattern.
The following command does not print *.h or *.c files in a directory:
$ ls -l -I '*.h' -I '*.c'
In order to list files , but not directories, that don't match *.h and *.c recursively, the following command might be helpful:
$ find . -type f ! -name "*.h" ! -name "*.c"

-i

-i prints the inode (index number) of the file.
This can be useful to rename or remove files with special characters.

-S - sort by size

With -S (uppercase s), the files are sorted by size.
The following command shows all mp3's in a directory with wide listing, the largest first:
ls -lS *mp3
Compare with ls -s which prints the files' sizes in blocks.

List files case insensitvely

With nocaseglob in effect, ls matches files case insensitvely. The following ls command lists all files that contain xyz, irrespective of the characters' case:
$ shopt -s nocaseglob
$ ls -l *xyz*
$ shopt -u nocaseglob

LS_COLORS

This is a script that can be sourced to assign some values to the environment variable LS_COLORS which is used to color the output of ls (with --colors=auto ?)
#
#  TODO:
#    The file should be created with
#       dircolors -p > $LS_COLORS_FILE
#    and then be read in ./profile with
#       eval "$(dircolors -b $LS_COLORS_FILE)"
#
#
. ~/.tq84-dotfile-sequence .ls_colors

#          fg   bg
# -------+----+----
# black  | 30 | 40  
# red    | 31 | 41
# green  | 32 | 42
# yellow | 33 | 43
# blue   | 34 | 44
# pink   | 35 | 45
# cyan   | 36 | 46
# white  | 37 | 47
#
# using 9x instead of 3x and 10x instead of 4x makes
# the color brighter
#
#  0: normal,
# 01: bold,
# 03: cursive
# 04: underlined
# 05: blinking
# 07: swap fg and bg color
# 08: hidden
# 09: strike through

  LS_COLORS=''
  LS_COLORS="${LS_COLORS}no=0:"               # normal - used for text other than file names and not assigned colors
  LS_COLORS="${LS_COLORS}fi=0:"               # file
  LS_COLORS="${LS_COLORS}rs=0:"
  LS_COLORS="${LS_COLORS}di=01;34:"           # directory
  LS_COLORS="${LS_COLORS}ln=01;36:"           # symbolic link - can be set to 'target' in which case the color of the file pointed to is chosen
  LS_COLORS="${LS_COLORS}mh=00:"
  LS_COLORS="${LS_COLORS}pi=40;33:"           # pipe
  LS_COLORS="${LS_COLORS}so=01;35:"           # socket
  LS_COLORS="${LS_COLORS}do=01;35:"           # door
  LS_COLORS="${LS_COLORS}bd=40;33;01:"        # block device
  LS_COLORS="${LS_COLORS}cd=40;33;01:"        # character device
  LS_COLORS="${LS_COLORS}or=40;31;01:"        # orphan - symbolic link that points to non-existing target
  LS_COLORS="${LS_COLORS}mi=00:"              # missing (symbolic link???)
  LS_COLORS="${LS_COLORS}su=37;41:"           # setuid
  LS_COLORS="${LS_COLORS}sg=30;43:"           # setgid
  LS_COLORS="${LS_COLORS}ca=30;41:"
  LS_COLORS="${LS_COLORS}tw=30;42:"           # directory with sticky bit and world writable (+t, o+w)
  LS_COLORS="${LS_COLORS}ow=30;107:"          # directory that is world writable
  LS_COLORS="${LS_COLORS}st=37;44:"           # directory with sticky bit
  LS_COLORS="${LS_COLORS}ex=01;32:"           # executable file

  # Extenions
  # ---------
# LS_COLORS="${LS_COLORS}*.tar=01;31:"
# LS_COLORS="${LS_COLORS}*.tgz=01;31:"
# LS_COLORS="${LS_COLORS}*.arc=01;31:"
# LS_COLORS="${LS_COLORS}*.arj=01;31:"
# LS_COLORS="${LS_COLORS}*.taz=01;31:"
# LS_COLORS="${LS_COLORS}*.lha=01;31:"
# LS_COLORS="${LS_COLORS}*.lz4=01;31:"
# LS_COLORS="${LS_COLORS}*.lzh=01;31:"
# LS_COLORS="${LS_COLORS}*.lzma=01;31:"
# LS_COLORS="${LS_COLORS}*.tlz=01;31:"
# LS_COLORS="${LS_COLORS}*.txz=01;31:"
# LS_COLORS="${LS_COLORS}*.tzo=01;31:"
# LS_COLORS="${LS_COLORS}*.t7z=01;31:"
# LS_COLORS="${LS_COLORS}*.zip=01;31:"
# LS_COLORS="${LS_COLORS}*.z=01;31:"
# LS_COLORS="${LS_COLORS}*.Z=01;31:"
# LS_COLORS="${LS_COLORS}*.dz=01;31:"
# LS_COLORS="${LS_COLORS}*.gz=01;31:"
# LS_COLORS="${LS_COLORS}*.lrz=01;31:"
# LS_COLORS="${LS_COLORS}*.lz=01;31:"
# LS_COLORS="${LS_COLORS}*.lzo=01;31:"
# LS_COLORS="${LS_COLORS}*.xz=01;31:"
# LS_COLORS="${LS_COLORS}*.zst=01;31:"
# LS_COLORS="${LS_COLORS}*.tzst=01;31:"
# LS_COLORS="${LS_COLORS}*.bz2=01;31:"
# LS_COLORS="${LS_COLORS}*.bz=01;31:"
# LS_COLORS="${LS_COLORS}*.tbz=01;31:"
# LS_COLORS="${LS_COLORS}*.tbz2=01;31:"
# LS_COLORS="${LS_COLORS}*.tz=01;31:"
# LS_COLORS="${LS_COLORS}*.deb=01;31:"
# LS_COLORS="${LS_COLORS}*.rpm=01;31:"
# LS_COLORS="${LS_COLORS}*.jar=01;31:"
# LS_COLORS="${LS_COLORS}*.war=01;31:"
# LS_COLORS="${LS_COLORS}*.ear=01;31:"
# LS_COLORS="${LS_COLORS}*.sar=01;31:"
# LS_COLORS="${LS_COLORS}*.rar=01;31:"
# LS_COLORS="${LS_COLORS}*.alz=01;31:"
# LS_COLORS="${LS_COLORS}*.ace=01;31:"
# LS_COLORS="${LS_COLORS}*.zoo=01;31:"
# LS_COLORS="${LS_COLORS}*.cpio=01;31:"
# LS_COLORS="${LS_COLORS}*.7z=01;31:"
# LS_COLORS="${LS_COLORS}*.rz=01;31:"
# LS_COLORS="${LS_COLORS}*.cab=01;31:"
# LS_COLORS="${LS_COLORS}*.jpg=01;35:"
# LS_COLORS="${LS_COLORS}*.jpeg=01;35:"
# LS_COLORS="${LS_COLORS}*.mjpg=01;35:"
# LS_COLORS="${LS_COLORS}*.mjpeg=01;35:"
# LS_COLORS="${LS_COLORS}*.gif=01;35:"
# LS_COLORS="${LS_COLORS}*.bmp=01;35:"
# LS_COLORS="${LS_COLORS}*.pbm=01;35:"
# LS_COLORS="${LS_COLORS}*.pgm=01;35:"
# LS_COLORS="${LS_COLORS}*.ppm=01;35:"
# LS_COLORS="${LS_COLORS}*.tga=01;35:"
# LS_COLORS="${LS_COLORS}*.xbm=01;35:"
# LS_COLORS="${LS_COLORS}*.xpm=01;35:"
# LS_COLORS="${LS_COLORS}*.tif=01;35:"
# LS_COLORS="${LS_COLORS}*.tiff=01;35:"
# LS_COLORS="${LS_COLORS}*.png=01;35:"
# LS_COLORS="${LS_COLORS}*.svg=01;35:"
# LS_COLORS="${LS_COLORS}*.svgz=01;35:"
# LS_COLORS="${LS_COLORS}*.mng=01;35:"
# LS_COLORS="${LS_COLORS}*.pcx=01;35:"
# LS_COLORS="${LS_COLORS}*.mov=01;35:"
# LS_COLORS="${LS_COLORS}*.mpg=01;35:"
# LS_COLORS="${LS_COLORS}*.mpeg=01;35:"
# LS_COLORS="${LS_COLORS}*.m2v=01;35:"
# LS_COLORS="${LS_COLORS}*.mkv=01;35:"
# LS_COLORS="${LS_COLORS}*.webm=01;35:"
# LS_COLORS="${LS_COLORS}*.ogm=01;35:"
# LS_COLORS="${LS_COLORS}*.mp4=01;35:"
# LS_COLORS="${LS_COLORS}*.m4v=01;35:"
# LS_COLORS="${LS_COLORS}*.mp4v=01;35:"
# LS_COLORS="${LS_COLORS}*.vob=01;35:"
# LS_COLORS="${LS_COLORS}*.qt=01;35:"
# LS_COLORS="${LS_COLORS}*.nuv=01;35:"
# LS_COLORS="${LS_COLORS}*.wmv=01;35:"
# LS_COLORS="${LS_COLORS}*.asf=01;35:"
# LS_COLORS="${LS_COLORS}*.rm=01;35:"
# LS_COLORS="${LS_COLORS}*.rmvb=01;35:"
# LS_COLORS="${LS_COLORS}*.flc=01;35:"
# LS_COLORS="${LS_COLORS}*.avi=01;35:"
# LS_COLORS="${LS_COLORS}*.fli=01;35:"
# LS_COLORS="${LS_COLORS}*.flv=01;35:"
# LS_COLORS="${LS_COLORS}*.gl=01;35:"
# LS_COLORS="${LS_COLORS}*.dl=01;35:"
# LS_COLORS="${LS_COLORS}*.xcf=01;35:"
# LS_COLORS="${LS_COLORS}*.xwd=01;35:"
# LS_COLORS="${LS_COLORS}*.yuv=01;35:"
# LS_COLORS="${LS_COLORS}*.cgm=01;35:"
# LS_COLORS="${LS_COLORS}*.emf=01;35:"
# LS_COLORS="${LS_COLORS}*.ogv=01;35:"
# LS_COLORS="${LS_COLORS}*.ogx=01;35:"
# LS_COLORS="${LS_COLORS}*.aac=00;36:"
# LS_COLORS="${LS_COLORS}*.au=00;36:"
# LS_COLORS="${LS_COLORS}*.flac=00;36:"
# LS_COLORS="${LS_COLORS}*.m4a=00;36:"
# LS_COLORS="${LS_COLORS}*.mid=00;36:"
# LS_COLORS="${LS_COLORS}*.midi=00;36:"
# LS_COLORS="${LS_COLORS}*.mka=00;36:"
# LS_COLORS="${LS_COLORS}*.mp3=00;36:"
# LS_COLORS="${LS_COLORS}*.mpc=00;36:"
# LS_COLORS="${LS_COLORS}*.ogg=00;36:"
# LS_COLORS="${LS_COLORS}*.ra=00;36:"
# LS_COLORS="${LS_COLORS}*.wav=00;36:"
# LS_COLORS="${LS_COLORS}*.oga=00;36:"
# LS_COLORS="${LS_COLORS}*.opus=00;36:"
# LS_COLORS="${LS_COLORS}*.spx=00;36:"
# LS_COLORS="${LS_COLORS}*.xspf=00;36:"

export LS_COLORS
Github repository PostLinuxInstallation, path: /dot-files/ls_colors

Show largest files in a directory

The output of ls can be piped into sort in order to show a directory's largest files.
Because we want to sort numerically rather than alphabetically, we use the -n flag of sort
ls -l | sort -nrk 5 | head
-h in ls reports file sizes in human readable format (for example 1.89G or 815M; -h in sort sorts by human readable numbers:
ls -lh | sort -hrk 5 | head

See also

https://github.com/ReneNyffenegger/shell-commands/tree/master/ls
Other Shell commands such as stat, dircolors (to set $LS_COLORS) and namei.
The methods scandir() and listdir() found in the Python stdlib os.

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/Linux/sh...', 1758198963, '216.73.216.150', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/Linux/shell/commands/ls/index(373): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78