I want to limit the number of files that get put into a tar ball and erease them after they've been inserted into the tarball and being indipendent from the current directory. I've tried this: tar -C ~/semios/tmp/ -cjvf ~/semios/tmp/test.tar.bz2 $(cd ~/semios/tmp/; ls *| head -5) | xargs rm -f but this leaves the files where they are as the verbose option of tar only prints the file name, not the whole path, I thought I could fix it with the -C argument but it doesn't look like it... any hints?


Assuming no evil chars (whitespace, newlines) in the paths:

... $(ls ~/semios/tmp/*| head -5) | xargs -d '\n' rm -f


... | { cd ~/semios/tmp/; xargs -d '\n' rm -f; }


tar -C ~/semios/tmp/ -cjvf ~/semios/tmp/test.tar.bz2 \
  $(cd ~/semios/tmp/; ls * | head -5 | 
    { while read file; do echo "$file"; rm -f "$file"; done;})


As xargs uses any blanks as delimiters by default, newline should be set as the only delimiter. But due to the $() this example would crash even with whitespace in the name.



$ tar -cjf test.tar.bz2 $(find ~/semios/tmp/ -name "*"| head -5 | xargs rm -f)

  • Have you tried that? – Hauke Laging May 3 '14 at 5:33
  • @HaukeLaging yes I have – stdcerr May 3 '14 at 6:09
  • That is really strange because if I run something like $(find ~/semios/tmp/ -name "*"| head -5 | xargs rm -f) then it expands to nothing. – Hauke Laging May 3 '14 at 7:28

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.