Learn Docker With My Newest Course

Dive into Docker takes you from "What is Docker?" to confidently applying Docker to your own projects. It's packed with best practices and examples. Start Learning Docker →

Customize ls Output to Not Show the Date

customize-ls-output-to-not-show-the-date.jpg

This can be handy if you want to see all files and their size but not the date because you want to diff the output of 2 lists of files.

Quick Jump:

This came up recently when I wanted to diff the output of a list of files on my main computer and my Chromebook running Linux. I wanted to make sure that I copied over the same exact number of files and the files were all the same size. The files were copied where the timestamp of the files were not the same so it reported a diff on every file.

The --time-style flag lets us customize or even remove the date / time.

Here’s an example directory / file tree that we’ll use:

$ tree
.
├── 1
└── a
    ├── 2
    └── b
        ├── 3
        └── c
            └── 4

3 directories, 4 files

You can run ls -la to list all files in the current directory, such as:

$ ls -la
total 12
drwxr-xr-x   3 nick nick 4096 Oct 26 10:05 .
drwxr-xr-x 105 nick nick 4096 Oct 26 10:05 ..
-rw-r--r--   1 nick nick    0 Oct 26 10:05 1
drwxr-xr-x   3 nick nick 4096 Oct 26 10:05 a

You can also do ls -laR to recursively list all files and directories:

$ ls -laR
.:
total 12
drwxr-xr-x   3 nick nick 4096 Oct 26 10:05 .
drwxr-xr-x 105 nick nick 4096 Oct 26 10:05 ..
-rw-r--r--   1 nick nick    0 Oct 26 10:05 1
drwxr-xr-x   3 nick nick 4096 Oct 26 10:05 a

./a:
total 12
drwxr-xr-x 3 nick nick 4096 Oct 26 10:05 .
drwxr-xr-x 3 nick nick 4096 Oct 26 10:05 ..
-rw-r--r-- 1 nick nick    0 Oct 26 10:05 2
drwxr-xr-x 3 nick nick 4096 Oct 26 10:05 b

./a/b:
total 12
drwxr-xr-x 3 nick nick 4096 Oct 26 10:05 .
drwxr-xr-x 3 nick nick 4096 Oct 26 10:05 ..
-rw-r--r-- 1 nick nick    0 Oct 26 10:05 3
drwxr-xr-x 2 nick nick 4096 Oct 26 10:05 c

./a/b/c:
total 8
drwxr-xr-x 2 nick nick 4096 Oct 26 10:05 .
drwxr-xr-x 3 nick nick 4096 Oct 26 10:05 ..
-rw-r--r-- 1 nick nick    0 Oct 26 10:05 4

You can set the time to be an empty string with ls -laR --time-style=+"":

$ ls -laR --time-style=+""
.:
total 12
drwxr-xr-x   3 nick nick 4096  .
drwxr-xr-x 105 nick nick 4096  ..
-rw-r--r--   1 nick nick    0  1
drwxr-xr-x   3 nick nick 4096  a

./a:
total 12
drwxr-xr-x 3 nick nick 4096  .
drwxr-xr-x 3 nick nick 4096  ..
-rw-r--r-- 1 nick nick    0  2
drwxr-xr-x 3 nick nick 4096  b

./a/b:
total 12
drwxr-xr-x 3 nick nick 4096  .
drwxr-xr-x 3 nick nick 4096  ..
-rw-r--r-- 1 nick nick    0  3
drwxr-xr-x 2 nick nick 4096  c

./a/b/c:
total 8
drwxr-xr-x 2 nick nick 4096  .
drwxr-xr-x 3 nick nick 4096  ..
-rw-r--r-- 1 nick nick    0  4

If you look at the above output and the one right above that you’ll see that the only difference is the date / time is not included. This isn’t something I’ll use too often but it’s nice to know it exists.

The video below demos running ls in a few different ways.

# Demo Video

Timestamps

  • 0:32 – Recursively listing files and directories with the date
  • 0:45 – Removing the date and time from the output

What will you use this for? Let me know below.

Never Miss a Tip, Trick or Tutorial

Like you, I'm super protective of my inbox, so don't worry about getting spammed. You can expect a few emails per year (at most), and you can 1-click unsubscribe at any time. See what else you'll get too.



Comments