How to zip contents of a folder in linux command line - linux

So i have a folder structure like MyFolder/File1, MyFolder/File2 .... MyFolder/FileN
I want to zip all the contents of MyFolder to MyFolder.zip such that on unzipping MyFolder.zip the contents are File1, File2 ... FileN
I want to do this using the linux command line.
I saw a method using the -r option of the zip command but the issue is that it creates a MyFolder.zip that on unzipping gives MyFolder. I do not want the MyFolder in it. I want it to directly give me the contents of MyFolder.
Any help will be appreciated. Thanks

You might enter the directory first before running the zip command (output file should be elsewhere for speed & to avoid warning "file changed during compression" on the zip file itself
zip ../toto.zip . -r
for example

Related

How to unzip a file inside a zipped folder without unzipping the folder.zip using command line?

I have a zipped folder : folder.zip, the zip contains file1 and file2. I want to unzip file1 without unzipping folder.zip (Like what we can do using WinRaR). I want to be able to do this scenario using command line :
open (Without unzipping) folder.zip and display content
Find file1 inside folder.zip
Unzip file1
Get at the end file1 unzipped and folder.zip
I tried to do it using WinRaR application, and I'm expecting to do it using command line.
Did you try unzip folder.zip my/file/in/folder/file1 the/other/file/in/folder/file2?
I tried unzip zipfile.zip '*/file1.txt' and it works. Thank you!

unziping files in linux folder

in Python I have the following command executed unzip '{dir}ATTOM_RECORDER/*.zip' -d {dir}ATTOM_RECORDER/ as a bash command. The python call works perfectly. my question is about the unzip command itself.
for some reason when unzip is called to expand any relevent zip files in the folder specified, not all the files WITHIN the zip is extracted. There's usually a rpt and a txt file. However, sometimes the txt file is not coming out and I do not have an error command.
How can I ensure the txt file is guaranteed to be extracted before moving on?
Thanks
While you want to unzip your specific zip file. There are many option to decompress any file from zip files. Easiest way is the ā€˜-lā€™ option with unzip command is used to list the contents of a zip file after extracting it.
Syntax: unzip -l [file_name.zip]

How to zip all txt files recursively in linux using zip?

I want to zip all .txt files in using zip command in linux recursively. For this, I'm using:
zip -r /home/folder/zipName /home/folder2/subfolder1/*.txt
and it is giving me the below error:
zip warning: missing end signature--probably not a zip file (did you
zip warning: remember to use binary mode when you transferred it?)
zip warning: (if you are trying to read a damaged archive try -F)
zip error: Zip file structure invalid
use
zip -r /home/folder/zipName /home/folder2/subfolder1 -i \*.txt
it will do what you expected.
I had the same error, and I found that the solution was to make sure that your zipName ends in .zip. Putting this here in case it can help somebody else.
This works for me ...
zip -r /home/folder/zipName /home/folder2/subfolder1/**/*.txt

unzip in current directory while preserving file structure

I'm in a directory and I have a zip containing files and directories.
I need to unzip that file, into current directory, but preserving the file structure.
unzip myfile.zip will create a myfile directory in current directory which is not what I want.
unzip -j myfile.zip will kill all the file strucure, which is not what I want.
unzip myfile.zip extracts files in the working directory by keeping path names from the zip file.
So if you get a subdirectory myfile it means it is part of the relative path of compressed files. Check it by listing the zip content
unzip -l myfile.zip
So you can unzip the file from the directory above, or, from the target directory unzip with -d option, where -d is the directory above
cd myfile
unzip myfile.zip -d ..
Dont select the folder while zipping.
For example
myfile/abc.txt and myfile/efg.txt
so while zipping select the files (abc.txt,efg.txt) and zip dont select the myfile folder to zip.
So that when you unzip the file, the parent dir for each file or folder will be the directory in which you unzip.
The myfile directory was zipped into the zip file when it was created and looking at the unzip options there isn't a way to do this without adding additional steps.
If this entire process is under your control you should look at either creating the zip without using including the parent directory or you could use an alternative like tar (to create and extract) which allows you to extract content from the repo with greater precision.

How to rename files according to other files?

I have several files in a folder with extension .img and I have only one file with extension data.txt
What I need is to copy data.txt and rename it as the names of the .img files.
For instance for the first file in my folder:
`Meaurmen_2154_data.img` >>> copy data.txt >>> rename it Meaurmen_2154_data.txt
Now I have :
Meaurmen_2154_data.img
Meaurmen_2154_data.txt ## the content is the same as data.txt
and do the same for all other files. The content of he text files will be the same for all files just we change the name according to the .img files in my folder.
Run this script
#!/bin/bash
imageFiles=( *.img );
for i in ${imageFiles[*]}
do
withoutExtension=${i%.img};
cp data.txt "$withoutExtension.txt";
done
inside the relevant directory and it will do it for you.
Try
for i in *.img; do cp data.txt $i.txt; done
rename 's/.img.txt/.txt/' *.img.txt
In some distro's rename is different, requiring
rename .img.txt .txt *.img.txt
As always, you might find yourself in need of installing additional packages.

Resources