Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to change the command I'm actualy using to put files in a tar archive in order to achieve this:

1) Remove directories tree from the archive (actually the .tar.gz file mantains the tree of the files. I want the files to be "parent").

2) Remove the files used to create the archive and leave only the new .tar.gz

BACKUP_DEST=/home/backup/db/`date +\%G-\%m-\%d`
tar -czvf ${BACKUP_DEST}/files.tar.gz ${BACKUP_DEST}
share|improve this question
OK, so what's stopping you from doing so? We're a question-and-answer site, and I'm not really seeing a question here. – womble Jul 17 '12 at 22:56
I use this command in a cron job. I'd like to have the target .tar.gz file without directories tree and remove the original files used to create the archive. Automatically.. – MultiformeIngegno Jul 17 '12 at 23:08
"remove the original files used to create the archive." Huh? – thinice Jul 17 '12 at 23:14
In /home/backup/db I have file "a", file "b" and file "c". I use tar to create /home/backup/db/files.tar.gz. When creating the archive I'd like to remove also ORIGINAL "a", "b" and "c". – MultiformeIngegno Jul 17 '12 at 23:18
So edit your question to include an actual question. – womble Jul 18 '12 at 0:07
up vote 3 down vote accepted

The tar command has a little-known option named --remove-files, which is designed pretty much for this specific use-case. There is a known bug in the interaction between --remove-files and --append, but since you don't appear to be using that, you should be fine.

share|improve this answer
Thanks for the reply. :) For the 1st question I found the -C command.. I tried with: tar -czvf ${BACKUP_DEST}/files.tar.gz ${BACKUP_DEST} -C but a dummy archive is created.. – MultiformeIngegno Jul 18 '12 at 0:33

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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