Linux+: Linux Shell 07 – Shell Commands Part 4

Discussion in 'Shell / Command Line' started by Jarret W. Buse, Jul 25, 2014.

  1. Jarret W. Buse

    Jarret W. Buse Active Member Staff Writer

    Messages:
    160
    Likes Received:
    221
    Trophy Points:
    43
    Linux+: Linux Shell 07 – Shell Commands Part 4

    In the previous Shell command articles I have discussed changing directories, copying directories and files and finding files. In this article, we will cover listing files and folders.

    The main syntax for the 'ls' command is:

    ls parameters pattern_to_find

    The “pattern to find” is a whole or partial name of files or folders, which may include wildcards. With wildcards, an asterisk (*) is used to represent one or more characters while a question mark (?) represents one character.

    The list of parameters for the ls command is as follows:

    • -a (--all) - displays all files and folders, including '.' and '..'. Use without a “pattern_to_find
    • -A (--almost-all) – same as -a but no '.' and '..'. Use without a “pattern_to_find”
    • --author – shows author of file (use -l as well)
    • -b (--escape) – displays C-style escape characters
    • --block-size=SIZE – prints the size of the file in blocks of set SIZE bytes
    • -c – use with -lt to sort by modification date and time, -l to sort by name
    • -C – use column display
    • --color=when – color the output at specified time:
      • always (default)
      • never
      • auto
    • -d (--directory) – list directories and not contents, do not follow soft links. Using the -d parameter requires the use of a directory name to list. To get all directories within the current folder, use the directory name '*/'
    • -D (--dired) – display in EMAC output mode
    • -f – do not sort output or show hidden files and folders
    • -F (--classify) – each file and folder has a symbol after it to show what it is. The symbols are as follows:
      • none - regular file
      • * - executable regular file
      • / - directory
      • @ - symbolic link
      • | - FIFO
      • = - socket
      • > - door
    • --file-type – same as -F, but does not signify executable files with an asterisk (*)
    • --format=WORD – specifies format for output and its parameter equivalent as follows:
      • verbose – shows extended file and folder information (-l) (this is the letter 'l' and not the number '1'
      • long – same as verbose (-l)
      • commas – files and folders are separated by commas (-m)
      • horizontal – lists files and folders horizontally with equal spacing to allow for a table type layout (-x)
      • across – same as horizontal (-x)
      • vertical – uses a column display (-C)
      • single-column – lists in a column (-1, this is a number one)
    • -g – same as -l, but does not list owner
    • --group-directories-first – places directories before files in listing
    • -h (--human-readable) – shows file sizes in a more readable format, eg.: 1K, 3.0M, etc.
    • -H (--dereference-command-line) – follows listed soft links
    • -hide=PATTERN – do not show listings matching the “PATTERN”
    • --indicator-style=TYPE – defines the style for file indication as follows:
      • none – no indication
      • slash (-p) – adds '/' to directories
      • file-type (--file-type) – does not add asterisk (*) to executables
      • classify (-F) – see the -F parameter
    • -i (--inode) – display inode number for each file
    • -I (--ignore=PATTERN) – specifies a pattern to ignore specified files and folders
    • -k – same as “–block-size=1k
    • -l – uses a long listing format
    • -L (--derefernce) – when a soft link is found, the linked file is used and not the link itself
    • -m – fills width of screen with a comma delimited list
    • -n (--numeric-uid-gid) – same as -l, but also lists file/folder UID and GID
    • -N (--literal) – displays raw entry names
    • -o – same as -l, but without group information
    • -p – add a slash to folder names
    • -q (--hide-control-chars) – a '?' is used for non-graphic characters
    • -Q (--quote-name) – place entries within double quotes
    • -r (--reverse) – reverses sorting order
    • -R (--recursive) – list subdirectories
    • -s – shows size of files in blocks
    • -S – sorts by file size
    • --show-control-chars – show non-graphic characters
    • --si – same as -h, but uses 1,000 and not 1,024 as Kilobyte size
    • --sort=TYPE - sorts the list in a specified way
      • none (-U) – no sorting
      • extension (-X) – sort by the file extensions
      • size (-S) sort by file size
      • time (-t) – sort by time
      • version (-v) – sort by version number
    • --time=TYPE – sorts by time using specific time TYPE
      • atime (-u) – sort by access time
      • access (-u) – sort by access time
      • ctime (-c) – sort by modification time
      • status (-c) – sort by modification time
    • -t – sort list by modification time with newest first
    • -T (--tabsize=COLUMNS) - places tab stops at the specified column count and not at the default of 8
    • -u – sort by and show access time when used with -lt; show access time and sort by name with -l; with neither, sort by access time
    • -U – do not sort but use directory order
    • -w (--width=COLUMN) – assume screen width is COLUMN size
    • -x – lists entries in lines and not columns
    • -X – sort by file extensions
    • -Z (--context) – prints security context of files, only if SELinux is enabled
    • -1 - lists one file per line (this is a numeric 'one')
    • --help – displays 'ls' help information
    • --version – shows 'ls' version

    Most of these are straightforward, but I will discuss the '-d' parameter since it is a unique case. Each parameter does not require a “pattern_to_find”, as listed in the syntax. Some parameters only work properly if the “pattern_to_find” is not included. However, the '-d' option requires the use of '*/' as a pattern_to_find to make it work. The command is as follows:

    ls -d */

    Keep in mind that the parameters can be used together as follows:

    ls -X -l

    Also, be aware that single lettered parameters can be joined into one. The idea is applied to the previous example:


    ls -Xl

    NOTE: Since many single letter parameters can be joined, be aware of the case-sensitivity of each letter.

    For the certification exam, I would suggest to try each of these parameters and be comfortable with them.

    Attached Files:

    • slide.jpg
      slide.jpg
      File size:
      49.4 KB
      Views:
      293,487
    humphreysichei and DevynCJohnson like this.
  2. DevynCJohnson

    DevynCJohnson Well-Known Member Staff Member Staff Writer

    Messages:
    1,287
    Likes Received:
    1,029
    Trophy Points:
    113
    Hambo likes this.
  3. ZHANG/ZHIKUN

    ZHANG/ZHIKUN New Member

    Messages:
    14
    Likes Received:
    1
    Trophy Points:
    3
    I've never realize the –F option. Thanks a lot.

Share This Page