There are various archival tools available in Linux the most commonly used of which is tar (“tape archiver”). We shall cover tar as well as star which is very similar in terms of functionality but in some cases might be faster.
# tar -cf archive.tar foo bar directory1/ → creates tarfile with files and directory contents
# tar -xf archive.tar → extracts tarfile contents into current directory
# tar -tvf archive.tar → list tarfile contents
# tar -uf archive.tar foo bar directory1/ → update tarfile only with new/modified contents
# tar -df archive.tar → compare tarfile contents with filesystem’s
# tar -rf archive.tar directory1/ → appends (!updates) contents at end of tarfile
# tar -f archive.tar delete directory1/old/ → deletes from the tarfile those files and directories whose
. fullpath matches expression
# tar -cjf arch.tar dir1/ → creates tarfile and compresses it with bzip2
# tar -czf arch.tar dir2/ → creates tarfile and compresses it with gzip
# tar -cZf arch.tar dir2/ → creates tarfile and compresses it with compress
# tar -cf arch.tar selinux acls dir3/ → preserves SELinux and ACLs settings
# tar -cf arch.tar xattrs dir4/ → preserves extended attributes
# tar -cf arch.tar H pax dir5/ → changes header from default format gnu to pax but it
. could be oldgnu, posix, ustar or v7
# tar -cf arch.tar –exclude=”*.old” dir6/ → excludes files/directories marching regexp
Star is an archival utility similar to tar but with added functionality:
• it can handle pattern matching to decide its input files
• it has no filename length limitations
• it can handle the 3 times (ctime, atime & mtime)
• it has an sfind utility that can search files within an archived file
Let’s look at an example:
[root@rhce7 marc]# cd /var/www/html/
.
[root@rhce7 html]# ls -laZ
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 .
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 ..
.
[root@rhce7 html]# touch /var/www/html/file{1,2,3}
.
[root@rhce7 html]# ls -laZ
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 .
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 ..
-rw-r–r–. root root unconfined_u:object_r:httpd_sys_content_t:s0 file1
-rw-r–r–. root root unconfined_u:object_r:httpd_sys_content_t:s0 file2
-rw-r–r–. root root unconfined_u:object_r:httpd_sys_content_t:s0 file3
.
[root@rhce7 html]# star -xattr -H=exustar -c -f=test.star file{1,2,3}
star: 1 blocks + 0 bytes (total of 10240 bytes = 10.00k).
.
[root@rhce7 html]# mkdir /tmp/test
.
[root@rhce7 html]# cp test.star /tmp/test/
.
[root@rhce7 html]# cd /tmp/test
.
[root@rhce7 test]# star -x -f=test.star
star: current ‘file1’ newer.
star: current ‘file2’ newer.
star: current ‘file3’ newer.
star: 1 blocks + 0 bytes (total of 10240 bytes = 10.00k).
.
[root@rhce7 test]# ls -laZ
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 .
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 ..
-rw-r–r–. root root unconfined_u:object_r:httpd_sys_content_t:s0 file1
-rw-r–r–. root root unconfined_u:object_r:httpd_sys_content_t:s0 file2
-rw-r–r–. root root unconfined_u:object_r:httpd_sys_content_t:s0 file3
-rw-r–r–. root root unconfined_u:object_r:httpd_sys_content_t:s0 test.star
As we can see, using the “-xattr -H=exustar” (extended attributes + exustar headers) allows the transferability of SE Linux contexts. The -c flag means compress whereas -f=test.star states the name of the created archival file. We could also have used the option -acl -H=exustar to handle ACLs.
Other useful options to remember…
# to list the table of contents of the file in verbose mode…
root:~> star -tv -f=test.star
.
# to copy the directory tree /home/marc onto /home/fs either of the next 2 can be used…
root:~> (cd /home/marc; star -c .) | (cd /home/fs; star -xp)
root:~> star -c -C /home/marc . | star -xp -C /home/fs
.
# to do the same as above but with ACLs and SE Contexts…
root:~> star -c -xattr -H=exustar -acl -C /home/marc . | star -xp -acl -C /home/fs
.
# to extract all the files except those ending with “*.old”…
root:~> star -xp -V pat=* -f=star.test
.
# to extract all *.c files to $SRC, all *.obj files to $OBJ and all the rest to /tmp…
root:~> star -xp -C $SRC *.c -C $OBJ *.obj -C /tmp * -f=star.test
.
# to a rchive a list of files that is the output of a find command…
root:~> find / -name *.c -print | star -c -list=- -f=star.test