Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I'm trying to create a tar archive of a folder thisfolder but want to omit a sub-folder named notthisfolder. Can someone show me how to do this? I've tried to tar'ing the entire folder and removing the one I want to omit but that particular folder is 17gb.

\thisfolder
  file1
  file2
  \notthisfolder
    omitfile1
    omitfile2
  \butincludethisfolder
    includefile1
    infludefile2

I was using tar --exclude='notthisfolder' -zcvpf archive.tar /thisfolder/

share|improve this question
    
Can you post your tar script? –  troylatroy Mar 5 at 23:03
    
@troylatroy, I'm not sure exactly but the tar script I'm using is only omitting files in notthisfolder but everything else in its subfolder is being included... =( –  sam yi Mar 5 at 23:25

3 Answers 3

Your command is almost correct. Use

 tar cvfzp achive.tar.gz --exclude notthisfolder thisfolder/
  1. Note that the --exclude notthisfolder option goes after the archive name.

  2. Cosmetic comment: when using the z option, you are using gzip to compress your tarball, so name the archive archive.tar.gz and not archive.tar.

  3. Also, /thisfolder/ is an absolute path and points to the folder thisfolder inside the root directory /. For specifying a relative path (to the current working directory), use thisfolder/ without the leading slash /.

  4. As a little tidbit, you don't need to prefix the cvfzp options with a -. One less character to type!

Works for me ;)

$ tree
.
└── thisfolder
    ├── butincludethisfolder
    │   ├── includefile1
    │   └── includefile2
    ├── file1
    ├── file2
    └── notthisfolder
        └── notthisfile

3 directories, 6 files

$ tar cvfzp achive.tar.gz --exclude notthisfolder thisfolder/
thisfolder/
thisfolder/butincludethisfolder/
thisfolder/butincludethisfolder/includefile1
thisfolder/butincludethisfolder/includefile2
thisfolder/file1
thisfolder/file2
share|improve this answer

It looks like you syntax is wrong.

It should be more like this:

tar -zcvpf /where/you/store/backups/archive.tar /thisfolder --exclude "/notthisfolder" --exclude "/notthisfolder/omitfile1" --exclude "/notthisfolder/omitfile2"

or if the omit files are all in /notthisfolder

tar -zcvpf /where/you/store/backups/archive.tar /thisfolder --exclude "/notthisfolder"

share|improve this answer

From man tar:

 --exclude=PATTERN
       exclude files, given as a PATTERN

So, given a directory like this:

terdon@oregano foo $ tree
.
└── thisfolder
    ├── butincludethisfolder
    │   ├── includefile1
    │   └── includefile2
    └── notthisfolder
        ├── omitfile1
        └── omitfile2

You could run the following tar command to get what you want:

tar -cvzf foo.tar --exclude notthisfolder thisfolder/

That will create tar.foo which contains:

$ tar xvzf foo.tar 
thisfolder/
thisfolder/butincludethisfolder/
thisfolder/butincludethisfolder/includefile2
thisfolder/butincludethisfolder/includefile1
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.