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?

5 Answers 5

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.

2
  • -type f Thats what i was trying to think of! Thank you coding man!
    – j0h
    Jul 1, 2015 at 16:50
  • @j0h not a problem!
    – nixpower
    Jul 1, 2015 at 17:33
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 {} \;
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).

1
  • 1
    It's a clever idea.
    – A.B.
    Jul 1, 2015 at 20:43
1

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

find /opt/g09 -maxdepth 1 -executable | awk -F/ '{print $NF}'
2
  • Where's your python version bro ? ;) Jul 1, 2015 at 16:26
  • @Serg haha, I had to tie my fingers :) Jul 1, 2015 at 16:33
1

Using a combination of find and perl

find /opt/g09 -maxdepth 1 -type f -executable | perl -pe 's/.+\/(.*)$/\1/'
1
  • Great way to learn some Perl :P
    – nixpower
    Jul 2, 2015 at 12:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .