Excluding directory when creating a .tar.gz file
Try removing the last / at the end of the directory path to exclude
tar -pczf MyBackup.tar.gz /home/user/public_html/ --exclude "/home/user/public_html/tmp"
Shell command to tar directory excluding certain files/folders
You can have multiple exclude options for tar so
$ tar --exclude='./folder' --exclude='./upload/folder2' -zcvf /backup/filename.tgz .
etc will work. Make sure to put --exclude
before the source and destination items.
Tar: create archive exclude directories except one
One way would be first excluding the mydir
and then appending the my_archive_dir
tar cvf dir_archive.tar --exclude=dir_archive/mydir dir_archive
tar rvf dir_archive.tar dir_archive/mydir/my_archive_dir
gzip dir_archive.tar
Unfortunately appending doesn't work with zipped archives.
The --exclude
option takes a pattern as argument, so if the names of the dirs to be excluded are similar, you can avoid them and still include the archive dir
tar cvfz dir_archive.tar.gz --exclude=dir_archive/mydir/exclude* dir_archive
It is also possible to create a file with names of all the files that you want included and give that list to tar with option -T
or --files-from
(or in similar fashion list the files to be excluded and give the list with option -X
).
filelist.txt:
dir_archive
dir_archive/temp1
dir_archive/mydir
dir_archive/mydir/temp2
dir_archive/mydir/my_archive_dir
dir_archive/mydir/my_archive_dir/temp7
tar cvfz dir_archive.tar.gz --no-recursion --files-from filelist.txt
Exclude directory while using tar
You can't put the complete path into -C, if you want to tar the content of www
. Do this instead:
tar -pczf domain.com.tar.gz -C /var/www/domain.com/public_html/www .
That way 'www' is the directory to be tarred but omited itself by including it into the -C path. You would than later extract all files of the 'www' directory.
In addtion to your edit (exclude) it must look like this:
tar --exclude=tmp -pczf domain.com.tar.gz -C /var/www/domain.com/public_html/www .
EDIT
According to your wishes, I found a funny but working solution. You exclude the dirs you want with exclude (see the man page of your tar, there are some with --no-recurse
option, too) and you will have no ./
syntax at all:
ls /var/www/domain.com/public_html/www | xargs tar --exclude=tmp -C /var/www/domain.com/public_html/www -pczf domain.com.tar.gz
The way you give the filenames to the input, is the way tar is storing it. So it is even possible with -C
to store the files without ./
but you need to pipe the list of ls
with | xargs
to tar.....
Exclude common subdirectories when creating a tarball
Instead of manually typing --exclude 'root/a/.CC' --exclude 'root/b/.CC' ...
you can type $(find root -type d -name .CC -exec echo "--exclude \'{}\'" \;|xargs)
You can use whatever patterns find
supports, or even use something like grep inbetween find
and xargs
.
Extract tar archive excluding a specific folder and its contents
You can use '--exclude' to omit a folder:
tar -xf archive.tar -C /home/user/target/folder" --exclude="folderC"
tar/gzip excluding certain files
You need to use the --exclude
option:
tar -zc -f test.tar.gz --exclude='*.xdr' *
Exclude directories from tar with .tarignore file (#2)
So I was doing something wrong - incorrect order of the arguments. I also switched to --exclude-from
per @Cyrus' comment and it seems to work fine.
tar -zcvf cur.tar.gz --exclude-from=.tarignore /home/ubuntu/dirname
And a sample from the .tarignore
file:
# Ignore these paths
build/*
node_modules/*
.git/*
How do I tar a directory of files and folders without including the directory itself?
cd my_directory/ && tar -zcvf ../my_dir.tgz . && cd -
should do the job in one line. It works well for hidden files as well. "*" doesn't expand hidden files by path name expansion at least in bash. Below is my experiment:
$ mkdir my_directory
$ touch my_directory/file1
$ touch my_directory/file2
$ touch my_directory/.hiddenfile1
$ touch my_directory/.hiddenfile2
$ cd my_directory/ && tar -zcvf ../my_dir.tgz . && cd ..
./
./file1
./file2
./.hiddenfile1
./.hiddenfile2
$ tar ztf my_dir.tgz
./
./file1
./file2
./.hiddenfile1
./.hiddenfile2
Related Topics
Should I Rebuild a Dependent Lib After System Update
How to Use "Py" Instead of "Python" at the Command Line in Linux
How to Delete Multiple Files at Once in Bash on Linux
Linux Script to Kill Java Process
How to Convert an Ssl Certificate in Linux
Setting Environment Variable Globally Without Restarting Ubuntu
Rename All Files in Folder to Numbered List 1.Jpg 2.Jpg
Shell Script Error Expecting "Do"
Creating Symbolic Link: Protocol Error
Trying to Search Files from User Keyword in Bash
How to Shell Have More Than One Job in Linux
Are Message Queues Obsolete in Linux
List Files Over a Specific Size in Current Directory and All Subdirectories
How to Make Vim Play Typewriter Sound When I Write a Letter
Linux: How to Know the Module That Exports a Device Node