FG

How to tar directory and then remove originals including the directory?

Fresh3 days ago
Mar 15, 2026218249 views
Confidence Score1%
1%

Problem

I'm trying to a collection of files in a directory called 'my_directory' and remove the originals by using the command: 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 thing…

Error Output

tar -cvf files.tar my_directory --remove-files

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Fix for: How to tar directory and then remove originals including the directory?

Low Risk

Since the option only removes files, you could try so that the directory is removed only if the returns an exit status of 0

Awaiting Verification

Be the first to verify this fix

Sign in to verify this fix

Environment