Zipping contents of folder includes parent directories - linux

zip -r $packageName.zip /home/ubuntu/backend/upgrade/temp
I expect $packageName.zip to have all the contents in temp folder. However, I'm getting /home/ubuntu/backend/upgrade/contents in my zip folder.
I've tried
pushd /home/ubuntu/backend/upgrade/temp
zip -j /home/ubuntu/backend/test.zip ./*
popd
and
pushd /home/ubuntu/backend/upgrade/temp
zip -r /home/ubuntu/backend/test.zip ./*
popd

This should work:
zip -rj $packageName.zip /home/ubuntu/backend/upgrade/temp
You need the recursive portion of the command combined with the relative paths.

Related

Zip and Tar compress entire top directory and not (sub)directory

I'm trying to only zip the directory where I am exactly, this is part of a bigger bash script so I need to cd into the directory where I want to extract files and then exit.
However, using either tar or zip, the entire top directory path is recreated and not just the subdirectory that I'm interested in.
I get the following error:
zip warning: name not matched: $PWD/*
What I'm trying to do:
#Sub Directory and contents will be compressed
cd "$Sub_Dir"
Zipped_Files=$(basename "$Sub_Dir")
zip -r "$Zipped_Files".zip "$PWD/*"
#or
zip -j "$Zipped_Files".zip "$PWD/*"
#or
#tar -zcf "$Zipped_Files".zip "$PWD"
echo "Files have been compressed"
You have already cd into the sub dir, zip -r "$Zipped_Files".zip ./* should work.

Loop through directory and zip specific folder without parent path

I'm trying to make a bash script to loop through all folder in directories, and individually zip just the folder I want without all path and choose where to zip theme.
#!/bin/bash
for dir in /MyPersonalFolder/*/*/WhatIWantFolder
do
folder_number=$(basename ${dir%/*}) ### basename get the name of this folder [*] 'folder have numbers' /MyPersonalFolder/*/[*]/WhatIWantFolder
sudo tar -cf "${folder_number}-WhatIWantFolder".tar.gz --absolute-names "$dir"
mkdir -p ./backup-theme/ && sudo mv "${folder_number}-theme".tar.gz $_ ### I use this to move zipped folder to specific directory if i can choose where to zip file in the zip command line it's better
done
I can zip the folder I want, but the output zip file comes with this content:
/MyPersonalFolder/0001/0001/WhatIWantFolder
But what I need is to output the file with this path:
0001/
|___WhatIWantFolder/
I tried to change "$dir" in this line
sudo tar -cf "${folder_number}-WhatIWantFolder".tar.gz --absolute-names "$dir"
with basename ${dir%/*}
sudo tar -cf "${folder_number}-WhatIWantFolder".tar.gz --absolute-names "basename ${dir%/*}"
tar not found the folder it's come with errors
tar: e0001: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors
You can cd to another directory while running tar using the --cd option.
#!/bin/bash
for dir in MyPersonalFolder/*/*/WhatIWantFolder; do
parent="${dir%/*/*}"
subdir="${dir#*/*/*}"
outfile="backup-theme/${subdir/\//-}.tar.gz"
tar --cd "$parent" -cvzf "$outfile" "$subdir"
done
The directory structure of the tar file would look something like this:
tar -tf backup-theme/0001-WhatIWantFolder.tar.gz
0001/WhatIWantFolder/
0001/WhatIWantFolder/f1

Zip including hidden files

In Linux I can zip all(except hidden files) in current directory by doing:
zip 1.zip *
But how do I include the hidden files?
EDIT: The correct way is zip -r 1.zip .
The commands shown in my previous answer below are incorrect because they also include the parent directory.
Have you tried this:
zip yourfile.zip sourcedir/* .*
or you in your case
zip 1.zip * .[^.]*
It should include all hidden files also.
Or you can add more simple
zip 1.zip ./
Just to be sure it is not forgotten since this is a forum for developers and a good number of us use git.
An easy way to get only what you want in the zip is to use git archive -o filename.zip branch
If you want to zip all files (+hidden files)
Kindly using: zip -r namefiles.zip .
The "." is all files in folder.
zip -r namefiles.zip "folder will zip"
On macOS 10.15.7 I had to separatelly add all dot leading files (\.*) and rest of the files (*):
zip -r file.zip \.* *
if you don't have rights to save zip file in current dir you can go to dir where you have rights and type
zip -r 1.zip /path/to/source/dir/.
However when if in .../some_dir you type
unzip 1.zip
then your files will be decompress into .../some_dir/path/to/source/dir/
zip -r 1.zip .* -x "../*"
Just doing zip -r 1.zip .* will include the parent folder as well so the trick is to exclude the parent folder using -x "../*"
If you'd like to save some subdirectory of the current directory recursively with hidden and regular files just type
$ zip -r backup_subdirectory.zip backup_subdirectory/. backup-subdirectory/*
And for unzipping:
$ unzip backup_subdirectory.zip
Or even simpler by using tar for creating an archive:
$ tar czvf backup_subdirectory.tar.gz backup_subdirectory/
And for extracting all files from the archive:
$ tar xzvf backup_subdirectory.tar.gz

Unix unzip: how to batch unzip zip files in a folder and save in subfolders?

Say if I have a folder 'images' and inside it there are 0001.zip to 9999.zip, I want to unzip all of them and save them in subfolder which has their file name, for example, 0001.zip will be unzipped and saved to /0001, 0002.zip will be unzipped and saved to /0002, I tried to do
unzip '*.zip'
but that extracts all files in current folder.
You could do something like this:
for file in *.zip; do
dir=$(basename "$file" .zip) # remove the .zip from the filename
mkdir "$dir"
cd "$dir" && unzip ../"$file" && rm ../"$file" # unzip and remove file if successful
cd ..
done
or, run it together on one line:
for file in *.zip; do dir=$(basename "$file" .zip); mkdir "$dir"; cd "$dir"; unzip ../"$file" && rm ../"$file"; cd ..; done
If you need/want to keep the original .zip files, just remove the && rm ../"$file" bit.
for zip in *.zip
do
unzip "$zip" -d "${zip%.zip}"
done

Zip stating absolute paths, but only keeping part of them

zip -r 1.zip /home/username/the_folder
At here, when i unzip 1.zip, it will create /home/username/the_folder, from whichever folder i am unzipping from.
How do I zip, stating the full absolute paths, but make the zip only contain the folder structure starting at, in this case for instance, /home/username?
That way I could be at whatever path i wanted, unzip and it would just create the_folder, and not /home/username/the_folder.
Use this command:
cd path_under_folder_to_zip && \
zip -r 1.zip folder_to_zip >/dev/null && \
mv 1.zip my_current_path
Use relative path when specifying the file to zip.
cd /home/username
zip -r 1.zip ./the_folder
Then when you unzip, it'll be a relative path starting at whichever folder you're in when unzipping.
Just use the -j option, works on OSX, I don't know about linux.
zip -j -r 1.zip /home/username/the_folder
List item
How about this:
function zipExtraFolder {
if [ $# -lt 2 ]; then
echo "provide at least two arguments"
return
fi
folder=$2
mkdir del
echo cp -r `dirname $folder` del
cd del
echo zip -r ../$1 .
cd -
rm -rf del
}
Define above as a shell function in your .bashrc and you should be able to use it whenever you want.
The usage will be like below.
zipExtraFolder 1.zip /home/username/the_folder

Resources