I want to find and tar/archive all my scripts ending .sh so I wrote something like that:

touch ~/archive.tar; find ~ -type f -name '*.sh' -exec rvf ~/archive.tar {} \; 

The problem is that after I make an archive called archive.tar the archive won't unpack the files into current directory but to directories where it was archived from. For example, there can be archived script called ~/Desktop/wtf/delete.sh but it will be extracted into folder wtf not into current directory where I currently am. So how do I edit my find script to have all scripts in this format ./filename.sh?

  • 1
    Use cd; find . rather than find ~. Then the paths in the tarball will be ./path/to/script.sh rather than /home/username/path/to/script.sh. You can also use the -C option for tar to specify the base path to extract tarballs into. – DopeGhoti May 17 '16 at 19:01
  • @DopeGhoti But if you cd somewhere there can be another direcory and also he probably does not know where all his .sh scripts are located. – pnom May 17 '16 at 19:09
  • In the OP, he's explicitly finding from the home directory. cd with no parameters goes to that location. – DopeGhoti May 17 '16 at 19:24
  • with GNU find, you can use -execdir instead of -exec, and find will cd to each matching file's directory before running the command. See man tar and search for -execdir for details. BTW, use + to terminate the -exec or execdir rather than \;...that will run tar with as many filenames as possible, rather than running tar once for each filename. – cas May 18 '16 at 3:17

Assuming you are using GNU tar, you can say:

tar --transform 's/.*\///' -xzf archive.tgz

This will strip everything up to the last slash from the file names.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.