Shell Command to Tar Directory Excluding Certain Files/Folders

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 :

  1. The -type f option. If not put, directories will be included in find's result. This is bad, because it would include each file as many times as their depth in the file hierarchy.
  2. The ^ in grep'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



Leave a reply



Submit