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.

8 comments:

Max said...

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

Jadu Kumar Saikia said...

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

Dedy said...

nice tips
thanks

Bookmarked

Jadu Saikia said...

@Dedy, you are welcome, thanks :-)

Jadu Saikia said...

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

Vladimir 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'`

daniel hanover said...

ls -d */
Done

© Jadu Saikia www.UNIXCL.com