Loop to copy like files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Loop to copy like files
# 1  
Old 06-16-2010
Loop to copy like files

Hi,

I need to write a script that copies all .zip files in the subdirectories of ~100 folders. No clue how to write a loop that goes into each folder, searches for a .zip file, and copies it and extracts it to a unique location.

I imagine something like
cp -f /home/folder1/*.zip /home/NameOfFolderOne

but then how do i get it to copy to a unique folder name, which is the title of the zip file?

Thanks,
# 2  
Old 06-16-2010
You can loop through the zip files.
Code:
for file in *.zip
do
 zipdir=${file%.*}
 mkdir $zipdir || echo "unable to create $zipdir"
 cp $file $zipdir || echo "unable to copy $file"
done

you can also extract the zip file inside the loop at the same time.
# 3  
Old 06-16-2010
Thanks but there's a few problems I received

The zip files are all in subdirectories, i.e. in /home/folder1/folder1A, /home/folder1/folder1B, etc., with one zip file per folder in the directory /home/folder1/___/. I need to unzip each zip file and put it into a different directory, which is titled just /home/filename, not /home/zipdir/

It currently returns an error saying file exists

Also, when I unzip, how do I get it to unzip it to the correct folder?



Maybe this is too confusing. All I want is to unzip the contents of each 'filename.zip' file in those subdirectories into its own folder called /home/'filename'

Is there an unzip to {create directory} command I can run in a loop?
# 4  
Old 06-16-2010
You can cd to newly created directory and unzip the file.

Instead of that, you have a ready-made command for that.
Have a look at the man page of unzip.
Code:
[-d exdir]
    An optional directory to which to extract files. By default, all files and subdirectories are recreated in the current directory; 
    the -d option allows extraction in an arbitrary directory (always assuming one has permission to write to the directory). This option


Ok. so zip files are not at the single location. Also you dont want copy the actual file but the extract the file?
probably you should try:

Code:
for file in $(find /home/folder1 -name "*.zip" -type f 2>/dev/null)
do
 zipdir=${file##*\/}
 zipdir=${zipdir%.*}
 zipdir=/home/${zipdir}
 mkdir $zipdir || echo "unable to create $zipdir"
 unzip $file -d $zipdir || echo "unzip failed for $file"
done

Check for unzip command syntax and test it first, as i never used that command.

---------- Post updated at 01:12 AM ---------- Previous update was at 01:06 AM ----------

I am assuming that your filename doesn't contains spaces.
# 5  
Old 06-16-2010
Seems to be getting closer

Problem now is it outputs a 1000 entries of:
error: cannot create /cygdrive/'extractedfile.whatever'
where 'extractedfile.whatever' is the name of each file in the compressed *.zip file.

I need to make it go to /cygdrive/c/UNZIPPED/'zipfilename'
(where UNZIPPED is just some random master folder to keep these all in)
And I need the folders to be named whatever their zip file is named, not each individual compressed file within the zip file.

I've played around with the code but can't seem to get by this, otherwise it works great and the unzip -d works fine. Thanks for your help so far!
# 6  
Old 06-16-2010
MySQL

Code:
for i in `find /home/folder1 -name "*.zip* -type f`
do
folder=`echo ${i} | awk -F'.' '{print $1}'`
mkdir /home/${folder}/
cp ${i} /home/${folder}/.
unzip /home/${folder}/$(echo ${i} | sed -e 's/\/.*\///')
done


Last edited by 3junior; 06-16-2010 at 07:19 PM..
# 7  
Old 06-17-2010
thanks a lot, works great!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Copy files from one drive to another, keeping most recently modified files

Hi all, I am a bit of a beginner with shell scripting.. What I want to do is merge two drives, for example moving all data from X to Y. If a file in X doesn't exist in Y, it will be moved there. If a file in X also exists in Y, the most recently modified file will be moved to (or kept) in... (5 Replies)
Discussion started by: apocolapse
5 Replies

2. Red Hat

Unable to copy files due to many files in directory

I have directory that has some billion file inside , i tried copy some files for specific date but it's always did not respond for long time and did not give any result.. i tried everything with find command and also with xargs.. even this command find . -mtime -2 -print | xargs ls -d did not... (2 Replies)
Discussion started by: before4
2 Replies

3. Shell Programming and Scripting

KSH Script -- loop and data copy question

I am trying to write a script that will allow me to train others with commands that I run manually by only allowing the exact command before continuing onto the next set of commands. Here is where I come into an issue. I have changed the directories for this post. Software we run creates files... (2 Replies)
Discussion started by: hurtzdonut
2 Replies

4. Shell Programming and Scripting

Loop through text file > Copy Folder > Edit XML files in bulk?

I have a text file which contains lines in this format - it contains 105 lines in total, but I'm just putting 4 here to keep it short: 58571,east_ppl_ppla_por 58788,east_pcy_hd_por 58704,east_pcy_ga_por 58697,east_pcy_pcybs_por It's called id_key.txt I have a sample folder called... (9 Replies)
Discussion started by: biscuitcreek
9 Replies

5. UNIX Desktop Questions & Answers

Copy all files in 1 directory to another usinge for-in loop

I was looking to get some help with copying files in one directory to another using a for-in loop. My script file is called copyfile and here is what I have: for file in $(ls -a $1) do cp $file ~/dir-2 done When I run copyfile dir-1 this is what I get cp: omitting directory `.'... (1 Reply)
Discussion started by: Trinimini
1 Replies

6. Shell Programming and Scripting

Loop folders, delete files, copy new ones

Folks, I am hopeful that you may be able to help me out with writing a script that can be run nightly (as cron?) to loop through all subfolders within the "/media" directory, delete all of the files in each of them, and then copy in all of the files from the "/home//sansa" directory to each of... (6 Replies)
Discussion started by: acraig
6 Replies

7. Shell Programming and Scripting

how to copy files followed by list of names of all the files in /etc?

....... (2 Replies)
Discussion started by: pcbuilder
2 Replies

8. Solaris

How to safely copy full filesystems with large files (10Gb files)

Hello everyone. Need some help copying a filesystem. The situation is this: I have an oracle DB mounted on /u01 and need to copy it to /u02. /u01 is 500 Gb and /u02 is 300 Gb. The size used on /u01 is 187 Gb. This is running on solaris 9 and both filesystems are UFS. I have tried to do it using:... (14 Replies)
Discussion started by: dragonov7
14 Replies

9. Shell Programming and Scripting

while loop to copy on failure

I'm trying to do an automated SCP (passwordless auth is already set up) from a bash script... My problem is that on the receiving end, the computer will sometimes NOT actually get my files. BUT if I loop enough times (by hand at the moment) calling an 'ls' on those files, I can see if the files... (2 Replies)
Discussion started by: jjinno
2 Replies

10. Shell Programming and Scripting

Unzip, copy, and delete loop

Hey there, I am trying to move zipped text files from a remote server to a remote tape storage facility, through my home directory. What I want to do is get the zip file (using scp), unzip it, copy the output text file which was inside (using rfcp) to the tape storage server, and then delete... (3 Replies)
Discussion started by: spyne
3 Replies
Login or Register to Ask a Question