Sometimes we need to remove or delete the files after creating the tar archive to increase the free space. Use the tar command with –remove-files option to remove/delete the files after creating the tar archive.
List the files
[root@dbappweb logs_backup]# ll total 1015088 -rw-r--r-- 1 root root 810695 Dec 31 04:00 access_log.4 -rw-r--r-- 1 root root 311080 Dec 31 04:00 deflate_log.4 -rw-r--r-- 1 root root 3742 Dec 31 04:02 error_log.4 -rw-r--r-- 1 root root 89191119 Dec 31 04:02 ssl_access_log.4 -rw-r--r-- 1 root root 845278 Dec 31 04:00 ssl_error_log.4 -rw-r--r-- 1 root root 89191119 Dec 31 04:02 ssl_access_log.4
Create a tar archive using the tar command
[root@dbappweb logs_backup]# tar -cvzf dbappweb_31Dec2017.tar.gz *.4 --remove-files access_log.4 deflate_log.4 error_log.4 ssl_access_log.4 ssl_error_log.4 ssl_request_log.4
tar created and original files removed
[root@dbappweb logs_backup]# ll total 221676 -rw-r--r-- 1 root root 9184120 Feb 15 10:14 dbappweb_31Dec2017.tar.gz
You can see all files under newly created tar by using the command tar -tvzf
[root@dbappweb logs_backup]# tar -tvzf dbappweb_31Dec2017.tar.gz -rw-r--r-- root/root 810695 2017-12-31 04:00:07 access_log.4 -rw-r--r-- root/root 311080 2017-12-31 04:00:07 deflate_log.4 -rw-r--r-- root/root 3742 2017-12-31 04:02:21 error_log.4 -rw-r--r-- root/root 89191119 2017-12-31 04:02:19 ssl_access_log.4 -rw-r--r-- root/root 845278 2017-12-31 04:00:10 ssl_error_log.4 -rw-r--r-- root/root 99193865 2017-12-31 04:02:19 ssl_request_log.4
To extract the files from tar use the command tar -xvzf
[root@dbappweb logs_backup]# tar -xvzf dbappweb_31Dec2017.tar.gz