Zipping everything in directory with 7z except one file or one file type
Asked Answered
K

4

19

i'd like to zip everything except one file

7z a -tzip files.zip *

this will zip all the files in my current directory.. is there a way I can tell it to not zip one file or one file type ?

Kamilah answered 18/11, 2012 at 23:17 Comment(0)
A
25

Per the 7za command-line help, you use the -x switch to do this:

-x[r[-|0]]]{@listfile|!wildcard}: eXclude filenames

To exclude the file foo.txt you would add:

-x!foo.txt

To exclude all .html files (*.html) you would add:

-x!*.html

You can add multiple -x entries to exclude multiple filenames and/or wildcards in one zip command. Adding the following will exclude foo.txt and *.html:

-x!foo.txt -x!*.html

So with your example, this would add all files to files.zip EXCEPT files named "FILENAME" or that matched the *.extension wildcard:

7z a -tzip files.zip * -x!FILENAME -x!*.extension
Araminta answered 19/11, 2012 at 23:39 Comment(4)
For future reference: -x!*.extension doesn't work (at least for recursive sub-directories). -x!*extension does work.Barriebarrientos
In powershell use "-xr!*.extension" for ignoring all *.extension recursively. Pay close attention to the quotesDescend
To exclude Git-related files and folders in PowerShell, add "-xr!.git\" and "-xr!.gitignore". Note the double quotes (") as mentioned by @ChrisKolenko.Monarch
Your example assumes the target dir is the current dir. How would you structure the command if your target dir isn't the current dir?Flatware
P
11

If you are using batch script do not forget to escape ! mark.

7z a -xr^^!*.xml "dest_dir.zip" "Source_dir"
Piling answered 14/3, 2017 at 15:45 Comment(2)
I used 7z command in GitLab CI and it was stuck. "-xr^^!*.xml" helped. Thank you for the solution!Seaden
I think there is one too many ^ here...^^ didn't work for me, this worked for me: 7z a -xr^!*.xml "dest_dir.zip" "Source_dir"Haleakala
V
1

For excluding individual files or wildcard entries, the command line option does not seem to work. Instead list the files to be excluded and wild cards in text file (..\xlist.txt) and specify it in the comand line like this:

7z a -tzip files.zip * -xr@..\xlist.txt

Specify xlist.txt using relative path.

xlist.txt contents:

xlist.txt itself if you dont want it
index*
*.js
*.tmp
etc...
Venom answered 14/11, 2023 at 13:1 Comment(0)
H
0

This fragment of a larger (linux bash) 7z-script (with password compression, adjustable LEVEL setting etc) excludes the script file itself, also when dirs need to be compressed.

The ${INPUT} comes from:

echo "Your Input please?"

    read -e -p "" INPUT

and can be a simple '*' wildcard to refer to all files and dirs.

Fragment:

for FILE in ${INPUT}
do
if [[ ${FILE} != my_7z_script.sh ]]
then
7z a -mx=${LEVEL} -mhe -t7z ${FILE%.*}.7z ${FILE} -x!my_7z_script.sh
fi
done

Could probably leave the '-x!my_7z_script.sh' out, but kept it in anyway. If the file name of the script is long, you can also use an asterix wildcard there, e.g.: 'my_7z*.sh'.

Addendum, the above makes separate 7z's of every file and folder. To convert/compress all files and dirs (within the dir of the script, except the script itself) to 1 7z:

echo "The Final 7z Destination of your Input please (without 7z-extension)?"

    read -e -p "" DESTINATION
    
        for FILE in ${INPUT}
            do
            if [[ ${FILE} != my_7z_script.sh ]]
            then
            7z a -mx=${LEVEL} -mhe -t7z ${DESTINATION}.7z ${INPUT} -x!my_7z_script.sh
            fi
            done

'*' wildcard for all files and dirs input, exclusive hidden ones (.hidden), '.' wildcard for all files and dirs input, inclusive hidden ones.

Maybe room for improvements, but it seems to work.

(P.S.: the ${LEVEL} var isn't specified here. You can replace it with anything from '0' aka 'store only' to '9' aka 'high compression'.)

Hoecake answered 21/2 at 16:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.