7z creates emtpy file within script


New Member
May 13, 2019
Reaction score

I tried to create a 7z file with a script. But only an empty file was created. When I use the same command on the cli it works. I am at a loss. What do I have to do to use 7z in a script?

I'm using:
Debian GNU/Linux buster/sid
Linux raspberrypi 4.9.35-v7+ #1014 SMP Fri Jun 30 14:47:43 BST 2017 armv7l GNU/Linux

The command within the script...
7z a -r -tzip ./test.zip /home/pi/test/*
.... and the corresponding output:
pi@raspberrypi ~ $ bash ./test7z.sh

7-Zip [32] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=de_DE.UTF-8,Utf16=on,HugeFiles=on,32 bits,4 CPUs LE)

Scanning the drive:
0 files, 0 bytes

Creating archive: ./test.zip

Items to compress: 0

Files read from disk: 0
Archive size: 22 bytes (1 KiB)
Everything is Ok
And here running the 7z command directily in the cli. Now it works:
pi@raspberrypi ~ $ 7z a -r -tzip ./test.zip /home/pi/test/*

7-Zip [32] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=de_DE.UTF-8,Utf16=on,HugeFiles=on,32 bits,4 CPUs LE)

Open archive: ./test.zip
Path = ./test.zip
Type = zip
Physical Size = 22

Scanning the drive:
3 folders, 7 files, 6167 bytes (7 KiB)

Updating archive: ./test.zip

Items to compress: 10

Files read from disk: 7
Archive size: 4028 bytes (4 KiB)
Everything is Ok
What I'm doing wrong? Maybe someone can help me.
Thank you.


The command within the script...
7z a -r -tzip ./test.zip /home/pi/test/*
The syntax usually is [archiver] [options] [archive-name] [source] and what I can see in your code is the './ ' in front of the archive name. There shouldn't be a './ ' in the name. It should be just 'test.zip'. IDK why this works in CLI, maybe the terminal is ignoring that part. AFAIK the './ ' is used for executing scirpts in terminal, not for creating archives.
Hi @Markus, and welcome. I'm not too good with programming, but check out the man page for 7z:
man 7z

There it first hints that the -r switch might be a problem for you.
Recurse subdirectories (CAUTION: this flag does not do what you
think, avoid using it)

Then further down it says the directory/* at the end of your command should not be used like that. It also reminds you that 7z does not store owner/group information about the data you are archiving.... you should use tar instead (with example shown).
Backup and limitations
DO NOT USE the 7-zip format for backup purpose on Linux/Unix because :
- 7-zip does not store the owner/group of the file.

On Linux/Unix, in order to backup directories you must use tar :
- to backup a directory : tar cf - directory | 7z a -si direc‐
- to restore your backup : 7z x -so directory.tar.7z | tar xf -

If you want to send files and directories (not the owner of file) to
others Unix/MacOS/Windows users, you can use the 7-zip format.

example : 7z a directory.7z directory

Do not use "-r" because this flag does not do what you think.

Do not use directory/* because of ".*" files (example : "directory/*"
does not match "directory/.profile")

Good luck!

Members online