Archiving multiple folders and/or files using CLI?

rado84

Well-Known Member
Joined
Feb 25, 2019
Messages
757
Reaction score
623
Credits
4,705
I started practicing archiving stuff using the CLI (because that happens a lot faster in the terminal than it does in the GUI) and so far I managed to do it but only for a single file or folder:
Code:
7z a mozilla-archive.7z ~/.mozilla -m9=lzma2

However, my several attempts to archive several folders (containing many subfolders and many files and subfiles) failed miserably, so apparently I'm doing something wrong. For instance, there's this directory called "Scania Streamline v1.0.9" which contains this:

Code:
.
├── def
│   ├── country
│   │   ├── austria
│   │   ├── belgium
│   │   ├── czech
│   │   ├── denmark
│   │   ├── estonia
│   │   ├── finland
│   │   ├── france
│   │   ├── germany
│   │   ├── hungary
│   │   ├── italy
│   │   ├── latvia
│   │   ├── lithuania
│   │   ├── luxembourg
│   │   ├── netherlands
│   │   ├── norway
│   │   ├── poland
│   │   ├── russia
│   │   ├── slovakia
│   │   ├── sweden
│   │   ├── switzerland
│   │   └── uk
│   └── vehicle
│       ├── ai
│       └── truck
│           └── scania.streamline
│               ├── accessory
│               │   ├── beacon
│               │   ├── b_grill
│               │   ├── codrv_plate
│               │   ├── doorhndl
│               │   ├── doorstep
│               │   ├── drv_plate
│               │   ├── exhaust_l
│               │   ├── exhaust_r
│               │   ├── f_grill
│               │   ├── f_intake_cab
│               │   ├── f_mirror
│               │   ├── mirror
│               │   ├── r_bumper
│               │   ├── r_grill
│               │   ├── r_mudflap
│               │   ├── set_dashbrd
│               │   ├── set_glass
│               │   ├── sideskirt
│               │   ├── s_mirror
│               │   ├── steering_w
│               │   ├── sunshld
│               │   ├── toyhang
│               │   └── toystand
│               ├── cabin
│               ├── chassis
│               ├── engine
│               ├── head_light
│               ├── interior
│               ├── paint_job
│               ├── sound
│               └── transmission
├── material
│   └── custom
├── sound
│   └── ui
└── unit
    └── hookup

So, what I wanna do is add to an archive all folders, subfolders and the files in there but everytime I get an error. This directory tree is a mod for Euro Truck Simulator 2 and the only way for the mod to work is to add to an archive these 4 folders (def, material, sound, unit) with everything that's inside them.

These are my attempts so far. And yes, I know I can do it using the 7-zip GUI for Windows through Wine but I don't want to. I wanna do it in the terminal.

Code:
[rado@anarchy]: /media/1000GB/АРХИВ/ИГРИ/TRUCK GAMES/ETS 2/temp-1/Scania Streamline v1.0.9>$ 7z a scania.zip def/ material/ sound/ unit/ -copy


Command Line Error:
Unknown switch:
-copy
[rado@anarchy]: /media/1000GB/АРХИВ/ИГРИ/TRUCK GAMES/ETS 2/temp-1/Scania Streamline v1.0.9>$ 7z a scania.zip def/ material/ sound/ unit/ -m0

7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=bg_BG.UTF-8,Utf16=on,HugeFiles=on,64 bits,4 CPUs Intel(R) Core(TM) i3-4170 CPU @ 3.70GHz (306C3),ASM,AES-NI)

Scanning the drive:
65 folders, 473 files, 1186160 bytes (1159 KiB)

Creating archive: scania.zip

Items to compress: 538

    

System ERROR:
E_INVALIDARG
[rado@anarchy]: /media/1000GB/АРХИВ/ИГРИ/TRUCK GAMES/ETS 2/temp-1/Scania Streamline v1.0.9>$ 7z a scania.zip */ -m0

7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=bg_BG.UTF-8,Utf16=on,HugeFiles=on,64 bits,4 CPUs Intel(R) Core(TM) i3-4170 CPU @ 3.70GHz (306C3),ASM,AES-NI)

Scanning the drive:
65 folders, 473 files, 1186160 bytes (1159 KiB)

Creating archive: scania.zip

Items to compress: 538

    

System ERROR:
E_INVALIDARG
 


I think the error messages are because of the extra arguments you're putting at the end of the line.
Any options should be put BEFORE the name of the archive and the list of files/directories.

If you are trying to create a zip and you are already inside the top level directory :
This will just recursively put everything into the archive:
Code:
7z a -r -tzip /path/to/test.zip *

NOTE: Where /path/to/test.zip is the path to the output file. Replace /path/to/test.zip with whatever path/filename you want to use for the output file!

If you only want specific directories/files going into the archive, put the list of directories/files at the end of the line:
Code:
7z a -r -tzip /path/to/test.zip def material sound unit
Again - replace /path/to/test.zip with the desired path to the output file.

If you want to use some of the compression options, they should be specified BEFORE the archive name.
So if you wanted to do a 7z with the maximum compression you could try something like this:
Code:
7z a -r -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=on /path/to/test.7z def material sound unit

I hope this helps!
 
@JasKinasis , I'll try the ways you said but I for one doubt the error is because of args at the end of the line. That way I make a full backup of my mozilla user directory and it works like a charm. Here, see for yourself:

Code:
[rado@anarchy]: ~>$ 7z a dot-mozilla.7z ~/.mozilla -m9=lzma2

7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=bg_BG.UTF-8,Utf16=on,HugeFiles=on,64 bits,4 CPUs Intel(R) Core(TM) i3-4170 CPU @ 3.70GHz (306C3),ASM,AES-NI)

