Tar with Absolute Paths

I recognize the tar interface is not really designed with absolute paths in mind, but I find absolute paths generally easier to work with when scripting.

Indeed, when writing a fabric script that requires sudo on a remote server, you need to use absolute paths because of an issue using sudo with the cd command.

So here’s a simple example of using absolute paths with tar:

# set up dir to tar and zip
mkdir -p ~/tar_me/subdir
touch ~/tar_me/{file1.txt,file2.txt}
touch ~/tar_me/subdir/{file1.txt,file2.txt}

# test input/output directories
mkdir /tmp/tar_in
mkdir /tmp/tar_out

# create zipped tar using absolute paths
tar -vczf /tmp/tar_in/tar_me.tgz -C /home/me/tar_me

# unzip tar using absolute paths
tar -xvf /tmp/tar_in/tar_me.tgz -C /tmp/tar_out/

Before: tree /home/me/tar_me

/home/me/tar_me/
|-- file1.txt
|-- file2.txt
+-- subdir
	|-- file1.txt
	+-- file2.txt

After: tree /tmp/tar_out/tar_me

/tmp/tar_out/tar_me
|-- file1.txt
|-- file2.txt
+-- subdir
	|-- file1.txt
	+-- file2.txt

References

Advertisements
Tar with Absolute Paths

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s