How to Remove Files After Tar Archive in Ubuntu
This guide provides a concise explanation of the tar command option used to delete source files upon successful archiving within the Ubuntu operating system. It details the specific flag required to automate this process and offers a practical example of its usage. Understanding this option helps users manage disk space efficiently while ensuring data is preserved within the compressed file.
The flag that tells tar to remove files after adding them to the
archive is --remove-files. When you create a tarball, this
option instructs the utility to delete the original input files from the
filesystem once they have been written to the archive successfully. This
is particularly useful when moving data to cold storage or freeing up
space on a full drive immediately after backup.
To use this flag, include it in your creation command. The standard syntax looks like this:
tar --create --file=backup.tar --remove-files /path/to/directory
You can also use the shorthand -c and -f
flags alongside the long option for brevity:
tar -cf backup.tar --remove-files /path/to/directory
It is critical to verify the archive before relying on this flag. Since the original files are deleted permanently, ensure the tar command completes without errors. If the archive creation fails, the files may still be removed depending on the execution flow, so testing in a safe environment is recommended before using this on critical data. Always confirm the integrity of the resulting tar file before assuming the source files are safely stored.