LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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

Reply
 
Search this Thread
Old 06-03-2009, 12:37 AM   #1
ajayan
Member
 
Registered: Dec 2007
Posts: 89

Rep: Reputation: 16
Shell Script to search and copy jpg to Another Folder.


Dear All,
My manager asked me to find out all the jpg files in a directory and copy to another destination directory.I tried to write a shell script.But the problem is this source directory contains thousands of subdirectories.The jpg files resides in all of these directories.We have to search inside all these sub directories.I am reallyconfused.Can Any one help me.I am only a beginner to Shell scripting.Thanking YoU
 
Old 06-03-2009, 12:46 AM   #2
rikxik
Member
 
Registered: Dec 2007
Posts: 88

Rep: Reputation: 19
Code:
find /main_directory -name "*.jpg" -exec cp -i {} /destination_directory/. \;
 
Old 06-03-2009, 12:48 AM   #3
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Look at the manpage for the "find" command.
You can look for files with the pattern "*.jpg". You can use -iname to find files in a directory and it's subdirectories. The -iname argument is case insensitive.

Also look at the -exec argument.

example:
find /path/to/directory/ -iname "*.jpg" -execdir cp '{}' /path/to/dest/dir/ \;
 
Old 06-03-2009, 02:07 AM   #4
ajayan
Member
 
Registered: Dec 2007
Posts: 89

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by jschiwal View Post
Look at the manpage for the "find" command.
You can look for files with the pattern "*.jpg". You can use -iname to find files in a directory and it's subdirectories. The -iname argument is case insensitive.

Also look at the -exec argument.

example:
find /path/to/directory/ -iname "*.jpg" -execdir cp '{}' /path/to/dest/dir/ \;
Yes.That works Fine Thankyou.But one question too.Whether we can rename the files with directory name also.Because the file with similar names will be overwrited.Not at a big problem.Can u please try with this
 
Old 06-03-2009, 02:22 AM   #5
rikxik
Member
 
Registered: Dec 2007
Posts: 88

Rep: Reputation: 19
Quote:
Originally Posted by ajayan View Post
Yes.That works Fine Thankyou.But one question too.Whether we can rename the files with directory name also.Because the file with similar names will be overwrited.Not at a big problem.Can u please try with this
Thats why I had put "cp -i" in my exec command - that will ask you just in case it finds multiple files with same name.
 
Old 09-20-2012, 10:18 PM   #6
saran8485@yahoo.com
LQ Newbie
 
Registered: Sep 2012
Posts: 4

Rep: Reputation: Disabled
Hi

I have been trying to find files(Some hundres) from multiple directories.I have let us say 200 files in Heros.txt I need find and copy all those files from multiple subdirectroies under /data.Developed below script and executed it takes few secs and the completes with no action.I checked the destination folder but none was copied.Please share you idea to complete this task.


filename=/home/sankars/Heros.txt
count=0
while read LINE;
do
find /data -name LINE -exec cp '{}' /home/sankars/zip_thumbs \;
let count++
done < $filename


Thanks
Saravanan

Last edited by saran8485@yahoo.com; 09-20-2012 at 10:19 PM.
 
Old 09-20-2012, 10:27 PM   #7
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 2,657

Rep: Reputation: 951Reputation: 951Reputation: 951Reputation: 951Reputation: 951Reputation: 951Reputation: 951Reputation: 951
Quote:
Originally Posted by saran8485@yahoo.com View Post
Hi

I have been trying to find files(Some hundres) from multiple directories.I have let us say 200 files in Heros.txt I need find and copy all those files from multiple subdirectroies under /data.Developed below script and executed it takes few secs and the completes with no action.I checked the destination folder but none was copied.Please share you idea to complete this task.


filename=/home/sankars/Heros.txt
count=0
while read LINE;
do
find /data -name LINE -exec cp '{}' /home/sankars/zip_thumbs \;
let count++
done < $filename


