Shell script to extract all zip files in a directory and its sub directory, and copy the extracted files into another folder with same hierarchy - shell

I have some zip files like below.
./etk/test/etf_time_series_am_update.zip
./etk/test/etf_time_series_am_delete.zip
./etk/dir1/etf_time_series_am_update.zip
./etk/dir1/etf_time_series_am_delete.zip
./etk/dir1/dir2/etf_time_series_am_update.zip
./etk/dir1/dir2/etf_time_series_am_delete.zip
./etk/dir1/dir2/dir3/etf_time_series_am_update.zip
./etk/dir1/dir2/dir3/etf_time_series_am_delete.zip
I want these files to unzip and store into the another folder. like
./newf/test/etf_time_series_am_update.txt
./newf/test/etf_time_series_am_delete.txt
./newf/dir1/etf_time_series_am_update.txt
./newf/dir1/etf_time_series_am_delete.txt
./newf/dir1/dir2/etf_time_series_am_update.txt
./newf/dir1/dir2/etf_time_series_am_delete.txt
./newf/dir1/dir2/dir3/etf_time_series_am_update.txt
./newf/dir1/dir2/dir3/etf_time_series_am_delete.txt
I tried with the find command and able to unzip, but couldn't copy in the destination folder.
I tried to unzip by following command. but have no idea about copy into the destination folder.
find -name '*.zip' -exec sh -c 'unzip -d "${1%.*}" "$1"' _ {} \;

This might be works for you.
find . -name "*.zip" | while read filename; do unzip -o -d "`dirname "$filename" | sed -r 's/etk/newf/g'`" "$filename"; done;
You may also get better solution by referring following links.
Finding-multiple-files-recursively-renaming
Recursively unzip

Related

Copy all files with a certain extension from all subdirectories and preserving structure of subdirectories

How can I copy specific files from all directories and subdirectories to a new directory while preserving the original subdirectorie structure?
This answer:
find . -name \*.xls -exec cp {} newDir \;
solves to copy all xls files from all subdirectories in the same directory newDir. That is not what I want.
If an xls file is in: /s1/s2/ then it sould be copied to newDir/s1/s2.
copies all files from all folders and subfolders to a new folder, but the original file structure is lost. Everything is copied to a same new folder on top of each other.
You can try:
find . -type f -name '*.xls' -exec sh -c \
'd="newDir/${1%/*}"; mkdir -p "$d" && cp "$1" "$d"' sh {} \;
This applies the d="newDir/${1%/*}"; mkdir -p "$d" && cp "$1" "$d" shell script to all xls files, that is, first create the target directory and copy the file at destination.
If you have a lot of files and performance issues you can try to optimize a bit with:
find . -type f -name '*.xls' -exec sh -c \
'for f in "$#"; do d="newDir/${f%/*}"; mkdir -p "$d" && cp "$f" "$d"; done' sh {} +
This second version processes the files by batches and thus spawns less shells.
This should do:
# Ensure that newDir exists and is empty. Omit this step if you
# don't want it.
[[ -d newDir ]] && rm -r newDir && mkdir newDir
# Copy the xls files.
rsync -a --include='**/*.xls' --include='*/' --exclude='*' . newDir
The trick here is the combination of include and exclude. By default, rsync copies everything below its source directory (. in your case). We change this by excluding everything, but also including the xls files.
In your example, newDir is itself a subdirectory of your working directory and hence part of the directory tree searched for copying. I would rethink this decision.
NOTE: This would not only also copy directories whrere the name ends in .xls, bur also recreated the whole directory structure of your source tree (even if there are no xls files in it), and populate it only with xls files.
Thanks for the solutions.
Meanwhile I found also:
find . -name '*.xls' | cpio -pdm newDir

bash script to Remove *.pom file from multiple directories which contains *.jar and *.pom aswell

Multiple directories in the Linux system contain *.jar and *.pom files. However, some directories only contain *.pom files. That I am attempting to delete but am unable to do so
Using below script, it removes all *.pom files from all the directories which contains *.jar as well.
#!/bin/sh
sudo find /var/opt/jfrog/artifactory/2021*/repositories/ -type f \( -name "*.pom" \) -exec rm {} \;
I'm attempting to delete only the *.pom file from the pic directory structure shown below.
directory structure
Instead of running rm directly, run a script that checks for jar files and deletes the pom only if there is no jar. Here we use -execdir to make this a bit easier and more efficient:
find ... -type f -name '*.pom' -execdir bash -c \
'compgen -G \*.jar > /dev/null || rm "$#"' . {} +
I would code:
#!/bin/bash
find /var/opt/jfrog/artifactory/2021*/repositories/ -name '*.pom' |
while IFS= read -r file; do
if [ ! -f ${file%.pom}.jar ] ; then rm $file ; fi
done
Explanation: find sends all the .pom files to a while loop. For each .pom file, the loop checks that the corresponding .jar file does not exist and in that case, it deletes the .pom file.

Unix - batch file to unzip folder with specific name

