How to get only names from find command without path

Use find trunk/messages/ -name "*.po" -exec basename {} .po \;

Example and explanations:

Create some test files:

$ touch test1.po  
$ touch test2.po  
$ find . -name "*.po" -print
./test1.po  
./test2.po

Ok, files get found, including path.

For each result execute basename, and strip the .po part of the name

$ find . -name "*.po" -exec basename \{} .po \;  
test1  
test2

You can use -execdir parameter which would print the file without path, e.g.:

find . -name "*.po" -execdir echo {} ';'

Files without extensions:

find . -name "*.txt" -execdir basename {} .po ';'

Note: Since it's not POSIX, BSD find will print clean filenames, however using GNU find will print extra ./.

See: Why GNU find -execdir command behave differently than BSD find?


Based on a command I found here: http://www.unixcl.com/2009/08/remove-path-from-find-result-in-bash.html

You could do this using sed (which executed faster for me, than the basename approach):

find trunk/messages/ -name "*.po" | sed 's!.*/!!' | sed 's!.po!!'

Tags:

Linux

Shell