Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List files matching pattern when too many for bash globbing

Tags:

find

bash

ls

I'd like to run the following:

ls /path/to/files/pattern*

and get

/path/to/files/pattern1 
/path/to/files/pattern2 
/path/to/files/pattern3

However, there are too many files matching the pattern in that directory, and I get

bash: /bin/ls: Argument list too long

What's a better way to do this? Maybe using the find command? I need to print out the full paths to the files.

like image 577
DavidR Avatar asked Jun 29 '13 16:06

DavidR


1 Answers

This is where find in combination with xargs will help.

find /path/to/files -name "pattern*" -print0 | xargs -0 ls

Note from comments: xargs will help if you wish to do with the list once you have obtained it from find. If you only intend to list the files, then find should suffice. However, if you wish to copy, delete or perform any action on the list, then using xargs instead of -exec will help.

like image 92
jaypal singh Avatar answered Nov 19 '22 00:11

jaypal singh