Bash – Is it possible to remove folder prefix from a `ls` command

bashdirectorylsshell

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:

lib/mylib_1.jar
lib/mylib_2.jar
...

Is there any option to have the following output:

mylib_1.jar
mylib_2.jar
...

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 I did not find any solution.

A solution with another command would be good as long I can pipe it to my ls command or self sufficient.

Best Answer

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 {} \;
Related Question