I have some directories with some files
dir_archive/somedir1
dir_archive/somedir2
dir_archive/somedir3
dir_archive/mydir
dir_archive/mydir/excludedir1
dir_archive/mydir/excludedir2
dir_archive/mydir/excludedir3
dir_archive/mydir/many_other_directories...
dir_archive/mydir/my_archive_dir
I want create tar(gz) archive dir_archive.tar.gz with all files and directories exclude
dir_archive/mydir/excludedir1
dir_archive/mydir/excludedir2
dir_archive/mydir/excludedir3
dir_archive/mydir/many_other_directories...
but include
dir_archive/mydir/my_archive_dir
I dont want use --exclude
for each directory
tar cvfz dir_archive.tar.gz --exclude=dir_archive/mydir/excludedir1 --exclude=dir_archive/mydir/excludedir2 --exclude=dir_archive/mydir/excludedir3
I try use --add-file
, but it doesn't work:
tar cvfz dir_archive.tar.gz --exclude=dir_archive/mydir --add-file=dir_archive/mydir/my_archive_dir dir_archive
Exists some simple way? Thanks
For example, to create an archive with all the contents of the directory 'src' except for files whose names end in '.o', use the command ' tar -cf src. tar --exclude='*.o' src '. You may give multiple ' --exclude ' options. Causes tar to ignore files that match the patterns listed in file .
To exclude multiple directories, OR them between parentheses. And, to exclude directories with a specific name at any level, use the -name primary instead of -path .
We can exclude directories by using the help of “path“, “prune“, “o” and “print” switches with find command. The directory “bit” will be excluded from the find search!
Order matters:
$ mkdir -p dir_archive/{somedir{1..3},mydir/{excludedir{1..3},otherone,my_archive_dir}}
$ touch dir_archive/{somedir{1..3},mydir/{excludedir{1..3},otherone,my_archive_dir}}/dir_file.txt
$ tar cvzf dir_archive.tar.gz dir_archive/mydir/my_archive_dir* --exclude "dir_archive/mydir/*" dir_archive
dir_archive/mydir/my_archive_dir/
dir_archive/mydir/my_archive_dir/dir_file.txt
dir_archive/
dir_archive/mydir/
dir_archive/somedir2/
dir_archive/somedir2/dir_file.txt
dir_archive/somedir1/
dir_archive/somedir1/dir_file.txt
dir_archive/somedir3/
dir_archive/somedir3/dir_file.txt
What this is doing in essence is being more explicit up front, then applying the exclude to the parent directory only.
Stolen shamelessly from https://unix.stackexchange.com/a/417707/420501
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With