sharkdp / fd

A simple, fast and user-friendly alternative to 'find'
Apache License 2.0
33.55k stars 801 forks source link

How to set current working directory of execute command? #951

Open 0x7FFFFFFFFFFFFFFF opened 2 years ago

0x7FFFFFFFFFFFFFFF commented 2 years ago

What version of fd are you using? fd 8.3.2

I'm trying to extract a bunch of .rar files. The following command is what I have right now.

fd -d 2 -e rar -a -x rar x -y -ad

The problem is that all the .rar files were extract to the current folder (where I executed the fd command). I hope to extract each rar file to the folder where they are in. How can I pass the directory of the file (one of fd's search result) to rar as its current working directory? I've tried the following but all of no avail.

fd -d 2 -e rar -a -x cmd.exe /c "cd {//}; rar x -y -ad {}"
fd -d 2 -e rar -a -x cmd.exe /c "cd {//} && rar x -y -ad {}"
tmccombs commented 2 years ago

Does rar have an option to set the extraction directory?

tmccombs commented 2 years ago

I'm surprised that fd -d 2 -e rar -a -x cmd.exe /c "cd {//}; rar x -y -ad {}" doesn't work though.

If I run

fd meson -a -x zsh -c 'cd {//}; pwd; ls -l {/}'

in a folder with some meson files, it prints the parent directory followed by the long ls output for the file. Maybe it has to do with how cd wokrs in cmd.exe?

sharkdp commented 2 years ago

find supports this with -execdir command:

       -execdir command ;

       -execdir command {} +
              Like -exec, but the specified command is  run  from
              the subdirectory containing the matched file, which
              is not normally the directory in which you  started
              find.   As  with  -exec, the {} should be quoted if
              find is being invoked from a shell.   This  a  much
              more  secure  method  for  invoking commands, as it
              avoids race conditions  during  resolution  of  the
              paths  to the matched files.  As with the -exec ac‐
              tion, the `+' form of -execdir will build a command
              line to process more than one matched file, but any
              given invocation of command will  only  list  files
              that  exist  in  the same subdirectory.  If you use
              this option, you must ensure that your $PATH  envi‐
              ronment variable does not reference `.'; otherwise,
              an attacker can run any commands they like by leav‐
              ing  an  appropriately-named file in a directory in
              which you will run -execdir.  The same  applies  to
              having  entries  in  $PATH which are empty or which
              are not absolute directory names.  If  any  invoca‐
              tion  with the `+' form returns a non-zero value as
              exit status, then find returns a non-zero exit sta‐
              tus.   If  find encounters an error, this can some‐
              times cause an immediate exit, so some pending com‐
              mands may not be run at all.  The result of the ac‐
              tion depends on whether the + or the ;  variant  is
              being  used;  -execdir command {} +  always returns
              true, while -execdir command {} ; returns true only
              if command returns 0.

see also: #459

de11n commented 1 week ago

I still think it's worth adding this. -x -c 'cd "$1" && X" sh {//} is quite the mouthful and hard to remember. It makes it harder for find users to switch over to fd. I'm a relatively new fd convert, and I'm resorting to find just for this feature because in this case find's ergonomics are better.