Bootstrap FreeKB - Linux Commands - zip
Linux Commands - zip

Updated:   |  Linux Commands articles

The zip command can be used to create a .zip archive that contains one or more files. In this example, example.zip will contain two files (foo.txt and bar.txt).

zip example.zip foo.txt bar.txt

 

Let's say the /tmp/foo directory contains a few files.

/tmp/foo/1.txt
/tmp/foo/2.txt
/tmp/foo/3.txt

 

The -r or --recurse-path flag can be used to create a .zip file that contains all of the files and directories in a target directory. However, be aware that by default, the full path to each file will be included in the zip. In this example, the zip file will contain tmp/foo/1.txt and tmp/foo/2.txt and tmp/foo/3.txt.

~]$ zip --recurse-path example.zip /tmp/foo
  adding: tmp/foo/ (stored 0%)
  adding: tmp/foo/3.txt (stored 0%)
  adding: tmp/foo/2.txt (stored 0%)
  adding: tmp/foo/1.txt (stored 0%)

 

If you just want to include the files and directories in the target directory, one option is to move into the target directory and then create the zip. In this example, the example.zip file will contain 1.txt and 2.txt and 3.txt.

~]$ cd /tmp/foo/
~]$ zip --recurse-path example2.zip .
  adding: 3.txt (stored 0%)
  adding: 2.txt (stored 0%)
  adding: 1.txt (stored 0%)

 

By default, zip will not remove the original files / directories. The -m or --move flag can be used to remove the original files and directories that were zipped.

zip --move example.zip foo.txt

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter f4adf5 in the box below so that we can be sure you are a human.