Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am in a bash script and I want to get the list of all files (let say all jar files). I execute the command ls -1 lib/*.jar and I get the output:


Is there any option to have the following output:


Making cd lib before is not an option as I am in a loop and need to be in the parent folder for the actions I want to do inside the loop.

I tried to find information by typing man ls but did not find any way.

A solution with awk would be good as long I can pipe it to my ls command.

share|improve this question
up vote 14 down vote accepted

Instead of parsing ls you should use find instead. Then you can also execute basename on each file to strip the leading directories:

find lib/ -name '*.jar' -exec basename {} \;
share|improve this answer
Ok this way is perfect for me as well. – ruffp Apr 15 '14 at 13:46
Also see the -maxdepth option. – Evan Teitelman Apr 15 '14 at 20:11

How about (cd lib && echo *.jar), assuming that you don't have whitespace or special characters in the file names. Parent script never changes directory.

share|improve this answer
Spaces won't be an issue either. – terdon Apr 15 '14 at 14:39
This is a good idea and will probably be the fastest option. I would do printf '%s\n' *.jar to get each file on a different line though. – Graeme Apr 15 '14 at 15:14
Or even printf '%s\0' *.jar to eliminate whitespace issues (although this is not the Q). – Graeme Apr 15 '14 at 15:16

With GNU find there is no need to run basename for every single file, this will be much faster (especially if there is a lot of files):

find lib -name '*.jar' -printf '%P\n'
share|improve this answer

As Josh Jolly said in his answer, you should never parse ls, use the approach in his answer instead. Still, here's an awk solution to remove paths from file names, just don't use it with ls:

find . | awk -F'/' '{print $NF}'

The -F'/' sets the field separator to / which means that the last field, $NF, will be the file name.

share|improve this answer

find is probably the way to go, but if you really, really do (you don't) want to strip off lib/ from ls -1, you can use sed:

$ ls -1 lib/*.jar | sed 's#^lib/##'
share|improve this answer

An alternative way solve your query is to list all the files using ls -R. Combine the output of ls command with grep to list only .jar files. You can use following command to do the same for your query:

ls -R lib | grep jar| grep -v jar*
share|improve this answer
This would fail badly if there were a subdirectory that also contains jar files, or a file named for example "list-of-jars.txt". – Jules Apr 16 '14 at 3:14
grep -v jar* would filter out results with file named like your quoted example. – joshi.mohit86 Apr 16 '14 at 6:18
true. it would also filter out all the intended files. Now I look at it again, I can see no circumstances where the command you suggest would actually produce any output at all... – Jules Apr 19 '14 at 3:39
No, it would display files like mylib_2.jar and filter out files like list-of-jars.txt. – joshi.mohit86 May 13 '14 at 10:07
No, it doesn't. I just tried it. find development -name '*.jar' | wc -l -> 70. ls -R development | grep jar| grep -v jar* -> no output at all. – Jules May 14 '14 at 11:35

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.