Home > front end >  find command - get base name only - NOT with basename command / NOT with printf
find command - get base name only - NOT with basename command / NOT with printf

Time:02-02

Is there any way to get the basename in the command find?

What I don't need:

  • find /dir1 -type f -printf "%f\n"
  • find /dir1 -type f -exec basename {} \;

Why you may ask? Because I need to continue using the found file. I basically want something like this:

find . -type f -exec find /home -type l -name "*{}*" \;

And it uses ./file1, not file1 as the agrument for -name.

CodePudding user response:

Simply spawn a bash shell:

find /dir1 -type f -exec bash -c '
    base=$(basename "$1")
    echo "$base"
    do_something_else "$base"
' bash {} \;

$1 in the bash part is each file filtered by find.

CodePudding user response:

You'll have to forward it to another evaluator. There is now way to do that in find.

find . -type f -printf '%f\0' |
    xargs -r0i find /home -type l -name '*{}*'
  • Related