Linux tar exclude directory

[Linux] : How to exclude directory when using “tar” shell command

In day-to-day activities administrators needs to perform regular backups on their Linux servers. Being myself an administrator, i would recommended “tar”, the simple and the best tool. Backup doesn’t mean all the files and folders need to be backed up ! Sometimes we may have to exclude directories like template cache, log files, cache, temporarily created files, gallery directory etc., So in this article, we will see how to exclude certain directories and certain patterns even !

1. tar –exclude “directory”

Note: When excluding directories, make sure NOT to use the trailing slash(/) at the end of the directory name.

I have wasted much time in exploring this. So requesting you to not to waste time and follow the good procedure to get the work done soon.

Bad Practice :

tar -cvf backup.tar --exclude="public_html/template/cache/" public_html/

Good Practice :

tar -cvf backup.tar --exclude="public_html/template/cache" public_html/

2. tar –exclude Multiple directories

To exclude multiple directories you can either provide directories separately or by listing each directory seperated by comma and encased in .

Method 1 :

tar -cvf backup.tar --exclude="public_html/template/cache" --exclude="public_html/images" public_html/

Method 2 :

tar -cvf backup.tar --exclude= public_html/

3. tar –exclude directories from a File

List all the directories to be excluded into a file and use this list to exclude directories during tar.

Method 1 :

tar -cvf backup.tar -X exclude_directory.txt public_html/

Method 2 :

tar -cvf backup.tar --exclude-from=exclude_directory.txt public_html/

exclude_directory.txt Contains :

public_html/template/cache public_html/images

4. tar –exclude certain patterns

Sometimes we might find multiple pattern in different folders and we would not be interested only on that pattern. So here we see how to exclude particular pattern.

To exclude directory with particluar pattern :

tar -cvf backup.tar --exclude="log" --exclude="cache" public_heml/

To exclude files with patterns containing preceding and trailing characters :

tar -cvf backup.tar --exclude="*.log" --exclude="cache*" public_heml/

Was this article helpful?

About The Author

Author Profile

Ramya Santhosh

is a Web Designer and content creator. A freelance writer on latest trends in technology, gadget reviews, How to’s and many more.

Читайте также:  Вывод дерева папок linux
  • How to shrink a qcow2 Windows VM image on Linux
  • [Linux]: How to Change Hostname on CentOS/RHEL – 7/8
  • No such file or directory c++ Error [CentOS]
  • How to configure Open vSwitch bridge for OpenStack
  • Too many connections for neutron-db-manage [MySQL]
  • How to manually install higher version of PIP for Python v2.7

Comments Leave a Comment

It took me a week off and on to figure out why exclude option wasn’t working. I wanted to exclude all the hidden files and directories from being included in the archive. I generated the file exclude with the ls -a | egrep “^\.” > exclude (list all the files & directories including hidden files, then keep those that start with a . and save results in the exclude file). When I used -X exclude with tar nothing was backed up. Finally, I took a real look at exclude and the first line was just a . The light went off, I had just told tar to exclude the current directory, so no backup. Should have use ls -A (not -a) when I generated exclude to not include . and .. in the output. Live and learn!

Best explanation I read online, simple and definitely works, at least for me :), advise to whose who read this “READ THE ADVICE ABOVE” Thanks a million and keep up the good help

Источник

Exclude directory while using tar

Did you add a . at the end of your command, otherwise you have nothing to add to the archive. -C changes the directory, and . is the current directory you just changed to that you want added.

2 Answers 2

After reading all this good answers for different versions and having solved the problem for myself, I think there are very small details that are very important, and rare to GNU/Linux general use, that aren’t stressed enough and deserves more than comments.

So I’m not going to try to answer the question for every case, but instead, try to register where to look when things doesn’t work.

IT IS VERY IMPORTANT TO NOTICE:

  1. THE ORDER OF THE OPTIONS MATTER: it is not the same put the —exclude before than after the file option and directories to backup. This is unexpected at least to me, because in my experience, in GNU/Linux commands, usually the order of the options doesn’t matter.
  2. Different tar versions expects this options in different order: for instance, @Andrew’s answer indicates that in GNU tar v 1.26 and 1.28 the excludes comes last, whereas in my case, with GNU tar 1.29, it’s the other way.
  3. THE TRAILING SLASHES MATTER: at least in GNU tar 1.29, it shouldn’t be any.
Читайте также:  Установка monodevelop astra linux

In my case, for GNU tar 1.29 on Debian stretch, the command that worked was

tar --exclude="/home/user/.config/chromium" --exclude="/home/user/.cache" -cf file.tar /dir1/ /home/ /dir3/ 

The quotes didn’t matter, it worked with or without them.

I hope this will be useful to someone.

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 .

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 Files and Directories While Creating Tar File

Don’t want all the files in your tarball? Here’s how to exclude files and folders while creating tar archive file.

The tar command is used for creating archive files from various files and folders.

It’s quite simple when you have all the required files in one place. But what if you want to put some files or folders in the tar file and exclude some?

One way would be to copy those required files into a new directory and then proceed with the standard procedure, but it is not an efficient way.

Instead, you can tell the tar command to exclude the undesired files in this manner:

tar --exclude="File_to_exclude" [options] [archive_name] [path]

By the way, that’s not the only way to do it. Let me show you a couple of methods here.

Читайте также:  Поменять владельца файла linux

But before I jump to the guide, I’d like to show you the directory containing multiple files and sub-directories which I’m going to utilize in this guide:

use tree command in linux

Now, let’s proceed with the first method.

Method 1. Using -exclude option

To use the -exclude option, you’d need to follow the given command syntax:

tar --exclude="File_to_exclude" [options] [archive_name] [path]

For example, I’ll be creating a tarball named IHateC excluding HelloWorld.c file:

tar --exclude='HelloWorld.c' -zcvf IHateC.tar.gz .

exclude files while creating tar files in linux using --exclude option

First, let me break down the used options here:

  • z uses the gzip utility to compress files in the archive.
  • c is responsible for creating a new archive file.
  • f allows users to specify the name of the new archive.
  • v (verbose) gets us a list of processed files and directories to create archive files.

And the . used at the end of the command indicates I want to utilize the current working directory.

To check the contents of the tar file, you can use the tar command with the -tf option:

list contents of tar file in linux

but what about excluding multiple files and directories? Here you go.

Excluding multiple files and directories

To exclude multiple files and directories, you can use the chain of —exclude .

For example, I’ll disclude Bash.sh and the subdirectory named Sub-Directory-1 :

tar --exclude='Bash.sh' --exclude='Sub-Directory-1' -zcvf NoDir.tar.gz .

exclude multiple files and directories while creating tar file in linux

But this command can get too long if you’re supposed to exclude various files. And if they have the same extension, things can get a lot easier!

Use file extension to exclude files

You just have to pair the file extension with the —exclude and it will get your job done.

For example, I want to exclude every .mp3 file while creating tarball, then I’d have to follow the given command:

tar --exclude='*.mp3' -zcvf NoMp3.tar.gz .

Method 2. Using a text file to exclude files and directories

In this method, I’ll be creating a text file containing the names of files and directories that I want to exclude.

So I made a text file containing 3 filenames that I want to exclude named exclude.txt :

list contents of file using cat command in linux

So now I only have to use that text file to exclude files:

tar -zcvf NewFile.tar.gz --exclude-from="exclude.txt" .

use text file to exclude multiple files and directories while creating tar file in linux

Final Words

This was my take on how you can exclude files and directories while creating tar files in the easiest way possible.

And if you have any queries, you’re welcome to extract them in the comments!

Источник

Оцените статью
Adblock
detector