Thanks
Saravanan
You need a dollar sign in front of the LINE in find. The way it's written you're just searching for a file called LINE 200 times. The dollar sign in front tells it to substitute the contents of the variable LINE, rather than using the literal string "LINE". You should also add a trailing slash to the destination directory, so that it will error out if the directory doesn't exist. The way it's written it will copy each file, one by one, to a new file called "zip_thumbs", if the directory "zip_thumbs" doesn't already exist.

Code:
filename=/home/sankars/Heros.txt
count=0
while read LINE;
do
find /data -name $LINE -exec cp '{}' /home/sankars/zip_thumbs/ \;
let count++
done < $filename
 
1 members found this post helpful.
Old 09-21-2012, 12:33 AM   #8
arun5002
Member
 
Registered: Aug 2011
Location: Chennai,India
Distribution: Redhat,Centos,Ubuntu,Dedian
Posts: 549
Blog Entries: 5

Rep: Reputation: Disabled
These the shell script you can use to find and backup the jpg file from source to destination folder


#Script -Purpose:Backup script
# Exits with zero if no error.
#Before running the script create a folder /destination/jpg
#Function to copy the jpeg file form source to destination scripts



# Step -1 Function to archieve file using date and time

date=`/bin/date "+%Y.%m.%d.%H.%M.%S"`


# Step -2 Function to create folder on /root/logs with Timestamp

mkdir -p /destination/$date


# Step -3 Function to find & copy jpeg from /source to /destination


find /source/ -iname "*.jpg" -execdir cp '{}' /destination/$date \;


#Step -4 Function to compress the jpe copied file to /destination/jpg folder


tar -cvzf /destination/$date /destination/jpg/$date.tar.gz


#Step -5 Function to remove the copied jpg file

rm -rf /destination/*


#Step -4 Function to Print the logs Backup status

echo "$(date) jpg Backuped successfully ">>/destination/jpg/jpgbackup-status-$date.log

#Step -5 Function to trigger mail to user regarding backup status

mailid= $@gmail.com

#Step -5 Function to trigger mail after log backup status

mail -s '$(date) jpg copied Successfully $(hostname) - Successful' $mailid
 
Old 09-21-2012, 12:37 AM   #9
saran8485@yahoo.com
LQ Newbie
 
Registered: Sep 2012
Posts: 4

Rep: Reputation: Disabled
Quote:
Originally Posted by suicidaleggroll View Post
You need a dollar sign in front of the LINE in find. The way it's written you're just searching for a file called LINE 200 times. The dollar sign in front tells it to substitute the contents of the variable LINE, rather than using the literal string "LINE". You should also add a trailing slash to the destination directory, so that it will error out if the directory doesn't exist. The way it's written it will copy each file, one by one, to a new file called "zip_thumbs", if the directory "zip_thumbs" doesn't already exist.

Code:
filename=/home/sankars/Heros.txt
count=0
while read LINE;
do
find /data -name $LINE -exec cp '{}' /home/sankars/zip_thumbs/ \;
let count++
done < $filename



Hi

Thanks for the advice..it works now..

Saravanan
 
Old 09-23-2012, 06:46 AM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
Here are a couple of good links about using find:
http://mywiki.wooledge.org/UsingFind
http://www.grymoire.com/Unix/Find.html


By the way, please don't re-open old threads when asking new questions. Start a new one, and link back to the old one if it's necessary to refer to what it contains.

Only reply to old threads if you have something new and important to add to THAT conversation.

In addition, please use ***[code][/code] tags*** around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, bolding, colors, or other fancy formatting.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script to copy contents of folder brian.m Linux - General 8 05-01-2009 06:08 PM
script to copy configuration folder to all users irishbitte Ubuntu 3 11-12-2008 06:02 AM
Need a script to search and replace text in file using shell script unixlearner Programming 14 06-21-2007 10:37 PM
Cannot "Copy To" .jpg file to wallpaper folder LeanPudLou Suse/Novell 5 11-08-2004 08:24 AM
How to copy in a script files to a samba folder? Julianus Linux - Networking 1 10-09-2004 07:29 PM


All times are GMT -5. The time now is 06:21 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration