Learning Linux: List Files

Nov 10, 2021 | blog

This article was originally published on Medium on November 10, 2021.

Photo by Andrea Piacquadio from Pexels
Photo by Andrea Piacquadio from Pexels

Basics

When you first open a shell, you’re likely to be in your user’s home directory. All the files here belong to your user. What files you ask? Well, let’s find out. In your shell, type the following command and press the Enter or the Return button.

[bitmvr]:~ $ ls
Applications              Fonts                     Pictures
Videos                    Desktop                   Library                   Documents                 Movies                    Downloads                 Music                     

Long List

The output from that command should show different directories and files. You may spot Desktop, Documents, Pictures, and more. What if you wanted to know when the file was created? Well, the ls command by itself does not present much information to the user. In order to get more information, we have to pass a flag to alter the output format. One of my favorite flags is -l . This will output the files in the long format.

[bitmvr]:~ $ ls -l
total 64
drwx — — — @ 3  bitmvr wheel   96 May 29 2020  Applications
drwx — — — @ 17 bitmvr wheel  544 Nov 9  15:13 Desktop
drwx — — — + 17 bitmvr wheel  544 Oct 20 09:40 Documents
drwx — — — @ 20 bitmvr wheel  640 Nov 9  13:37 Downloads
drwxr-xr-x @ 4  bitmvr wheel  128 Apr 5  2019  Fonts
drwx — — — @ 73 bitmvr wheel 2336 Aug 9  15:46 Library
drwx — — — + 6  bitmvr wheel  192 Jul 6  16:16 Movies
drwx — — — + 7  bitmvr wheel  224 Oct 23 2020  Music
drwx — — — + 8  bitmvr wheel  256 Nov 12 2020  Pictures
drwxr-xr-x @ 6  bitmvr wheel  192 Mar 29 2021  Videos

This format displays information in columns. Here is a high overview of what each column represents:

  1. The permissions of the file.
  2. The number of hard links that file contains.
  3. Which User owns the file.
  4. Which Group owns the file.
  5. The number of bytes for the file.
  6. The next three columns represent the last modified date of the file.
  7. The name of the file.

List All Files

In Linux and other Unix-like operating systems, hidden files begin with a dot. A few common files you will often see in your home directory are .bash_profile , .bashrc, and .bash_history.

In order to list these files, we need to add the -a flag (List all files) to our previous ls -l command.

[bitmvr]:~ $ ls -l -a
total 64
-rw-------   1  bitmvr wheel  66158 Nov  9 20:18 .bash_history
-rw-r--r--@  1  bitmvr wheel    731 Jun 18  2020 .bash_profile
drwx — — — @ 3  bitmvr wheel     96 May 29 2020  Applications
drwx — — — @ 17 bitmvr wheel    544 Nov 9  15:13 Desktop
drwx — — — + 17 bitmvr wheel    544 Oct 20 09:40 Documents
drwx — — — @ 20 bitmvr wheel    640 Nov 9  13:37 Downloads
drwxr-xr-x @ 4  bitmvr wheel    128 Apr 5  2019  Fonts
drwx — — — @ 73 bitmvr wheel   2336 Aug 9  15:46 Library
drwx — — — + 6  bitmvr wheel    192 Jul 6  16:16 Movies
drwx — — — + 7  bitmvr wheel    224 Oct 23 2020  Music
drwx — — — + 8  bitmvr wheel    256 Nov 12 2020  Pictures
drwxr-xr-x @ 6  bitmvr wheel    192 Mar 29 2021  Videos

Tip: You can often times bundle command flags together. In the example above, instead of ls -l -a, you can type ls -la.

If done correctly, you will now be able to see any hidden files. With just two flags, we can now view a lot more information about the structure of our home directory. These are two of the most useful flags applied to the ls command. Hmm… what else could we do to make this output better?

Human Readable File Sizes

Given that we’re executing ls with the -l flag, we can append the -h flag as well. This will modify the output format so that we’re presented with human readable file sizes.

Note, I am going to also use the -a flag because I prefer to always view all files.

[bitmvr]:~ $ ls -lah
total 584
drwxr-xr-x+ 80 bitmvr  wheel   2.5K Nov  9 20:59 .
-rw-------   1 bitmvr  wheel    65K Nov  9 20:18 .bash_history
-rw-r--r--@  1 bitmvr  wheel   731B Jun 18  2020 .bash_profile
-rw-r--r--@  1 bitmvr  wheel   2.8K Nov  1 17:49 .bashrc
rwx------@  3  bitmvr  wheel    96B May 29  2020 Applications
drwx------@ 17 bitmvr  wheel   544B Nov  9 15:13 Desktop
drwx------+ 17 bitmvr  wheel   544B Oct 20 09:40 Documents
drwx------@ 20 bitmvr  wheel   640B Nov  9 13:37 Downloads
drwxr-xr-x@  4 bitmvr  wheel   128B Apr  5  2019 Fonts
drwx------@ 73 bitmvr  wheel   2.3K Aug  9 15:46 Library
drwx------+  6 bitmvr  wheel   192B Jul  6 16:16 Movies
drwx------+  7 bitmvr  wheel   224B Oct 23  2020 Music
drwx------+  8 bitmvr  wheel   256B Nov 12  2020 Pictures
drwxr-xr-x@  7 bitmvr  wheel   224B Oct 11 11:35 Projects
drwxr-xr-x@  6 bitmvr  wheel   192B Mar 29  2021 Videos

Whew. That makes it a bit easier to know the size of any given file in the output. These few flags are usually all that one needs to be very effective reviewing files inside their shell. If you want to learn more, I highly recommend reading the man page for ls. In your shell, just type:

man ls

Happy reading and as always, keep hacking!