Exclude Options for Tar +

Quick thought…

You can pretty much exclude a file, folder or specific pattern when using tar:


# tar cvfp 052613tar.tar /my/path/Example.com_DIR --exclude=/my/path/Example.com_DIR/images

 
or tar everything in the current directory, but exclude two specific files

# tar cvpf 052613tar.tar * --exclude=index.html --exclude=myimage.png

 
or

tar cvpf 052613tar.tar * --exclude='file1' --exclude='pattern*' --exclude='file2'

  


p.s some tar basics…

Creating a tar file

tar -cf tarfile.tar file1.txt

-c – create
-f or –file – immediately followed by a file or device will tell tar where to create the tar file and the files or directories to package

 
Extracting a tar file
tar -xf tarfile.tar
-x – extract
-f or –file
 

Add Verbose output
tar -xfv tarfile.tar
-v verbosely lists the files processed
 

List files in a tar
tar -tf file.tar
 

Add file to tar
tar -rvf file.tar file2.txt
file2.txt

 

Add gzip compression
tar -cvzf tarfile.tar.gz file1.txt file2.txt
file1.txt
file2.txt

 

Add bzip2 compression
tar -cjvf tarfile.tar.bz files_dir
files_dir/
files_dir/file3.txt
files_dir/file4.txt

 

Extract tarballs with compression
tar -xjvf tarfile.tar.bz files_dir
files_dir/
files_dir/file3.txt
files_dir/file4.txt

 

Extract a tarball without replacing old files
tar -czf tarfile.tar.gz file1.txt file2.txt
rm file2.txt && echo "file2 was removed" >> file1.txt
tar -xvzkf tarfile.tar.gz
file1.txt
file2.txt
cat file1.txt
file2 was removed

 

Tarpipe/TarCopy
This sends the packaged files to stdout rather than to a file. This is used as an alternative to ‘cp’ because it’s simply faster.
tar -cf - file* | (cd ../files_copied/ && tar -xf -)

To copy the files to a remote server
tar -cf - file* | ssh remote-server "(cd /files_copied/ && tar -xf -)"
 

g33kadmin

I am a g33k, Linux blogger, developer, student and Tech Writer for Liquidweb.com/kb. My passion for all things tech drives my hunt for all the coolz. I often need a vacation after I get back from vacation....