Exclude Certain Files When Creating A Tarball Using Tar Command

How can I keep out certain files when creating a tarball? For example:


How do I exclude zyz and abc file while using a tar command?

The GNU version of the tar archiving utility has –exclude and -X options. So to exclude abc and xyz file you need to type the command as follows:
$ tar -zcvf /tmp/mybackup.tar.gz --exclude='abc' --exclude='xyz' /home/me

If you have more than 2 files use -X option to specify multiple file names. It reads list of exclude file names from a text file. For example create a file called exclude.txt:
$ vi exclude.txtAppend file names:

Save and close the file. This lists the file patterns that need to be excluded. Now type the command:
$ tar -zcvf /tmp/mybackup.tar.gz -X exclude.txt /home/me


  • -X file.txt :exclude files matching patterns listed in FILE file.txt

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 7 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
7 comments… add one
  • will Aug 4, 2008 @ 20:13

    Is there a way to do the opposite? That is, tar up only files of a certain type and not everything else? Would like to preserve directory structure when doing so. Thanks!

  • aceqbaceq Mar 13, 2009 @ 13:30

    common style using “tar+exclude” suggested in internet sources is like:

    tar \
    – -exclude… \
    – -exclude… \
    – -exclude… \

    – -exclude… \
    -cvpzf home_bup.tgz /home/username

    but i think it is a very annoying method.

    I suggest next:

    for fedora core

    tar cvpzPf /tmp/backup.tar.gz –exclude={/proc/*,/sys/*,/tmp/*,/dev/*} /

    for Debian

    tar cvfpP /tmp/debian2.tar –exclude={”/proc/*”,”/sys*”,”/tmp/*”,”/home/user/*”} /

    and more common if i need backup using ssh (ssh+nice+tar)

    ssh root@ “cd /;nice -n 10 tar cvpP –exclude={”/proc/*”,”/sys*”,”/tmp/*”,”/home/user/*”} /”>backup.tar.gz

    and before “exclude” we have two “-” not one.

  • Tokyo Joe Jun 24, 2009 @ 9:02

    Using the exclude.txt file is the coolest thing since sliced bread. Thank you very much!!

  • cmo-0 Sep 2, 2009 @ 20:13


    you can use the ‘T’ option with ‘ -T selectedfiles.txt’ where selectedfiles.txt contains paths to the files you want to backup only.

  • chz Jan 13, 2012 @ 19:41

    there’s a typo at the beginning of your article: change “execute” to “exclude”

  • Pavche Jan 15, 2015 @ 14:35

    Thanks a lot for the exclude list.

  • Saddam ZEMMALI Mar 9, 2017 @ 11:02

    To search for all .zip files in current directory and all sub-directories try: this
    #find ./foldername -type f -name ‘*.zip’

    To Exclude all .zip files in current directory and all sub-directories try this:
    # find ./foldername -type f -name ‘*.zip’ > exclude.txt

    To Create tar Archive excluding all .zip files in current directory and all sub-directories try this:
    # tar -zcvf backup_foldername.tar.gz -X exclude.txt ./foldername

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum