ls for the win 🏆

ls command 🔭

The UNIX ls command is essential. I use it everyday. At its core it lists files and directories. Here is the basic output from running ls in the root directory of the gspc.digital project:

1
2
3
(.venv) (base) ➜  gspc-site git:(ls) ✗ ls
gspc                 instance             pyproject.toml
requirements-dev.txt requirements.txt     tests

You can see that it returns the directories and files in the current working directory, but it can do a lot more.

ls -a ♟️

Adding the -a flag to the command will show hidden files (files that are named with a leading .) like .gitignore etc. Here is the output of ls -a on the same gspc.digital directory:

1
2
3
4
5
(.venv) (base) ➜  gspc-site git:(ls) ✗ ls -a
(.venv) (base) ➜  gspc-site git:(ls) ✗ ls -a
.                    .git                 .python-version      gspc                 requirements-dev.txt
..                   .github              .ruff_cache          instance             requirements.txt
.DS_Store            .gitignore           .venv                pyproject.toml       tests

As you can see this output reveals the files and directories that begin with a ..

ls -l 🏄‍♀️

Using the -l flag you can get a long list that contains some additional data about your files:

1
2
3
4
5
6
7
8
(.venv) (base) ➜  gspc-site git:(ls) ✗ ls -l
total 24
drwxr-xr-x  10 gspcorp  staff   320 Sep  2 09:05 gspc
drwxr-xr-x   2 gspcorp  staff    64 Feb 17  2024 instance
-rw-r--r--   1 gspcorp  staff   195 Feb 18  2024 pyproject.toml
-rw-r--r--@  1 gspcorp  staff     5 Aug  2 17:31 requirements-dev.txt
-rw-r--r--   1 gspcorp  staff  2108 Jul 29 21:52 requirements.txt
drwxr-xr-x   2 gspcorp  staff    64 Feb 17  2024 tests

This output contains the file permissions, the number of links, the owner, the group, bytes in the file, date modified, and path.

Combining -l and -a flags ➕

To combine flags just type each flag in sequence ala ls -al:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
(.venv) (base) ➜  gspc-site git:(ls) ✗ ls -al
total 56
drwxr-xr-x  15 gspcorp  staff   480 Aug  2 15:08 .
drwxr-xr-x   4 gspcorp  staff   128 Jul 16 16:08 ..
-rw-r--r--@  1 gspcorp  staff  6148 Jul 28 13:41 .DS_Store
drwxr-xr-x  15 gspcorp  staff   480 Sep 24 17:10 .git
drwxr-xr-x   3 gspcorp  staff    96 Jul 29 21:52 .github
-rw-r--r--   1 gspcorp  staff   110 Jul 29 21:52 .gitignore
-rw-r--r--   1 gspcorp  staff     7 Feb 17  2024 .python-version
drwxr-xr-x   5 gspcorp  staff   160 Aug  2 15:04 .ruff_cache
drwxr-xr-x   9 gspcorp  staff   288 Jul  2 17:48 .venv
drwxr-xr-x  10 gspcorp  staff   320 Sep  2 09:05 gspc
drwxr-xr-x   2 gspcorp  staff    64 Feb 17  2024 instance
-rw-r--r--   1 gspcorp  staff   195 Feb 18  2024 pyproject.toml
-rw-r--r--@  1 gspcorp  staff     5 Aug  2 17:31 requirements-dev.txt
-rw-r--r--   1 gspcorp  staff  2108 Jul 29 21:52 requirements.txt
drwxr-xr-x   2 gspcorp  staff    64 Feb 17  2024 tests

This gives the hidden (dot files) and other files in the directory in long format.

The -h flag; one of my favorites 🫀

Combining the -h flag with the -l flag gives the file sizes in what I call 'human readable' format (the man page has the correct definition).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
(.venv) (base) ➜  gspc-site git:(ls) ✗ ls -alh
total 56
drwxr-xr-x  15 gspcorp  staff   480B Aug  2 15:08 .
drwxr-xr-x   4 gspcorp  staff   128B Jul 16 16:08 ..
-rw-r--r--@  1 gspcorp  staff   6.0K Jul 28 13:41 .DS_Store
drwxr-xr-x  15 gspcorp  staff   480B Sep 24 17:10 .git
drwxr-xr-x   3 gspcorp  staff    96B Jul 29 21:52 .github
-rw-r--r--   1 gspcorp  staff   110B Jul 29 21:52 .gitignore
-rw-r--r--   1 gspcorp  staff     7B Feb 17  2024 .python-version
drwxr-xr-x   5 gspcorp  staff   160B Aug  2 15:04 .ruff_cache
drwxr-xr-x   9 gspcorp  staff   288B Jul  2 17:48 .venv
drwxr-xr-x  10 gspcorp  staff   320B Sep  2 09:05 gspc
drwxr-xr-x   2 gspcorp  staff    64B Feb 17  2024 instance
-rw-r--r--   1 gspcorp  staff   195B Feb 18  2024 pyproject.toml
-rw-r--r--@  1 gspcorp  staff     5B Aug  2 17:31 requirements-dev.txt
-rw-r--r--   1 gspcorp  staff   2.1K Jul 29 21:52 requirements.txt
drwxr-xr-x   2 gspcorp  staff    64B Feb 17  2024 tests

You can see in this output that the 5th column contains the file sizes in human readable format.

Aliases 🤖

I like ls -alh so much and use it so often that I alias it to ll on every computer I use with any regularity (my computer, servers, etc.). Put this in your .bashrc or .zshrc file:

1
alias ll="ls -alh"

Now when I type ll I get the long list, hidden files, and human readable file sizes. Woot 🎁

Conclusion 🔎

I use ls everyday all day. Having a little context and info on the available flags can make your daily life easier. For the full power be sure to invoke:

1
man ls

for the ls man page that can reveal other powerful options and flags. I once heard it said that if used properly ls can scramble your eggs 🍳 in the morning ;). I'm not sure about that, but it sure does make my life easier.

For directories with a ton of files combine ls with grep ala:

1
2
3
4
(.venv) (base) ➜  gspc-site git:(ls) ✗ ll |grep .git
drwxr-xr-x  15 gspcorp  staff   480B Sep 24 17:10 .git
drwxr-xr-x   3 gspcorp  staff    96B Jul 29 21:52 .github
-rw-r--r--   1 gspcorp  staff   110B Jul 29 21:52 .gitignore

More on grep in a future post. Thanks for reading.

📨 Contact GSPC ✉️

AWS Certified Certified Terraform Associate AWS Serverless AWS Certified Developer Associate GitHub Foundations Certification GitHub Actions Certification Certified SysOps Administrator Certified AWS Security Specialist Certified AWS DevOps Engineer Professional GCP: Cloud Digital Leader Certification Badge