0

I have a directory with various python packages that I'd like to archive using tar. I need to exclude the numerous __pycache__ folders that get left behind by the build/testing process but using --exclude "__pycache__" will not achieve this. How can I get tar to ignore all subdirectories that match that pattern?

tar -cvf myarchive.tar -C ~/myproject package1/ package2/ --exclude "__pycache__"

To recreate:

me@me-laptop:~/myproject$ pwd
/home/me/myproject
me@me-laptop:~/myproject$ tree
.
├── package1
│   ├── include
│   └── __pycache__
└── package2
    ├── include
    └── __pycache__

6 directories, 0 files
me@me-laptop:~/myproject$ tar -cvf myarchive.tar -C ~/myproject package1/ package2/ --exclude "__pycache__"
package1/
package1/include/
package1/__pycache__/
package2/
package2/include/
package2/__pycache__/
  • I can't seem to reproduce this with a basic directory tree. Can you show us the directory structure you have on your system? – Haxiel Mar 19 at 13:07
  • added an example – ptr Mar 19 at 13:15
  • for reference, I'm currently on ubuntu 18.04 but this is also happening on centos7 – ptr Mar 19 at 13:18
  • Thanks for the update, but I still can't reproduce this. I'm on an RHEL 7 system with GNU tar 1.26 (which would be identical to your CentOS system). Copy-pasting the exact same command from your question works for me. – Haxiel Mar 19 at 13:34
  • my apologies, it does work on centos, i must have used a different flag the time it didn't work. So is it a difference between ubuntu and centos' tar versions? – ptr Mar 19 at 13:40
2

You need to add the --exclude switch at the beginning of the command, see this. This solution works on my openSUSE machine, I don't have other distros available right now.

host:/tmp # find test*
test1
test1/file1
test1/__pycache__
test1/__pycache__/file1-py
test1/include
test1/include/file1-incl
test2
test2/__pycache__
test2/__pycache__/file2-py
test2/include
test2/include/file2-incl
test2/file2

host: # tar --exclude='__pycache__' -cv test* -f testtar.tar
test1/
test1/file1
test1/include/
test1/include/file1-incl
test2/
test2/include/
test2/include/file2-incl
test2/file2
  • This works on both Ubuntu and Centos, odd that the centos version accepts the --exclude at the end of the line – ptr Mar 19 at 13:44

Your Answer

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

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