Shell Script to search and copy jpg to Another Folder.
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.
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
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.
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.
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
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.
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.
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
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
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.