1

I'm trying to zip a file from the command line while I'm currently working in a different directory than that file is in.

The file is located at /home/one/file.txt

The present working directory is /home/two/

When I use the command zip /home/one/file.zip /home/one/file.txt, the zip file is created in the correct directory (/home/one), but the contents of the zip file are /home/one/file.txt. I want for the zip file to only contain file.txt.

What's the command to do this?

2 Answers 2

2

Zip is archiving file structure relative to the current directory. To override it you will need a -j flag. As per man pages:

-j --junk-paths
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).

EDIT: That means -j omits directory names so only files will be compressed. So your example (with working directory in /home/two) would look something like this:

zip -j ../one/file.zip ../one/file.txt
2
  • I'm a bit confused how using -j works. When I use the command zip -j /home/one/file.zip /home/one/file.txt, I'm getting zip error: Nothing to do! Commented May 17, 2021 at 5:44
  • @XtevensChannel I updated the answer
    – Maxoholic
    Commented May 17, 2021 at 5:58
0

The simplest solution is to use this command :

cd /home/one; zip /home/two/file.zip file.txt

You could use pushd instead of cd so you may come back to the same folder with popd. You could also create a script for it.

You must log in to answer this question.

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