I want just stupid, simple thing. ZIP all the content of given directory.

What I get as result of:

zip -r /home/andi/MEDIA_DUMPS/xyz_media.zip /home/andi/xyz/mega_backend/mega_www/media

Is following:

➜  home  tree -L 5
└── andi
    └── xyz
        └── mega_backend
            └── mega_www
                └── media
                    ├── csv
                    ├── editor
                    └── img

while I want just this:

➜  media  tree -L 1
├── csv
├── editor
└── img


tar -cjf /home/andi/MEDIA_DUMPS/xyz_media.tar.gz -C /home/andi/xyz/mega_backend/mega_www/media .

based on: https://stackoverflow.com/questions/18681595/tar-a-directory-but-dont-store-full-absolute-paths-in-the-archive

  • 1
    cd /home/andi/xyz/backend/media first then run zip? – DavidPostill Nov 18 '14 at 9:07
  • not a solution. – andilabs Nov 18 '14 at 9:34
  • I was proposing it as a suggestion not a solution. If it doesn't work for you please explain why. – DavidPostill Nov 18 '14 at 9:38

How do I to zip only files and not the whole directory structure?

Use the -j (junk-paths) option.

zip: Package and compress (archive) files

      Store  just the name of a saved file (junk the path), and do not
      store directory names. By default, zip will store the full  path
      (relative to the current directory).


You may want to make a zip archive that contains the files in foo, without recording the directory name, foo.

You can use the -j option to leave off the paths, as in:

zip -j foo foo/*

Source zip: Package and compress (archive) files:

  • I got error: zip error: Nothing to do! (/home/andi/MEDIA_DUMPS/xyz_media.zip) – andilabs Nov 18 '14 at 9:53
  • 2
    or when using path like (skipped)/media with -jr instead of just -j and (skipped)/media/* I got error like: name in zip file repeated: .gitignore this may be a result of using -j zip error: Invalid command arguments (cannot repeat names in zip file) – andilabs Nov 18 '14 at 9:57
  • @andilabs have you got a solution? – Just a coder Oct 9 '18 at 22:12
  • I think only -j is not enough. I had to pass -jr. – Parth Tamane Apr 5 at 22:36
Zip command is:
zip -jr /home/jack/Desktop/Backup/20180314/032256.zip /home/jack/Desktop/bopy`

That creates a zip file without the whole folder in it. The flag "-jr" or "zip -j -r ....zip /../.."

  • 1
    (1) Can you explain what you are doing here? (2) Can you explain what your answer has that David’s answer didn’t already cover? (3) Your answer seems to have 1½ sentences. (4) What are you saying about ....zip /../..?  Please do not respond in comments; edit your answer to make it clearer and more complete. – Scott Mar 14 '18 at 2:36


find /home/andi/xyz/backend/media -type f -print | zip -j name.zip -@

find looks for files & pipes them to zip. -j means junk the path & -@ tells zip to read from the stdin.

Hope this was helpful.

  • 1
    Just to be complete: -type f only selects files and -print prints the name (pray you have no weird names. Else work around it with -print0). – Hennes Nov 18 '14 at 10:56
  • there are subdirectories I want to have in media. Ad-hoc I decided to use tar as demonstrated in updated question. – andilabs Nov 18 '14 at 12:43

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.