Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi all
I have several (more than 10000 ) folders lets call them A,B,C,D..... all located inside the directory
Code:
/home/somestuff.
Each one of these folders contains 1 or more zip files.
E.g.
Code:
Folder A contains aa1.zip aa2.zip (if extracted they give aaa file)
Folder B contains bbb.zip (if extracted it gives bbb file)
....
Folder P contains pp1.zi pp2.zip pp3.zip pp4.zip (if extracted they give ppp file)
and so on...
i want to extract the content of these zip files to each parent directory using command line.
I tried so far several commands with no sucess.
I run the following command from /home/somestuff:
//-----------------------------------------------------------------------------------------------------//
@TobiSGD thanks for explaining me that about dirname but the following:
does extract the files but NOT EACH ONE INSIDE ITS INITIAL SUBFOLDER LIKE DESIRED BUT ALL OF THEM ARE EXTRACTED INSIDE THE PARENTFOLDER WHICH IS somestuff
so the above has the same result like mine:
//-----------------------------------------------------------------------------------------------------//
I repeat i want a command which i will run while on somestuff. This should cause all zip files that are inside the subfolders of sumestuff to extract their contents INSIDE EACH CORRESPONDING SUBFOLDER. The last 2 commands above run well ONLY IF I RUN THEM WHILE IN EACH SUBFOLDER....that means i should do this about 10000 ..times...
i hope u make sense with my english...
I would like to show you the possibility to extract to the directory according to filename, but I used 7za.
I know it's not full script like you would like to have, but for the test I have in the directory 2 zip files which contain files with subdirectories:
If you want a simple shell script you can place this in the root directory of the file structure you're working with. This is the combined ideas of other posts in this thread. I tested it with a sample set and it works on my system. Just change the $rootdir variable if you want to set your own path and run the script elsewhere. Use full path name and not relative.
Code:
#!/bin/bash
rootdir="$PWD"
find "$rootdir" -type f -name '*.zip' | while read file;do
cd "$(dirname "$file")"
if [ "$(dirname "$file")" = "$PWD" ];then
unzip "$file"
else
echo "error"
break
fi
done
cd "$rootdir"
It unzips zip files relative to the path of the actual zip file. The script accounts for folders with spaces in them as well.
Alternatively, you can delete the zip files after successfully determining the exit code of unzip (i.e. delete only if extraction was successful).
Note: The parenthesis are not supposed to be escaped when inside of $()
There's not much to change to get the desired method. Also the OP's first post was not clear about that. I see in subsequent posts they clarify what they want though. At any rate here's the revised script.
Code:
#!/bin/bash
rootdir="$PWD"
find "$rootdir" -type f -name '*.zip' | while read file;do
cd "$(dirname "$file")"
if [ "$(dirname "$file")" = "$PWD" ];then
mkdir "$(basename "$file" | cut -d. -f1)"
cd "$(basename "$file" | cut -d. -f1)"
unzip "$file"
else
echo "error"
break
fi
done
cd "$rootdir"
Added two lines and now it solves what the OP describes from what I can tell.
THANKS EVERYBODY FOUR TIME!!!!!!!!!
Especially the last 2 commenters (lithos & sag47) for their help!!!!
@sag47
Your last script:
Quote:
#!/bin/bash
rootdir="$PWD"
find "$rootdir" -type f -name '*.zip' | while read file;do
cd "$(dirname "$file")"
if [ "$(dirname "$file")" = "$PWD" ];then
mkdir "$(basename "$file" | cut -d. -f1)"
cd "$(basename "$file" | cut -d. -f1)"
unzip "$file"
else
echo "error"
break
fi
done
cd "$rootdir"
DOES EXACTLY WHAT I WANTED!!!
However I would be grateful if you could add some comments to each line so that i learn as well besides getting the job done
I dont understand why mkdir is needed since the subdirectories exist!!!
However I would be grateful if you could add some comments to each line so that i learn as well besides getting the job done
I dont understand why mkdir is needed since the subdirectories exist!!!
But what if it doesn't exist ? It doesn't "hurt" to create it, otherwise Unzip command would fail.
About the script:
Code:
#!/bin/bash
rootdir="$PWD"
find "$rootdir" -type f -name '*.zip' | while read file;do # find all .zip files in the directory
cd "$(dirname "$file")"
if [ "$(dirname "$file")" = "$PWD" ];then # if the file found is in the same directory as defined "rootdir"
mkdir "$(basename "$file" | cut -d. -f1)"
cd "$(basename "$file" | cut -d. -f1)"
unzip "$file" # unzip the file
else
echo "error"
break
fi
done
cd "$rootdir"
Just some addition if one wants to use solely find
Code:
-execdir command {} +
Like -exec, but the specified command is run from the subdirectory containing the matched file, which is not normally the directory in which you started find. This a much more secure method for invoking commands, as
it avoids race conditions during resolution of the paths to the matched files. As with the -exec action, the `+' form of -execdir will build a command line to process more than one matched file, but any given invoca‐
tion of command will only list files that exist in the same subdirectory. If you use this option, you must ensure that your $PATH environment variable does not reference `.'; otherwise, an attacker can run any com‐
mands they like by leaving an appropriately-named file in a directory in which you will run -execdir. The same applies to having entries in $PATH which are empty or which are not absolute directory names.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.