32
7
I'm trying to tar
a collection of files in a directory called 'my_directory' and remove the originals by using the command:
tar -cvf files.tar my_directory --remove-files
However it is only removing the individual files inside the directory and not the directory itself (which is what I specified in the command). What am I missing here?
EDIT:
Yes, I suppose the 'remove-files' option is fairly literal. Although I too found the man page unclear on that point. (In linux I tend not to really distinguish much between directories and files that much, and forget sometimes that they are not the same thing). It looks like the consensus is that it doesn't remove directories.
However, my major prompting point for asking this question stems from tar's handling of absolute paths. Because you must specify a relative path to a file/s to be compressed, you therefore must change to the parent directory to tar it properly. As I see it using any kind of follow-on 'rm' command is potentially dangerous in that situation. Thus I was hoping to simplify things by making tar itself do the remove.
For example, imagine a backup script where the directory to backup (ie. tar) is included as a shell variable. If that shell variable value was badly entered, it is possible that the result could be deleted files from whatever directory you happened to be in last.
@isync I seem to be experiencing --remove-files deleting directories on Ubuntu 14.04. Except in my case I don't want it to. Haha – Bradley Odell – 2016-12-07T01:22:32.973
Nicholas, your point that it adds danger to have to remove the directory tree in an extra step is absolutely valid. I think it should be possible to have this done safely by the archiver. I also believe this was the intention of the creators of GNU tar, at least it should have been ;-) – mit – 2011-11-14T14:58:58.447
2I've found that the --remove-files option does indeed remove the containing dir - at least on some platforms/in some versions - and in my case. Might be that in your case the remaining dir wasn't completely empty due to some files being modified after being tar'ed. – isync – 2012-04-24T19:23:05.187