I need to unpack a big archive, but the problem is i don't have enough disc space for both compressed and extracted files. Is there option to delete compressed files from the archive right after unpacking? If so, how? Currently my files are compressed by tar command, but i probably can repack them to any other type on my other server.
-
Probably i should look for other way to transfer my files :)– Ivan OffOct 28, 2019 at 3:00
-
The problem that you will have is that many archives are compressed. Thus you will probably have a temporary file being created in the process of removing files from that archive. If you are just using tar, and are going from the last file in archive toward the first one, then in theory it could be done.– v010dyaOct 28, 2019 at 5:19
1 Answer
Note. This will not work, if your file is compressed! Do not even try!!
With that out of the way, let's do this.
I have created a simple script that goes through the tar archive backwards, extracts a file, and then removes it from the archive. I am unsure if this file will work with a directory structure inside of it, and i have only tested it with a "flat" tar archive.
The script is:
#!/bin/bash
archive="$1"
while true
do
file=$(tar --list -f tar.tar | tail -n 1)
if [ -z "$file" ]; then
break
fi
tar --extract -f "$archive" "$file" && tar --delete -f "$archive" "$file"
done
Warning: I have tested this. But i cannot guarantee that the script above will not skip some files, fail to work, or even potentially send make your computer into a toaster. Use at your own risk!
And here is my test:
:/mnt$ ls -l
total 2445456
-rwxrwxr-x 1 va va 201 окт 28 08:50 do.sh
-rw-rw-r-- 1 va va 2504140800 окт 28 08:45 tar.tar
:/mnt$ df -h |grep /mnt
tmpfs 3,0G 2,4G 684M 78% /mnt
:/mnt$ ./do.sh tar.tar
:/mnt$ df -h |grep /mnt
tmpfs 3,0G 2,4G 684M 78% /mnt
:/mnt$ ls -lh tar.tar
-rw-rw-r-- 1 va va 10K окт 28 08:53 tar.tar
The tar archive has only metadata inside of it at the end and can be deleted.