fd icon indicating copy to clipboard operation
fd copied to clipboard

How to set current working directory of execute command?

Open 0x7FFFFFFFFFFFFFFF opened this issue 2 years ago • 3 comments

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 {}"

0x7FFFFFFFFFFFFFFF avatar Jan 31 '22 22:01 0x7FFFFFFFFFFFFFFF

Does rar have an option to set the extraction directory?

tmccombs avatar Feb 01 '22 04:02 tmccombs

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?

tmccombs avatar Feb 01 '22 08:02 tmccombs

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

sharkdp avatar Feb 01 '22 20:02 sharkdp