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.
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.