9

I have a folder with many executables, and I want to omit the path in the results of the find command. this command shows the files I want to see, but it also lists the path; I just want the file name.

find /opt/g09 -maxdepth 1 -executable

how can I get the output of find to show only the filenames, and not the full path?

muru
  • 197,895
  • 55
  • 485
  • 740
j0h
  • 14,825

5 Answers5

10

Or use:

find /opt/g09 -maxdepth 1 -executable -printf "%f\n"

adding the -type f flag also works here.

From the find manual:

 %f     File's name with any leading directories removed (only the last element).

This answer only requires that you have GNU find whereas others require other programs to manipulate your results.

muru
  • 197,895
  • 55
  • 485
  • 740
nixpower
  • 1,210
  • 6
  • 18
6

Use basename :

find /opt/g09 -maxdepth 1 -executable -exec basename {} \;

From man basename:

Print NAME with any leading directory components removed.

Also you are trying to find everything, to restrict your search to only files, use:

find /opt/g09 -type f -maxdepth 1 -executable -exec basename {} \;
heemayl
  • 91,753
3

The most obvious solution to me is

(cd /opt/g09; find -maxdepth 1 -executable)

Because you start a subshell you remain in the same directory. Advantage of this method is that you don't need parsing. Disadvantage is that you start a subshell (you are not going to feel that though).

Bernhard
  • 131
1

With awk, splitting the path by the delimiter /, print the last section ($NF):

find /opt/g09 -maxdepth 1 -executable | awk -F/ '{print $NF}'
Jacob Vlijm
  • 83,767
1

Using a combination of find and perl

find /opt/g09 -maxdepth 1 -type f -executable | perl -pe 's/.+\/(.*)$/\1/'
A.B.
  • 90,397