I need a batch script for unix but I don't know it very well.
I have folder A and his subfolder
A\a1\b\c\file.zip
A\a2\b\c\otherFile.zip
A\a3\b\c\thirdFile.zip
Each zip file contains a xml file and a text file
The script have to do 2 things:
unzip all the zip files that are in all folder named 'c' of all sub
folder of 'A' ; the unzipped files should stay in the same folder in
which was the zip
all the unzipped files that have xml extension have to been renamed
someone can help me?
Thank you very much
You can do like this.
#find the folder 'c' and unzip all zip files
for folder in `find ./A -name c -type d`; do unzip $folder/data.zip -d $foler; done
#find all .xml files and change the extension to .edefg
for file in `find ./A -name *.xml -type f`; do mv "$file" "${file%.xml}.edefg"; done
You must go to all dir's.
Would be something like
find A -type d -name c | while read dir; do
cd ${dir} || continue
unzip -u *.zip
call_rename_xml_function
cd - # not needed in bash where this code is performed in a subshell
done
EDIT: Added -u flag for when the script is called twice.

Unzip Folders to Parent Directory Keeping Zipped Folder Name

I have a file structure as follows:
archives/
zips/
zipfolder1.zip
zipfolder2.zip
zipfolder3.zip
...
zipfolderN.zip
I have a script that unzips the folders to the parent directory "archives", but it is unzipping the contents of the folders to the "archives" directory. I need the zipped folders to remain as folders under the "archives" directory. The resultant file structure should look like this:
archives/
zips/
zipfolder1.zip
zipfolder2.zip
...
zipfolder1/
contents...
zipfolder2/
contents...
...
I am currently using the following:
find /home/username/archives/zips/*.zip -type f | xargs -i unzip -d ../ -q '{}'
How can I modify this line to keep the original folder names? Is it as simple as using ../*?
You could use basename to extract the zip into the desired directory:
find /home/username/archives/zips/*.zip -type f -exec sh -c 'unzip -q -d ../"$(basename "{}" .zip)" "{}"' \;

shell entering each folder and zip content

So I have some folder
|-Folder1
||-SubFolder1
||-SubFolder2
|-Folder2
||-SubFolder3
||-SubFolder4
Each subfolder contains several jpg I want to zip to the root folder...
I'm a little bit stuck on "How to enter each folder"
Here is my code:
find ./ -type f -name '*.jpg' | while IFS= read i
do
foldName=${PWD##*/}
zip ../../foldName *
done
The better would be to store FolderName+SubFolderName and give it to the zip command as name...
Zipping JPEGs (for Compression) is Usually Wasted Effort
First of all, attempting to compress already-compressed formats like JPEG files is usually a waste of time, and can sometimes result in archives that are larger than the original files. However, it is sometimes useful to do so for the convenience of having a bunch of files in a single package.
Just something to keep in mind. YMMV.
Use Find's -execdir Flag
What you need is the find utility's -execdir flag. The GNU find man page says:
-execdir command {} +
Like -exec, but the specified command is run from the subdirec‐
tory containing the matched file, which is not normally the
directory in which you started find.
For example, given the following test corpus:
cd /tmp
mkdir -p foo/bar/baz
touch foo/bar/1.jpg
touch foo/bar/baz/2.jpg
you can zip the entire set of files with find while excluding the path information with a single invocation. For example:
find /tmp/foo -name \*jpg -execdir zip /tmp/my.zip {} +
Use Zip's --junk-paths Flag
The zip utility on many systems supports a --junk-paths flag. The man page for zip says:
--junk-paths
Store just the name of a saved file (junk the path), and do not
store directory names.
So, if your find utility doesn't support -execdir, but you do have a zip that supports junking paths, you could do this instead:
find /tmp/foo -name \*jpg -print0 | xargs -0 zip --junk-paths /tmp/my.zip
You can use dirname to get the directory name of a file/directory it is located in.
You can also simplify the find command to search only for directories by using -type d. Then you should use basename to get only the name of the subdirs:
find ./*/* -type d | while read line; do
zip --junk-paths "$(basename $line)" $line/*.jpg
done
Explanation
find ./*/* -type d
will print out all directories located in ./*/* which will result in all subdirs of directories located in the current dir
while read line reads each line from the stream and stores it in the variable "line". Thus $line will be the relative path to the subdir, e.g. "Folder1/Subdir2"
"$(basename $line)" returns the only the name of the subdir, e.g. "Subdir2"
Update: add --junk-paths to the zip command if you do not want the directy paths to be stored in the zip filde
So a little check, I finally got something working:
find ./*/* -type d | while read line; do
#printf '%s\n' "$line"
zip ./"$line" "$line"/*.jpg
done
But this create un archive containing:
Subfolder.zip
Folder
|-Subfolder
||-File1.jpg
||-File2.jpg
||-File3.jpg
Instead I fold like it to be:
Subfolder.zip
|-File1.jpg
|-File2.jpg
|-File3.jpg
So I tried using basename and dirname in differnet combination...Always got some error...
And just to learn how to: what if I would like the new archive to be created in the same root directory as "Folder"?
Ok finally got it!
find ./* -name \*.zip -type f -print0 | xargs -0 rm -rf
find ./*/* -type d | while read line; do
#printf '%s\n' "$line"
zip --junk-paths ./"$line" "$line"/*.jpg
done
find . -name \*.zip -type f -mindepth 2 -exec mv -- '{}' . \;
In first row I simply remove all .zip files,
Then I zip all and in the final row I move all zip to the root directory!
Thanks everbody for your help!

Resources