Scanning the drive:
194 folders, 1027 files, 371552528 bytes (355 MiB)

Creating archive: dot-mozilla.7z

Items to compress: 1221

                                                                              
Files read from disk: 1021
Archive size: 243844400 bytes (233 MiB)
Everything is Ok
[rado@anarchy]: ~>$

For the above command to backup mozilla I used the explanations from the attached file with syntax namely: "program operation output-file-name-with-extension /path/to/source/directory/ -level_and_method_of_compression". Which translated to an actual command, looks like this:

Code:
7z a dot-mozilla.7z ~/.mozilla -m9=lzma2

That's why I thought it would be something similar for archiving several folders and/or files with all of their content.
 
I think this will work below. It should recurse subdirectories and it should include hidden files or folders, if they exist. If you are sure there are no hidden files/folders, then you can also replace the "." at the end with a "*" instead (do not use a / at the end). The .zip file will be in the same location as the source files, so you can change that to include a different path, if desired. As in your example above, start in the folder that you want to archive.

Code:
/media/1000GB/АРХИВ/ИГРИ/TRUCK GAMES/ETS 2/temp-1/Scania Streamline v1.0.9>$ 7z a scania.zip .


This link (with heading TL;DR) gave clear, simple directions that worked with a test folder that I created. My test also included hidden files and folders. I didn't try to add compression methods or other options... just a working model that you can build from.

The man page for 7z says that it is not good for archiving in Linux/Unix as it does not save owner/group information. However, in my very limited testing, it did seem to preserve that info. You may want to be sure in your own case if the loss of owner/group info would break your mod.

Cheers
 
The man page for 7z says that it is not good for archiving in Linux/Unix as it does not save owner/group information. However, in my very limited testing, it did seem to preserve that info. You may want to be sure in your own case if the loss of owner/group info would break your mod.
Sorry for the late answer, I was quite busy and completely forgot about this topic.

Owner/group info doesn't concern me bc my FS are NTFS and
Code:
/dev/sdb1    /             ext4          rw,relatime    0 1
/dev/sdc1 /media/1000GB ntfs-3g defaults,auto,nofail,x-systemd.device-timeout=10,uid=1000,gid=1000,umask=000 0 0
/dev/sda1 /media/500GB ntfs-3g defaults,auto,nofail,x-systemd.device-timeout=10,uid=1000,gid=1000,umask=000 0 0
all files - past, present and future automatically become mine. The mod can't be broken because of ownership because in Linux it's installed on the ext4 FS which is already owned by me. Plus, the majority of the users of this mod use Windows which doesn't care about linux ownership. ;)

The code you posted does work but it makes the zip file with a "normal" compressions bc of which the mod won't work. It has to be zero level of compression - meaning no compression at all.

So, here's what I discovered after some experimenting, after I posted this topic:
• when you want a specific level of compression (1, 5, 7, 9), you gotta type -mZ=lzma2 where Z is the desired number and where 1 is being the lowest level of compression and 9 is "ultra".
• however, if you want zero compression, then the syntax of the last argument changes a bit: it has to be like this (assuming I'm already in the top level dir where the Scania folder is located):
Code:
7z a scania-test.zip "Scania Streamline v1.1.1/" -mx=0 && mv *.zip *.scs
Technically the lack of compression level means there's no compression method and that's why the zero compression syntax is like this. And with the this single line of code I make the mod completed without even touching the file itself. :D

Oh, and one more thing I found out on my own: for the multiple folders archiving to work, it's not enough just to list them as
Code:
 def/ material/

Listing has to include quotes, otherwise archiving won't work.

Code:
[rado@anarchy]: ~/.local/share/Euro Truck Simulator 2/mod/Scania Streamline v1.1.1>$ 7z a scania.zip "def/" "material/" "sound/" "unit/" -mx=0

7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=bg_BG.UTF-8,Utf16=on,HugeFiles=on,64 bits,4 CPUs Intel(R) Core(TM) i3-4170 CPU @ 3.70GHz (306C3),ASM,AES-NI)

Scanning the drive:
64 folders, 472 files, 1184397 bytes (1157 KiB)

Creating archive: scania.zip

Items to compress: 536

    
Files read from disk: 472
Archive size: 1294099 bytes (1264 KiB)
Everything is Ok
 
Last edited:
What's wrong with tar? It keeps permissions?

tar cvf myarchive.tar /path/to/folder1 /path/to/folder2 /path/to/folder3

If you want to compress it.

bzip2 or gzip myarchive.tar

To restore the archive.

gunzip myarchive.tar.gz

tar xvf myarchive.tar
 
What's wrong with tar?
First, as I said above, the mod file must be with zero compression and then renamed to '.scs' for it to work.
Second, I prefer archiving the result file with ultra compression using a known format. Why? Give a tarball to a Windows user, grab some popcorn, lean back and watch the show. First he will scratch his noggin and when he can't figure it out (especially if it's archive within archive which is the case with tarballs sometimes), the result would be deleted mod and comments like "linux sucks".
 
First, as I said above, the mod file must be with zero compression
By default, tar does not use compression... but it can be compressed with gzip, bzip, 7zip, and others.


Give a tarball to a Windows user, grab some popcorn, lean back and watch the show.
Catering to the needs of Windows users does not usually occur to us when trying to answer questions on this forum, nor did you mention above that it was important to you when you began your questioning.

Glad you've found your solution!

Cheers
 
I'm already taking care of the Windows users needs when I make my mods (this game has both Windows and Linux native clients), that's why I didn't mention it and kept talking about the need for the file to be pure zip - not gzip, not tarbal, not bzip. 7zip can only be once the mod for the game was created and can be compressed at ultra level for convenience when uploading it.
 

Members online


Latest posts

Top