- How can I create a zip archive of a whole directory via terminal without hidden files?
- 8 Answers 8
- How to Zip Files and Folders in Linux
- Zip a folder in Linux Command Line
- Zip a folder in Linux Using GUI
- Создаем zip-архивы в командной строке
- Создаем простой zip-архив
- Создаем zip-архив папки
- Создаем zip-архив с паролем
- Распаковка zip-архива
How can I create a zip archive of a whole directory via terminal without hidden files?
I have a project with lots of hidden folders / files in it. I want to create a zip-archive of it, but in the archive shouldn’t be any hidden folders / files. If files in a hidden folder are not hidden, they should also not be included. I know that I can create a zip archive of a directory like this:
zip -r zipfile.zip directory
zip -r zipfile.zip directory -x .*
8 Answers 8
First of all if you don’t have installed zip install it first as follows:
Then for simply creating a zip file:
zip -r compressed_filename.zip foldername
If you want to exclude hidden files:
find /folder_path -path '*/.*' -prune -o -type f -print | zip ~/compressed_filename.zip -@
The basics of file exclusion when creating a zip archive are centered around the -x flag, which is used to exclude files from the archive that match a specific name or pattern. At it’s most basic, it will look like this:
zip archive.zip files -x "ExcludeMe"
Meaning you could exclude a single file, say it’s named “Nothanks.jpg”
zip archive.zip images/ -x "Nothanks.jpg"
Let’s cover a few specific examples where this is useful.
Exclude .DS_Store Files from Zip Archives
This will prevent the typically invisible Mac metadata .DS_Store files from being included in a zip archive, which are bundled in by default:
zip -r archivename.zip archivedirectory -x "*.DS_Store"
If the directory includes subdirectories however, you’ll want to use another variation of that command to exclude the the ds_store files from subdirectories as well:
zip -r archive.zip directory -x "*/\.DS_Store"
Note: not all shells require the quotations for this command to work properly, but in the bash shell (the default for OS X) you will need to use the quotes for excluding with wildcards and patterns.
Exclude Specific File Types from a Zip Archive
With wildcards, you can also exclude all files of a certain type by focusing on the extension. For example, this command will zip an entire directory, minus any .jpg files:
zip -r archive.zip directory -x "*.jpg"
That could be modified for any specific file extension or pattern matched in a file name.
Exclude the .git or .svn Directory from a Zip Archive
Zip a directory, minus .git and it’s contents:
zip -r zipdir.zip directorytozip -x "*.git*"
Zip a folder, without including the .svn directory:
zip -r zipped.zip directory -x "*.svn*"
Exclude All Hidden Files from a Zip Archive
Since patterns and wildcards can be used, you could also exclude any or all invisible files and folders that are made so by being prefixed with a period, whether it’s a directory like .svn or an individual file like .bash_profile or .htaccess .
zip -r archivename.zip directorytozip -x "*.*"
Or to exclude all invisible files from all subdirectories:
zip -r archive.zip directory -x "*/\.*"
How to Zip Files and Folders in Linux
This quick tip shows you how to create a zip folder in Ubuntu and other Linux distributions. Both terminal and GUI methods have been discussed.
Zip is one of the most popular archive file format out there. With zip, you can compress multiple files into one file. This not only saves disk space, it also saves network bandwidth. This is why you’ll encounter zip files almost all the time. As a normal user, mostly you’ll unzip files in Linux. But how do you zip a folder in Linux? This article helps you answer that question. Prerequisite: Verify if zip is installed Normally zip support is installed but no harm in verifying. Open a terminal and use the following command:
If you see some details on the zip version, you have zip installed already. If it displays ‘zip command not found’, you can run the below command to install zip and unzip support in Ubuntu and Debian based distributions.
sudo apt install zip unzip
Now that you know your system has zip support, you can read on to learn how to zip a directory in Linux.
Zip a folder in Linux Command Line
zip [option] output_file_name input1 input2
While there could be several options, I don’t want you to confuse with them. If your only aim is to create a zip folder from a bunch of files and directories, use the command like this:
zip -r output_file.zip file1 folder1
zip -r myzip abhi-1.txt abhi-2.txt sample_directory adding: abhi-1.txt (stored 0%) adding: abhi-2.txt (stored 0%) adding: sample_directory/ (stored 0%) adding: sample_directory/newfile.txt (stored 0%) adding: sample_directory/agatha.txt (deflated 41%)
You can use the -e option to create a password protect zip folder in Linux. You are not always restricted to the terminal for creating zip archive files. You can do that graphically as well. Here’s how!
Zip a folder in Linux Using GUI
Though I have used Ubuntu here, the method should be the same in other distributions using GNOME or other desktop environments.
If you want to compress a file or folder in desktop Linux, it’s just a matter of a few clicks. Go to the folder where you have the desired files (and folders) you want to compress into one zip folder. Here, select the files and folders. Now, right-click and select Compress. You can do the same for a single file as well. Now you can create a compressed archive file in zip , tar xz or 7z format. In case you are wondering, all three are various compression algorithms that you can use for compressing your files. Give it the name you desire and click on Create. It shouldn’t take long and you should see an archive file in the same directory. Well, that’s it. You successfully created a zip folder in Linux. The next step is to learn to unzip a zipped file in the Linux command line. I hope this quick little tip helped you with the zip files. Please feel free to share your suggestions.
Создаем zip-архивы в командной строке
Рассмотрим, как создавать и распаковывать zip архивы из командной строки.
Для создания архивов служит команда zip. У нее есть более 30 разных опций, но я рассмотрю простейшие примеры.
Создаем простой zip-архив
Для создания zip-архива просто выполняем команду zip, в первом аргументе указываем имя будущего архива, а во втором сам файл, который мы сжимаем:
Если нужно сжать несколько файлов то перечисляем их через пробел:
zip myarchive.zip myfile.txt yourfile.txt theirfile.txt
Создаем zip-архив папки
Чтобы заархивировать папку, используется ключ -r:
zip -r mydir.zip verygooddir
Создаем zip-архив с паролем
Очень важной функцией утилиты zip является возможность задания пароля на распаковку архива. Для этого применяется опция -P, после которой следует написать пароль:
zip -P мойпароль -r mysecretdir.zip mysecretdir
Если вы не хотите вводить пароль в командной строке у всех на виду, то можно использовать опцию -e, чтобы вместо ввода пароля в открытую, вводить его в срытом виде:
zip -er mysecretdir.zip mysecretdir
После выполнения данной команды, вам будет предложено дважды ввести пароль. Сам пароль виден при этом не будет:
Enter password: Verify password:
Распаковка zip-архива
Для того, чтобы разархивировать zip-архив, используется команда unzip. Ее можно запускать без опций, только указывая имя архива:
По умолчанию распаковка происходит в текущей директории. Чтобы распаковать архив в другую директорию, используется опция -d, после которой нужно указать путь до директории: