Results 1 to 7 of 7
Hi, I need to write a script that will copy all image files from removable media (Everything in /media?) to a folder in my Home folder. Preferably without copying the ...
- 03-04-2009 #1Just Joined!
- Join Date
- Dec 2008
- Posts
- 13
Copying all image files
Hi, I need to write a script that will copy all image files from removable media (Everything in /media?) to a folder in my Home folder. Preferably without copying the paths, e.g., /media/cdrom/pic1.jpg and /media/cdrom/subfolder/pic2.jpg will both be copied to /home/user/pics. I know I have to use something like for and the cp command, but I'm new to programming and need some help getting started. Thanks.
- 03-04-2009 #2
You shouldn't need a script. Employ the "r" and "t" option.
Debian GNU/Linux -- You know you want it.
- 03-04-2009 #3Just Joined!
- Join Date
- Dec 2008
- Posts
- 13
Thanks. If some of the source files are in subdirectories, how do I get cp to just copy them all to the same target directory?
- 03-04-2009 #4Linux Newbie
- Join Date
- Mar 2009
- Posts
- 228
I would use the 'find' command as follows (using your example):
Code:find /media/cdrom -type f -name "*.jpg" -exec cp {} /home/user/pics/ \;
- 03-09-2009 #5Just Joined!
- Join Date
- Dec 2008
- Posts
- 13
Thank you, that would work. What if I need to find multiple file types, not just *.jpg? Also, I'd like to also find files of the correct type that may have incorrect or missing file extensions. The "file" command can do this. So far I have:
which finds all images of various types, but how do I pipe this into "cp"?Code:find /media/cdrom -type f -exec file '{}' \;|grep image
gives "Not a directory" error citing the entire output of the "File" command.Code:find ./t1 -type f -exec file '{}' \;|grep image|xargs -0 cp {} /home/user/pics
- 03-09-2009 #6Linux Newbie
- Join Date
- Mar 2009
- Posts
- 228
For multiple filetypes, say .jpg and .mpeg
Checking the type of file with 'file':Code:find /media/cdrom -type f \( -name "*.jpg" -o -name "*.mpeg" \) -exec cp {} /home/user/pics/ \;
Code:find ./t1 -type f -exec file '{}' \; | grep image | awk '{print $1}' | \ sed 's/:$//' | xargs -I xxx cp xxx /home/user/pics
- 03-14-2009 #7Just Joined!
- Join Date
- Dec 2008
- Posts
- 13
Thanks. Here's the final form:
Code:find ./t1 -type f -exec file -F:pixlp: '{}' \;|grep "image data"|sed 's/:pixlp:.*//'|xargs -I xxx cp xxx ./t2


Reply With Quote