Some useful tips for listing directories with ls on Linux.

Posted: October 14, 2017. At: 9:57 PM. This was 1 month ago. Post ID: 11455
Page permalink: http://securitronlinux.com/bejiitaswrath/some-useful-tips-for-listing-directories-with-ls-on-linux/

To list the contents of a directory with the listing sorted by size, use the parameters in this example.

ls --format=long --sort=size -h

This is what you will get.

total 15G
-rw-rw-r-- 1 ubuntu ubuntu  15G Dec  2  2014 crackstation.txt
-rw-rw-r-- 1 ubuntu ubuntu  20M Mar 20  2015 system.save
-rw-rw-r-- 1 ubuntu ubuntu 9.3M Nov 18  2015 SYSTEM
-rw-rw-r-- 1 ubuntu ubuntu 256K Nov 18  2015 SAM
-rw-rw-r-- 1 ubuntu ubuntu 256K Nov 18  2015 SECURITY
-rw-rw-r-- 1 ubuntu ubuntu  64K Mar 20  2015 sam.save
-rwxrwxr-x 1 ubuntu ubuntu  63K Mar 20  2015 secretsdump.py
-rw-rw-r-- 1 ubuntu ubuntu  32K Mar 20  2015 security.save
-rwxrwxr-x 1 ubuntu ubuntu 8.7K Aug  9 23:17 a.out
-rwxrwxr-x 1 ubuntu ubuntu 8.7K Aug  9 23:18 read
-rw-rw-r-- 1 ubuntu ubuntu 7.6K Jun 18  2015 virii.S
-rwxrwxr-x 1 ubuntu ubuntu 7.6K Mar 20  2015 recipe-502268-1.py
drwxrwxr-x 6 ubuntu ubuntu 4.0K Mar 20  2015 impacket-read-only
drwxrwxr-x 3 ubuntu ubuntu 4.0K Mar 20  2015 python
drwxrwxr-x 9 ubuntu ubuntu 4.0K Jul 21 07:00 wpscan
-rw-rw-r-- 1 ubuntu ubuntu 2.5K Jun 18  2015 hole.o
-rw------- 1 ubuntu ubuntu 1.7K Jul 23 22:23 id_rsa
-rw-rw-r-- 1 ubuntu ubuntu 1.3K Aug  9 23:18 read.c
-rw-rw-r-- 1 ubuntu ubuntu 1.2K Nov 18  2015 hashes.out
-rw-rw-r-- 1 ubuntu ubuntu 1.1K Jul 25 02:00 my.c
-rw-rw-r-- 1 ubuntu ubuntu  995 Mar 18  2015 nikto-pinterest-log.log
-rw-r--r-- 1 ubuntu ubuntu  404 Jul 23 22:23 id_rsa.pub

Create a directory listing with each entry surrounded by double quotes, and a comma-delimited output.

ls --format=commas -Q

Ignore files that match a certain pattern. In this example, I am hiding files that start with “my”.

 ls --ignore=my* -hul

Related example, get a verbose creation date for a file.

date -d "@$(stat -c '%Y' my.c)" '+%a %b %d %T %Z %Y'

Get a directory listing with all files in megabytes.

ls --block-size=M -la

Print a directory listing with a verbose date.

ls -l --time-style=+%H:%M:%S:%Y

This is another ls format output, this is more compact than the standard ls output.

┌─[jason@parrot][~]
└──╼ $ls --format=across
Desktop     Documents   Downloads          ipinfo                psk-01.cap   psk-01.csv   psk-01.kismet.csv   psk-01.kismet.netxml
psk-02.cap  psk-02.csv  psk-02.kismet.csv  psk-02.kismet.netxml  psk-03.cap   psk-03.csv   psk-03.kismet.csv   psk-03.kismet.netxml
psk-04.cap  psk-04.csv  psk-04.kismet.csv  psk-04.kismet.netxml  psk2-01.cap  psk2-01.csv  psk2-01.kismet.csv  psk2-01.kismet.netxml
telstra     Templates   voteyes

Another way to get a ls -l listing.

