How to get only names from find command without path

by wakeup   Last Updated May 15, 2018 20:01 PM

I am trying to get only the names from the search result using find, but it always includes the directories too. How can I print only the names (or assign to a variable) using find

find trunk/messages/ -name "*.po" -printf '%f\n'

a similar command to assign this to a variable e.g. "resource" to use it later on.

EDIT: And if possible only the name excluding the extension.

Tags : linux shell

Answers 3

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

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 \;  
March 02, 2013 14:37 PM

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?

December 21, 2015 23:16 PM

Based on a command I found here:

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!!'
October 16, 2016 19:13 PM

Related Questions

Gnome Shell Extension Key Binding

Updated February 26, 2017 10:01 AM

Run batch over results of ls command

Updated November 05, 2016 09:00 AM