I have a magento installation I would like to tar. As some of you may now, in the root directory of a Magento installation is a directory called "media". This is the directory where all product images are stored.

I want to tar the entire Magento installation, without this directory. As this is not needed when developping, only on live server.

I have experienced with the --exclude option for tar, however, this excludes all directories called "media". I only want to skip the subdirectory media in the root.

Example root directory I want to tar:

/app /app/media -> I do not want to exclude this /lib /js /media -> I want to exclude this /skin ...etc

Normally I use tar -pcvzf filename.tar . to compress the whole directory. What would be the command to use to exclude the root media directory?


You want to use anchors in the --exclude regex to limit the pattern matches. If you do

tar -pcvzf filename.tgz --exclude "^media" .

that will exclude ./media but not ./app/media. The '^' at the beginning of the pattern

| improve this answer | |
  • Did not think about using regex, but this should work. Thanks! – Patrick Steenks Aug 3 '15 at 19:33

See here: https://stackoverflow.com/questions/984204/shell-command-to-tar-directory-excluding-certain-files-folders

The short version is that you can exclude the full path to the directory like so:

tar -pcvzf file.tar.gz --exclude=/media
| improve this answer | |
  • This would be correct if I would like to exclude /app/media. But I want to exclude /media only. User302933 seems to be right. – Patrick Steenks Aug 3 '15 at 19:33
  • Ah. I was mistaken about what you did and did not want to include. You probably should have put them on separate lines. Assuming you wanted to exclude /lib, /js, and /media, you would want --exclude=/lib --exclude=/js --exclude=/media. I edited my original answer to match the question. – BamaPookie Aug 3 '15 at 19:58
  • Not really correct, as this will also exclude directories like /app/media. This is the whole problem. As said, User302933 has the correct answer. – Patrick Steenks Aug 4 '15 at 8:32

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.