Tuesday, August 14, 2007

Display either Files or Directories -- UNIX

I'm revisiting the common but important ls command. Here's an easy way to show either files or directories that are in the current directory.

Listing files only
# ls -l | awk '{if (substr($1,1,1) == "-") {print}}'
-rw-r--r-- 1 esoft other 103936 Jul 7 21:11 070707_archive.tar
-rw-r--r-- 1 esoft other 348672 Jul 7 21:44 07072007_archive.tar
-rw-r--r-- 1 esoft other 330240 Jul 7 21:16 07072007Y_arch.tar
-rw-r--r-- 1 esoft other 482 Jun 2 19:42 crontab_file
-rw-r--r-- 1 esoft other 1029 May 30 00:21 exclude
-rw-r--r-- 1 esoft other 0 May 30 00:24 include
-rw-r--r-- 1 esoft other 103936 Jul 7 21:14 Jul07%s070707_archive.tar
...

Listing directories only
# ls -ld */.
drwxr-xr-x 2 esoft sys 512 Oct 22 2005 default/.
drwxr-xr-x 3 esoft other 512 Jul 7 20:03 esoft/.
drwx------ 2 esoft staff 512 May 30 22:16 Mail/.
drwxr-xr-x 2 esoft other 512 Jul 7 20:57 RAID/.
drwxr-xr-x 3 esoft other 512 Jul 7 19:33 TEMP/.
drwxr-xr-x 10 esoft other 512 Jun 19 21:59 TEMP1/.
drwxrwxrwx 2 esoft other 512 May 30 00:00 data/.
drwxr-xr-x 4 esoft other 512 Jun 26 21:14 TMP/.
drwxr-xr-x 3 esoft other 512 Jun 26 21:08 TMP1/.

3 comments:

Justin said...

ls -l | grep "^-"

esofthub said...

Thanks Justin

subhoech said...

To list only directories :

ls -l | grep "^d"