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.
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 --exclude="/home/user/public_html/tmp" /home/user/public_html/
Be aware that the exclude
argument:
1- Should be used with a =
, like this: --exclude=PATTERN
2- Expects a pattern (as the user Don Dilanga pointed out), not a directory, though a directory will work well as a pattern if it's long enough to not match any single files.
3- Has to be placed before the source directory. (as pointed out by kghbln)
How to exclude folders in DU and TAR commands
With GNU tar and du from GNU coreutils with absolute paths:
#!/bin/bash
mainLocation='/data/media/0/!Temp/!Test'
ex1='/data/media/0/!Temp/!Test/bellota'
ex2='/data/media/0/!Temp/!Test/Xiaomi'
tarArchiveLocationWithName='/tmp/big-files.tar'
# get size in bytes without excludes
size=$(du -sb --exclude="$ex1" --exclude="$ex2" "$mainLocation" | awk '$0=$1')
# create tar without excludes
tar -C / --exclude="$ex1" --exclude="$ex2" -c -P "$mainLocation" | pv -s "$size" > "$tarArchiveLocationWithName"
How to exclude specific files with the tar command?
From @arkascha 's answer :
find dir/ -type f | grep -v "^dir/TODO.txt" > files.txt
then
tar -czf dir.tar.gz -T files.txt
From the first line, there are 2 tricks to pay attention to :
- The
-type f
option. If not put, directories will be included infind
's result. This is bad, because it would include each file as many times as their depth in the file hierarchy. - The
^
ingrep
's regex : it ensures that we're excluding the pattern from the begining of the file hierarchy
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"
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
.
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.....
tar folder and exclude all subfolders, then tar to specific path
first, use find
to find the files meeting your criteria:
find ~/Desktop -type f -maxdepth 1
then pipe it to tar, using -T
( or --files-from
) to tell tar
to get the list of files from stdin
:
find ~/Desktop -type f -maxdepth 1 | \
tar -T - cvf r.tar
Excluding specific directory in tar using --exclude
tar cvfz ../foo.tar.gz --exclude='^./node_modules' .
since --exclude
takes a pattern. It remains to be seen if the '^' as beginning-of-line match is valid for your version of tar
.
Related Topics
Using Awk to Count the Number of Occurrences of a Word in a Column
Linux Default Behavior of Executable .Data Section Changed Between 5.4 and 5.9
How to Ensure Only One Instance of a Bash Script Is Running
How to Compile a 32-Bit Binary on a 64-Bit Linux Machine With Gcc/Cmake
How to Remove ^[, and All of the Escape Sequences in a File Using Linux Shell Scripting
How Are the Fs/Gs Registers Used in Linux Amd64
Expanding a Bash Array Only Gives the First Element
How to Know the Script File Name in a Bash Script
How to Access Physical Addresses from User Space in Linux
Asynchronous Io Io_Submit Latency in Ubuntu Linux
Performing Http Requests With Curl (Using Proxy)
Appending a Line to a File Only If It Does Not Already Exist
Sorting Multiple Keys With Unix Sort
How to Pass Command Output as Multiple Arguments to Another Command