┌─[jason@parrot][~]
└──╼ $ls --format=verbose -h
total 69M
drwxr-xr-x 2 jason jason 4.0K Aug 24 14:08 Desktop
drwxr-xr-x 2 jason jason 4.0K Aug 25 16:24 Documents
drwx------ 2 jason jason 4.0K Aug 25 16:24 Downloads
drwxr-xr-x 5 jason jason 4.0K Aug 11 08:05 ipinfo
-rw-r--r-- 1 root  root  7.0K Aug  1 09:56 psk-01.cap
-rw-r--r-- 1 root  root   389 Aug  1 09:57 psk-01.csv
-rw-r--r-- 1 root  root   593 Aug  1 09:57 psk-01.kismet.csv
-rw-r--r-- 1 root  root  1.7K Aug  1 09:57 psk-01.kismet.netxml
-rw-r--r-- 1 root  root  530K Aug  1 10:00 psk-02.cap
-rw-r--r-- 1 root  root   775 Aug  1 10:00 psk-02.csv
-rw-r--r-- 1 root  root   599 Aug  1 10:00 psk-02.kismet.csv
-rw-r--r-- 1 root  root  5.9K Aug  1 10:00 psk-02.kismet.netxml
-rw-r--r-- 1 root  root   39M Aug  1 12:37 psk-03.cap
-rw-r--r-- 1 root  root   968 Aug  1 12:37 psk-03.csv
-rw-r--r-- 1 root  root   605 Aug  1 12:37 psk-03.kismet.csv
-rw-r--r-- 1 root  root  7.9K Aug  1 12:37 psk-03.kismet.netxml
-rw-r--r-- 1 root  root   29M Aug  1 16:24 psk-04.cap
-rw-r--r-- 1 root  root   674 Aug  1 16:24 psk-04.csv
-rw-r--r-- 1 root  root   600 Aug  1 16:24 psk-04.kismet.csv
-rw-r--r-- 1 root  root  4.8K Aug  1 16:24 psk-04.kismet.netxml
-rw-r--r-- 1 root  root  571K Aug 10 09:24 psk2-01.cap
-rw-r--r-- 1 root  root   683 Aug 10 09:24 psk2-01.csv
-rw-r--r-- 1 root  root   607 Aug 10 09:24 psk2-01.kismet.csv
-rw-r--r-- 1 root  root  4.8K Aug 10 09:24 psk2-01.kismet.netxml
drwxr-xr-x 2 root  root  4.0K Aug 24 08:14 telstra
drwxr-xr-x 5 jason jason 4.0K Aug  1 09:38 Templates
drwxr-xr-x 2 jason jason 4.0K Sep 27 11:13 voteyes

Another way to sort all files by their filesize and show the filesize in megabytes.

┌─[jason@parrot][~]
└──╼ $ls --format=verbose -Sh
total 69M
-rw-r--r-- 1 root  root   39M Aug  1 12:37 psk-03.cap
-rw-r--r-- 1 root  root   29M Aug  1 16:24 psk-04.cap
-rw-r--r-- 1 root  root  571K Aug 10 09:24 psk2-01.cap
-rw-r--r-- 1 root  root  530K Aug  1 10:00 psk-02.cap
-rw-r--r-- 1 root  root  7.9K Aug  1 12:37 psk-03.kismet.netxml
-rw-r--r-- 1 root  root  7.0K Aug  1 09:56 psk-01.cap
-rw-r--r-- 1 root  root  5.9K Aug  1 10:00 psk-02.kismet.netxml
-rw-r--r-- 1 root  root  4.8K Aug 10 09:24 psk2-01.kismet.netxml
-rw-r--r-- 1 root  root  4.8K Aug  1 16:24 psk-04.kismet.netxml
drwxr-xr-x 2 jason jason 4.0K Aug 24 14:08 Desktop
drwxr-xr-x 2 jason jason 4.0K Aug 25 16:24 Documents
drwx------ 2 jason jason 4.0K Aug 25 16:24 Downloads
drwxr-xr-x 5 jason jason 4.0K Aug 11 08:05 ipinfo
drwxr-xr-x 2 root  root  4.0K Aug 24 08:14 telstra
drwxr-xr-x 5 jason jason 4.0K Aug  1 09:38 Templates
drwxr-xr-x 2 jason jason 4.0K Sep 27 11:13 voteyes
-rw-r--r-- 1 root  root  1.7K Aug  1 09:57 psk-01.kismet.netxml
-rw-r--r-- 1 root  root   968 Aug  1 12:37 psk-03.csv
-rw-r--r-- 1 root  root   775 Aug  1 10:00 psk-02.csv
-rw-r--r-- 1 root  root   683 Aug 10 09:24 psk2-01.csv
-rw-r--r-- 1 root  root   674 Aug  1 16:24 psk-04.csv
-rw-r--r-- 1 root  root   607 Aug 10 09:24 psk2-01.kismet.csv
-rw-r--r-- 1 root  root   605 Aug  1 12:37 psk-03.kismet.csv
-rw-r--r-- 1 root  root   600 Aug  1 16:24 psk-04.kismet.csv
-rw-r--r-- 1 root  root   599 Aug  1 10:00 psk-02.kismet.csv
-rw-r--r-- 1 root  root   593 Aug  1 09:57 psk-01.kismet.csv
-rw-r--r-- 1 root  root   389 Aug  1 09:57 psk-01.csv

No comments have been made. Use this form to start the conversation :)

Leave a Reply