Tuesday, December 23, 2008

List only file and not directory - ls command


Not sure if "ls" offers any standard option to list only file names and not the directories.

Some of the ways I found useful to list only files and no directories in my current directory.

Using find:

$ find . -type f -maxdepth 1

And if you want to avoid the hidden files.

$ find . -type f -maxdepth 1 \( ! -iname ".*" \)

And one more way derived from "ls -l" output

$ ls -l | awk 'NR!=1 && !/^d/ {print $NF}'

Please provide any alternatives if you have for the same (Please post in the comment section). Thanks.

11 comments:

Unknown said...

I often use "ls -l | grep -v ^d" to list only files.

Unknown said...

@Max, ya that's one more way. Thank you.

Unknown said...

nice tips
thanks

Bookmarked

Unknown said...

@Dedy, you are welcome, thanks :-)

Unknown said...

And to list only directories and no files, -d option of ls can be used.

Unknown said...

I use "ls -l | awk '/^-/ {print $NF}'" to list without symlinks.

Chris Bloom said...

Ah, excellent. Way better output than `tree` or `ls -lAR`. Came in useful while trying to find a way to list changed files recursively: `find . -type f \( ! -iname ".*" ! -ipath "*.svn*" \) -newerct '5 minutes ago'`

Unknown said...

ls -d */
Done

Unknown said...
This comment has been removed by the author.
Unknown said...
This comment has been removed by the author.
Unknown said...


/bin/ls -ld .//./* |\
sed -n '
:outer
/^-/!d;s|\.//\./||
:inner
p;$d;n
//!binner
bouter
'

© Jadu Saikia www.UNIXCL.com