List files 12 ways

[problem]

You want to list files under UNIX! 🙂 Here I show 12 different ways to do it.

[/problem]

[solution]

See the example and reference tabs.

[/solution]

[example]

1. Basic


[marcus@bagend Perl_Bin]$ ls
d-dump-tst.pl demo-ldap-entry2.pl demo-ldap-entry5.pl ldap-to-sql.pm
logcam.pm showstat.pl
demo-ldap-entry1.pl demo-ldap-entry3.pl demo-ldap-entry6.pl listenport.pl perl_eval_pattern.pl showtime.pl
demo-ldap-entry1.pm demo-ldap-entry4.pl ldap-to-sql.pl logcam.pl
scanport.pl

2. Long


[marcus@bagend Perl_Bin]$ ls -l
total 68
-rwxr--r-- 1 marcus adm 335 Sep 12 2005 d-dump-tst.pl
...
-rwx------ 1 marcus adm 151 Feb 5 2006 showtime.pl

2. Hidden files


[marcus@bagend Perl_Bin]$ ls -la
total 76
drwxr-xr-x 2 marcus adm 4096 Aug 15 04:53 .
drwx------ 29 marcus adm 4096 Sep 23 11:47 ..
-rwxr--r-- 1 marcus adm 335 Sep 12 2005 d-dump-tst.pl
...
-rwx------ 1 marcus adm 151 Feb 5 2006 showtime.pl

3. Sort by last modified


[marcus@bagend Perl_Bin]$ ls -lt
total 76
-rwxr--r-- 1 marcus adm 286 Aug 15 04:53 perl_eval_pattern.pl
-rw-r--r-- 1 marcus adm 210 Apr 30 08:07 logcam.pm
-rwxr--r-- 1 marcus adm 3483 Feb 5 2006 logcam.pl
...
-rwxr--r-- 1 marcus adm 335 Sep 12 2005 d-dump-tst.pl

4. Sort by last modified – reverse


[marcus@bagend Perl_Bin]$ ls -ltr
total 76
-rwxr--r-- 1 marcus adm 335 Sep 12 2005 d-dump-tst.pl
...
-rwxr--r-- 1 marcus adm 3483 Feb 5 2006 logcam.pl
-rw-r--r-- 1 marcus adm 210 Apr 30 08:07 logcam.pm
-rwxr--r-- 1 marcus adm 286 Aug 15 04:53 perl_eval_pattern.pl

5. Show hidden characters in file name


[marcus@bagend Perl_Bin]$ ls -ld *test*
-rw-r--r-- 1 marcus adm 5 Sep 23 11:59 ???testfile1
[marcus@bagend Perl_Bin]$ ls -lb *test*
-rw-r--r-- 1 marcus adm 5 Sep 23 11:59 bbbtestfile1

6. Show a digest of directories With asterix for executable and slash for directory, etc.


[marcus@bagend Perl_Bin]$ ls -aF
./ demo-ldap-entry1.pl* demo-ldap-entry3.pl* demo-ldap-entry6.pl*
listenport.pl* perl_eval_pattern.pl* showtime.pl*
../ demo-ldap-entry1.pm demo-ldap-entry4.pl* ldap-to-sql.pl*
logcam.pl* scanport.pl*
d-dump-tst.pl* demo-ldap-entry2.pl* demo-ldap-entry5.pl* ldap-to-sql.pm
logcam.pm showstat.pl*

7. Do a recursive listing

[marcus@bagend Perl_Bin]$ ls -lR
.:
total 76
-rwxr--r-- 1 marcus adm 335 Sep 12 2005 d-dump-tst.pl
...
-rw-r--r-- 1 marcus adm 5 Sep 23 11:59 ???testfile1

./test1:
total 4
drwxr-xr-x 2 marcus adm 4096 Sep 23 12:01 test2

./test1/test2:
total 0
-rw-r--r-- 1 marcus adm 0 Sep 23 12:01 showit

8. List the directory not its contents.


[marcus@bagend marcus]$ ls -ld Perl_Bin
drwxr-xr-x 3 marcus adm 4096 Sep 23 12:01 Perl_Bin

9. Digest the listing


[marcus@bagend Perl_Bin]$ ls -m
d-dump-tst.pl, demo-ldap-entry1.pl, demo-ldap-entry1.pm, demo-ldap-entry2.pl, demo-ldap-entry3.pl, demo-ldap-entry4.pl, demo-ldap-entry5.pl,
demo-ldap-entry6.pl, ldap-to-sql.pl, ldap-to-sql.pm, listenport.pl, logcam.pl, logcam.pm, perl_eval_pattern.pl, scanport.pl, showstat.pl,
showtime.pl, test1, ???testfile1

10. Include the inode – useful for then doing find . -inum #### – to remove, etc


[marcus@bagend Perl_Bin]$ ls -li
total 76
647846 -rwxr--r-- 1 marcus adm 335 Sep 12 2005 d-dump-tst.pl
...
648063 -rw-r--r-- 1 marcus adm 5 Sep 23 11:59 ???testfile1

11. Force ls to produce one column


[marcus@bagend Perl_Bin]$ ls -1
d-dump-tst.pl
...

12. Show file modified time in seconds Okay I cheated here – it is Perl! 🙂 But hey it’s my blog!! 😉


[marcus@bagend Perl_Bin]$ perl -e
'print(localtime((stat($ARGV[0]))[9])."n");' mytestfile
Sat Sep 23 12:50:03 2006

[/example]

[reference]

[tags]Listing files, Unix Coding School[/tags]

[/reference]

If you have found my website useful, please consider buying me a coffee below 😉

Leave a Reply

Your email address will not be published. Required fields are marked *