M BUZZ CRAZE NEWS
// news

How do I use tar to exclude all files of a certain directory?

By Emma Johnson

A client uploads files to a development server. When it's time to upload the project to a production server, those files are no longer needed, and we'd like to exclude them from the tar file we'll eventually push to production. However, we still want to keep the directory in tact, it's only the files that are not needed

Using Ubuntu 16 and tar 1.28, we've tried:

tar -vczf archive.tar.gz . --exclude=wp-content/uploads/wpallimport/files/*
tar: Cowardly refusing to create an empty archive

We've also tried enclosing exclude= parameter in single and double quotations, no luck.

tar -vczf archive.tar.gz . --exclude='wp-content/uploads/wpallimport/files/*'
tar: Cowardly refusing to create an empty archive

The official website shows --exclude without equals sign

tar -vczf file.tar.gz --exclude 'directory/*' . and gives the same error.

How is this done?

2

4 Answers

The usual gotcha with tar's --exclude option is that it is relative to the directory argument e.g. given

$ tree dir
dir
└── subdir └── subsubdir ├── file1 ├── file2 └── file3
2 directories, 3 files

then

$ tar cvf dir.tar.gz --exclude='/dir/subdir/subsubdir/*' dir
dir/
dir/subdir/
dir/subdir/subsubdir/
dir/subdir/subsubdir/file1
dir/subdir/subsubdir/file2
dir/subdir/subsubdir/file3

fails to exclude the contents of subsubdir (it's trying to exclude dir/dir/subdir/subsubdir/*, which doesn't match anything); what you want is

$ tar cvf dir.tar.gz --exclude='subdir/subsubdir/*' dir
dir/
dir/subdir/
dir/subdir/subsubdir/

AFAIK the order doesn't matter except that the output file must immediately follow the f option.

5

Let's say you have a directory named "top-dir" with subfolders "dir/subdir". If you want to exclude content of a directory "dir/subdir" but not the directory itself, the command line that actually works is tar -zcp --exclude='dir/subdir/*' -f archive.tgz top-dir.

You have to use the -f switch after the --exclude one and before the archive file name.

2

Success Case:

  1. If giving full path to take backup, in exclude also should be used full path.

    tar -zcvf /opt/ABC/BKP_27032020/backup_27032020.tar.gz --exclude='/opt/ABC/csv/*' --exclude='/opt/ABC/log/*' /opt/ABC
  2. If giving current path to take backup, in exclude also should be used current path only.

    tar -zcvf backup_27032020.tar.gz --exclude='ABC/csv/*' --exclude='ABC/log/*' ABC

Failure Case:

  1. If giving currentpath directory to take backup and full path to ignore,then it won't work

    tar -zcvf /opt/ABC/BKP_27032020/backup_27032020.tar.gz --exclude='/opt/ABC/csv/*' --exclude='/opt/ABC/log/*' ABC

Note: mentioning exclude before/after backup directory is fine.

Put directories you don't want (for example dir_A and dir_B) in a file:

echo dir_A > FILE
echo dir_B >> FILE
tar -cvf file_name.tar --exclude-from=FILE directory/
0

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy