23

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.

DavidR
  • 810
  • 2
  • 8
  • 16

1 Answers1

41

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.

jaypal singh
  • 74,723
  • 23
  • 102
  • 147
  • 2
    Actually, find /path/to/files -name "pattern*" seems to do exactly what I want -- any reason not to use that? Thanks! – DavidR Jun 29 '13 at 16:21
  • Well only if you wish to do something once you have obtained a list. For example copy them somewhere else, or delete them. In such cases `xargs` will help. If you just want to list them, then yes, you don't need `xargs` for that. – jaypal singh Jun 29 '13 at 16:22
  • Great -- I'm actually using this command to pipe into GNU parallel, which I use instead of xargs. [No need for the -print0 in that case.] – DavidR Jun 29 '13 at 16:29
  • Worked... I used a slightly different version: `find / -name "pattern*" 2>/dev/null -print0 | xargs -0 ls -la` ... the devnull stuff suppresses permissions errors. – Eric Hepperle - CodeSlayer2010 Nov 18 '14 at 19:12