7 Quirky ‘ls’ Command Tricks Every Linux User Should Know

We have covered most of the things on ‘ls’ command in last two articles of our Interview series. This article is the last part of the ‘ls command‘ series. If you have not gone through last two articles of this series you may visit the links below.

  1. 15 Basic ‘ls’ Command Examples in Linux
  2. Sort Output of ‘ls’ Command By Last Modified Date and Time
  3. 15 Interview Questions on Linux “ls” Command – Part 1
  4. 10 Useful ‘ls’ Command Interview Questions – Part 2
ls command tricks
7 Quirky ls Command Tricks
1. List the contents of a directory with time using various time styles.

To list the contents of a directory with times using style, we need to choose any of the below two methods.

# ls -l –time-style=[STYLE]               (Method A)

Note – The above switch (--time style must be run with switch -l, else it won’t serve the purpose).

# ls –full-time                           (Method B)

Replace [STYLE] with any of the below option.

full-iso
long-iso
iso
locale
+%H:%M:%S:%D

Note – In the above line H(Hour), M(Minute), S(Second), D(Date) can be used in any order.

Moreover you just choose those relevant and not all options. E.g., ls -l --time-style=+%H will show only hour.

ls -l --time-style=+%H:%M:%D will show Hour, Minute and date.

# ls -l --time-style=full-iso
ls Command Full Time Style
ls Command Full Time Style
# ls -l --time-style=long-iso
Long Time Style Listing
Long Time Style Listing
# ls -l --time-style=iso
Time Style Listing
Time Style Listing
# ls -l --time-style=locale
Locale Time Style Listing
Locale Time Style Listing
# ls -l --time-style=+%H:%M:%S:%D
Date and Time Style Listing
Date and Time Style Listing
# ls --full-time
Full Style Time Listing
Full Style Time Listing
2. Output the contents of a directory in various formats such as separated by commas, horizontal, long, vertical, across, etc.

Contents of directory can be listed using ls command in various format as suggested below.

  1. across
  2. comma
  3. horizontal
  4. long
  5. single-column
  6. verbose
  7. vertical
# ls –-format=across
# ls --format=comma
# ls --format=horizontal
# ls --format=long
# ls --format=single-column
# ls --format=verbose
# ls --format=vertical
Listing Formats of ls Command
Listing Formats of ls Command
3. Use ls command to append indicators like (/=@|) in output to the contents of the directory.

The option -p with ‘ls‘ command will server the purpose. It will append one of the above indicator, based upon the type of file.

# ls -p
Append Indicators to Content
Append Indicators to Content
4. Sort the contents of directory on the basis of extension, size, time and version.

We can use options like --extension to sort the output by extension, size by extension --size, time by using extension -t and version using extension -v.

Also we can use option --none which will output in general way without any sorting in actual.

# ls --sort=extension
# ls --sort=size
# ls --sort=time
# ls --sort=version
# ls --sort=none
Sort Listing of Content by Options
Sort Listing of Content by Options
5. Print numeric UID and GID for every contents of a directory using ls command.

The above scenario can be achieved using flag -n (Numeric-uid-gid) along with ls command.

# ls -n
Print Listing of Content by UID and GID
Print Listing of Content by UID and GID
6. Print the contents of a directory on standard output in more columns than specified by default.

Well ls command output the contents of a directory according to the size of the screen automatically.

We can however manually assign the value of screen width and control number of columns appearing. It can be done using switch ‘--width‘.

# ls --width 80
# ls --width 100
# ls --width 150
List Content Based on Window Sizes
List Content Based on Window Sizes

Note: You can experiment what value you should pass with width flag.

7. Include manual tab size at the contents of directory listed by ls command instead of default 8.
# ls --tabsize=[value]
List Content by Table Size
List Content by Table Size

Note: Specify the [Value]= Numeric value.

That’s all for now. Stay tuned to Tecmint till we come up with next article. Do not forget to provide us with your valuable feedback in the comments below. Like and share us and help us get spread.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Avishek
A Passionate GNU/Linux Enthusiast and Software Developer with over a decade in the field of Linux and Open Source technologies.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

18 Comments

Leave a Reply
  1. Is it possible to sort by extension then alphabetically? In other words, sort alphabetically by extension.?

    Reply
  2. The different file/directory/link/socket etc indicator styles are only seen with the -F option.

    The -p option only appends a slash to the name of directories.

    One of my preferred options is –group-directories-first which does exactly what it says.

    Reply
  3. Hi, am new up here…

    There was a question asking ”What is the ls option that prints the author of a file?” and I googled for the question and found an answer saying ”We need to use the option '--author' along with option '-l' to print the author name of each file.”

    Reply
  4. Very interesting, but I have one question: is it possible to list files from their name? For instance: if I have 1000 files and some of them have the part of the name as “name (1).txt“, can I list them just searching the last part of the name “(1).txt“?

    Reply
    • # ls *(1).txt
      # ls -option  [regular expression]
      

      Regular Expressions are considered a separate topic, and a part of most of the commands in linux

      They are also an art form.

      Reply
      • Sorry, but it doesn’t go!

        Here what I’ve got:

        Cart@pippo:/media/Disk_D/Foto/DCIM$ ls *(1).jpg
        ls: Cannot access '* (1) .jpg': File or directory does not exist
        
        Reply
        • In Bash shell doing

          ls a(1).jpg
          

          results in the error message

          bash: syntax error near unexpected token `('
          

          In Bash and tcsh (and most other shells), parentheses have to be escaped or enclosed in quotes. Wildcards must not be enclosed in quotes or they will not be expanded by the shell.

          So for filenames containing parentheses eg a(1).jpg b(1).jpg, this ls command will work

          ls *"(1).jpg"
          

          or if you prefer to have the quotes just around the critical part

          ls *"(1)".jpg
          

          If you files names contained quotes (double or single), then you would have to escape the quotes themselves to list them.

          Reply
  5. You forgot to mention that the setting of LC_COLLATE (and/or LC_ALL) will affect the order of sorting in the display of ls directory listings.

    Reply
  6. I have one question, let’s say I created 5 files per day (together i.e. 5 days), and I want to see each day only once. How can I do that?

    Reply
  7. I like my tab-stops to be every 4 characters. I set that in vi, so then I added a startup command of “tabs 4” in bash. ‘ls‘ is one of the few commands that breaks, so I aliased my ‘ls‘ command to be ‘ls -T4“. Works great now, EXCEPT when I go to Full-Screen (in an Exceed XTerm window). ANY IDEA WHY?

    But if I rerun the ‘tabs’ and ‘alias’ (of ‘ls’) again, then it works, and I have NO idea why

    Reply
    • I’m guessing that it’s any change of the terminal window size, not just going full-screen. That will reset a bunch of spacing-related stuff, obviously including the terminal tabs set.

      I’d suggest setting the bash PROMPT_COMMAND (or possibly one of PS0/PS1/PS2) environmental variable to include your desired tabs command so it’s run automatically at each prompt. See the bash manpage or info pages for the details.

      Reply
  8. Thanks for the valuable information about ls commands !! Hey can you post about the boot process or can you provide me some good link through which i can understand the boot process in linux

    Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.