I would like to extract from a tar file (20G) and exclude many files and folders during the restore. I know how to exclude one or few files/folders but my list is in hundreds which I want to exclude. Unfortunately, they are unique and I can not apply regular expression as well. Is there a way I can put all files and folders names in a file I want to exclude and then use tar.

tar xvfz mvs-2017-06-29-1753.tar.gz -C / *usr/MVS*  --exclude=/usr/MSV/GL

You can see above, I can exclude one folder GL, but if I specify all the other files and folders on the command line, it wont fit. I want to put all my exclude files in a text file and then use in the command argument so that tar can read from the file instead of command line, especially the files I want to exclude. Please provide some direction.

2 Answers 2


You should use the --exclude-from=exclude.txt where exclude.txt is your list of files/folders that you want to exclude. The short option is -X and I would recommend you checking the man page: tar(1) man page

  • Thanks a lot Georgi, let me try this --exclude-from=exclude.txt
    – Muhammad
    Jan 24, 2018 at 16:15
  • That answer works but by the time he gets done inputting all of the directories and folders that he wants to exclude into a file, he could have long ago just used the --exclude option with each file and directory to effectively do the same thing. Both of our answers do the same thing with the same amount of work but only in different ways. Jan 24, 2018 at 16:18
  • I tried --exclude-from=exclude.txt and -X=exclude.txt and did not work, getting error. I am using Linux version 3.10.0-693.5.2.el7.x86_64 ([email protected]) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) ) #1 SMP Fri Oct 13 10:46:25 EDT 2017
    – Muhammad
    Jan 24, 2018 at 16:33
  • Can you provide us with the complete command as well as its error output? Jan 24, 2018 at 16:39
  • 2
    Perfect George, my mistake, worked like a charm. Thank you so much, excellent community with great, kind, and helping professionals.
    – Muhammad
    Jan 24, 2018 at 16:42

What do you mean it won't fit? You can just add the other exclude options for the files or directories you want to exclude:

tar xvfz mvs-2017-06-29-1753.tar.gz -C /usr/MVS --exclude=/usr/MSV/GL --exclude=/usr/directory2 --exclude=/usr/directory2/file1

And so on and so forth.

  • Thanks Nasir, but my exclude list contains hundreds of items (files and folders)
    – Muhammad
    Jan 24, 2018 at 16:31
  • I understand and I upvoted Georgi's answer. Either way, you've got your work cut out for you. You'll have to filter out and enter those hundreds of directories into a text file which is just as much work as entering each one onto the command line. Jan 24, 2018 at 16:33
  • Thanks but when I used --exclude-from=exclude.txt and getting this error tar: exclude.txt: Not found in archive. Please note that exclude.txt is available where I am running tar
    – Muhammad
    Jan 24, 2018 at 16:39
  • 1
    @NasirRiley There is a limit to how big a command line can get, so using a file can be necessary, if not easier.
    – jpaugh
    Jan 24, 2018 at 19:02
  • I see. I'll take note of that. Jan 24, 2018 at 19:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .