LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 06-02-2005, 04:06 PM   #1
scoops98
Member
 
Registered: Nov 2001
Location: Cornwall
Distribution: Gentoo, Debian,Knoppix
Posts: 85

Rep: Reputation: 15
Script to find all picture files on a HDD and move them


Hi all,
What i want is to write a script that will look for files with a certain extension (e,g *jpg or *.mp3) on my hardrives and move or copy them into a folder of my choice. Its probably very easy {but not if you dont know where to start}. Could someone point me in the direction of any tutorials i should read to attempt this?
Thanks in advance for your time.
 
Old 06-02-2005, 04:46 PM   #2
hw-tph
Senior Member
 
Registered: Sep 2003
Location: Sweden
Distribution: Debian
Posts: 3,032

Rep: Reputation: 57
Type man find to learn about the mighty find command. Take care though, as it is a tad too easy to really mess things up.

This command will find and delete all files ending with *.jpg in your home directory and its subdirectories:
find ~ -type f -name *.jpg -exec rm -f {} \;

Breakdown of the above:
"~" expands to your home directory.
"-type f" will only list files (not directories, sockets, etc).
"-name *.jpg" will only list files ending with ".jpg".
"-exec rm -f {}": Execute the command for each file that is found. "rm -f" will delete without warning! {} expands to each filename.
"\;" is needed to terminate the -exec sequence above.


Håkan
 
Old 06-02-2005, 05:26 PM   #3
scoops98
Member
 
Registered: Nov 2001
Location: Cornwall
Distribution: Gentoo, Debian,Knoppix
Posts: 85

Original Poster
Rep: Reputation: 15
thanks hw-tph

it looks promising i'll do a dummy test run first because me and mess ups go hand in hand when it comes to linux
 
Old 06-03-2005, 01:34 AM   #4
hw-tph
Senior Member
 
Registered: Sep 2003
Location: Sweden
Distribution: Debian
Posts: 3,032

Rep: Reputation: 57
Oops! I just realized you said you wanted to move the files. I read the subject as "remove".
Well, just replace "rm -f {}" with "mv {} /path/to/pic_storage" or something to that effect.


Håkan
 
Old 06-06-2005, 12:42 PM   #5
scoops98
Member
 
Registered: Nov 2001
Location: Cornwall
Distribution: Gentoo, Debian,Knoppix
Posts: 85

Original Poster
Rep: Reputation: 15
thanks hw-tph

With your quidance i managed this.
find /mnt/ext_hd -name "*.jpg" -exec cp {} /home/scoops/pictures \;
It moves all jpg files from my external harddrive in my pictures folder.
I really should archive things better.

Next for my Mp3s, which is going to be alot worse (lots of Track1s etc)
 
Old 02-26-2007, 02:29 AM   #6
pacaman
LQ Newbie
 
Registered: Feb 2007
Posts: 4

Rep: Reputation: 0
auto renaming?

Good to know but that script will do auto rename? or how to?
 
Old 02-26-2007, 11:57 AM   #7
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Rep: Reputation: 51
depends on what you want to rename, and what you want to rename to.
 
Old 02-26-2007, 02:37 PM   #8
dv502
Member
 
Registered: Sep 2006
Location: USA - NYC
Distribution: Whatever icon you see!
Posts: 642

Rep: Reputation: 57
After you move or copy images, you can run this
shell script to rename the images.

#!/bin/bash


# This script will rename the image files


prefix=img
for i in *.[JjGg]*
do
ran=$(echo $RANDOM)
ext=$(echo "$i" | sed -e 's/.*\.//')
mv "$i" "${prefix}-${ran}.$ext"
done

exit 0

This script will rename the image files with the
prefix "img" follow by a random number while keeping the original extension. So, an image
called bigboobs.jpg will be rename to img-xxxx.jpg

Good Luck
 
Old 02-27-2007, 02:40 AM   #9
pacaman
LQ Newbie
 
Registered: Feb 2007
Posts: 4

Rep: Reputation: 0
Sorry guys, my mistake... I ment auto renaming while same file name in directory.

I have tons of pictures like "picture01.jpg" within directories. I have to collect all pictures (in directory and sub directory) to one directory. Script must rename file if the same filename is in target directory.

thx...
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I move files from a host to another in a script eantoranz Linux - Networking 7 08-12-2005 02:39 PM
find and move most recent files in dir backnine_99 Linux - Software 2 06-14-2005 12:03 PM
how to write a script to move files from one directory to another myeire Linux - Newbie 7 02-25-2005 10:01 AM
Script to Move files jain_rajesh Linux - Newbie 1 10-15-2004 08:53 AM
Need help with script to move files over a NFS Reggy Programming 1 03-19-2004 06:12 PM


All times are GMT -5. The time now is 06:31 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