Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list only files and not directories of a directory Bash?

How can I list all the files of one folder but not their folders or subfiles. In other words: How can I list only the files?

like image 754
Rodrigo Avatar asked May 13 '12 20:05

Rodrigo


People also ask

How do I get a list of files in a directory in bash?

Use the ls Command to List Directories in Bash. We use the ls command to list items in the current directory in Bash. However, we can use */ to print directories only since all directories finish in a / with the -d option to assure that only the directories' names are displayed rather than their contents.


2 Answers

Using find:

find . -maxdepth 1 -type f 

Using the -maxdepth 1 option ensures that you only look in the current directory (or, if you replace the . with some path, that directory). If you want a full recursive listing of all files in that and subdirectories, just remove that option.

like image 97
carlpett Avatar answered Sep 18 '22 14:09

carlpett


ls -p | grep -v / 

ls -p lets you show / after the folder name, which acts as a tag for you to remove.

like image 31
Dr_Hope Avatar answered Sep 19 '22 14:09

Dr_Hope