Find executable filenames without path
Solution 1:
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.
Solution 2:
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 {} \;
Solution 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).
Solution 4:
With awk
, splitting the path by the delimiter /
, print the last section ($NF
):
find /opt/g09 -maxdepth 1 -executable | awk -F/ '{print $NF}'
Solution 5:
Using a combination of find
and perl
find /opt/g09 -maxdepth 1 -type f -executable | perl -pe 's/.+\/(.*)$/\1/'