Useful ways to list directory contents

The ls command becomes second nature once you work in a Linux command line environment for any appreciable period of time. Commonly, it seems, people simply stick with the plain vanilla command, without exploring its rich options, as well as opportunities for creative aliases. It has a lot to offer!

Options

First an overview of some of ls’s more useful options:

  • -a: This toggles the display of hidden files (those beginning with “.” in *nix). However, the better option is -A. This shows all hidden files, except for the “..” and “.” that represent the current directory and its parent. Normally you don’t want to see these.
  • -F: This adds an indicator after each content item to indicate its type. Directories have a “/” appended to them, executable files have a “*” appended, and so on. In addition to properly coloring content types, this makes it very easy to spot the type of item you are looking for.
  • –group-directories-first: I myself don’t like this option, as I prefer to see all contents in alphabetic order. But if you are used to how most GUI file browsers list directories, with directories ordered first, you might like this option.
  • -h: This prints file sizes in more human readable units instead of bytes, namely K (kilobytes), M (megabytes), and G (gigabytes). If you are feeling perverse, you can add “–si” to have the human readable units converted with powers of 1000 instead of 1024, as hard drive manufacturers are wont to do.
  • -l: This lists directory contents with a single row per content item, in a vertical fashion. This allows for easier display of properties of each item, at the expense of taking more screen space.
  • -r: This reverses the sorting order. Useful if you want to use a long type of display, but are looking for a file near the end of the alphabet in a directory with a lot of contents.
  • -R: This displays subdirectories recursively. I prefer to use the tree command when I want to do this, as it displays the same information in less space and more attractively.
  • -t: Sorts contents by modification time. This is invaluable when you are trying to isolate old or new files.
  • –time-style: This allows you to set the format of the datetime displayed for each content item. You can either use pre-defined combinations such as “full-iso” and “locale”, or define your own. An example is provided in the next section.

Aliases

As you can see, there are a number of advanced features available through the options of ls. But this isn’t quite enough. If you are always typing options to a command (and the same options at that), it’s time to create some aliases.

Here are all the ls-related aliases I have in my .bashrc:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Listing:
alias ll="ls -lF --time-style='+%Y-%m-%d %H:%M:%S'"
alias lt="ls -ltF --time-style='+%Y-%m-%d %H:%M:%S'"
alias lht="ls -lhtF --time-style='+%Y-%m-%d %H:%M:%S'"
alias lst="ls -lshtF --time-style='+%Y-%m-%d %H:%M:%S'"
alias llt="ls -ltF --time-style='+%Y-%m-%d %H:%M:%S'"
alias lll="ls -lF --time-style='+%Y-%m-%d %H:%M:%S'"
alias la="ls -A"
alias lla="ls -lAF"
alias lsfull="ls -AFhlS"
alias lh="ls -lFh --time-style='+%Y-%m-%d %H:%M:%S'"
# Enable color support of ls
if [ "$TERM" != "dumb" ]; then
    eval "`dircolors -b`"
    alias ls="ls --color=auto"
fi

I don’t use all of these with the same frequency, but part of my general strategy in creating aliases is to add any common permutations I might think of. Then if I ever happen to type each version, I am still likely to get what I need.

The “–time-style” option I added to all the time-ordered displays is the same as the standard “ls -l”, but it adds seconds for each modification time. This can be useful if you are looking through a number of files created near the same time.

One other related tip that I don’t myself use but that others have mentioned as helpful is adding ls into an alias for cd:

1
alias cd="cd && ls"

This simplifies the common pattern of changing into a directory and listing its contents. However, it’s too often that I need to change into a directory and perform some task, without listing the contents (in fact doing so simply distracts me), that I added a slightly less intuitive alias:

1
2
3
4
5
# Change into a directory and list contents:
cdl () {
    builtin cd $1 && ls -lh
}
export -f cdl

This way I can still save time if I do want to list contents, and not clutter my display if I just want to change into a directory.

Post to Twitter Post to Delicious Post to Digg Post to Reddit

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

This entry was posted in CLI and tagged , , . Bookmark the permalink.

3 Responses to Useful ways to list directory contents

  1. Stephen P. Schaefer says:

    One of my favorite options is “-c”. Many programs that copy files (tar, rsync, cp -p) preserve the modification time of the original for legitimate reasons, yet there are times when you want to know when the file was actually created/chmoded/other (“When did I install that RPM? When did the automated system install that config file?”). “ls -ltrc” is a macro in my fingers: the most recently “tampered” with stuff is at the bottom of the screen.

  2. As far as I recall, I don’t think I’ve needed to sort files by that, but I will keep it in my toolbox. Thanks!

  3. Cristi says:

    Nice tutorial,thank you

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>