LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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
 
LinkBack Search this Thread
Old 06-03-2009, 01:37 AM   #1
ajayan
Member
 
Registered: Dec 2007
Posts: 88

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, 01: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, 01:48 AM   #3
jschiwal
Moderator
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 14,971

Rep: Reputation: 527Reputation: 527Reputation: 527Reputation: 527Reputation: 527Reputation: 527
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, 03:07 AM   #4
ajayan
Member
 
Registered: Dec 2007
Posts: 88

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, 03: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.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are 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 07:08 PM
script to copy configuration folder to all users irishbitte Ubuntu 3 11-12-2008 07:02 AM
Need a script to search and replace text in file using shell script unixlearner Programming 14 06-21-2007 11:37 PM
Cannot "Copy To" .jpg file to wallpaper folder LeanPudLou Suse/Novell 5 11-08-2004 09:24 AM
How to copy in a script files to a samba folder? Julianus Linux - Networking 1 10-09-2004 08:29 PM


All times are GMT -5. The time now is 02:42 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
Open Source Consulting | Domain Registration