Find the answer to your Linux question:
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 ...
  1. #1
    Just 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.

  2. #2
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    Posts
    935
    You shouldn't need a script. Employ the "r" and "t" option.
    Debian GNU/Linux -- You know you want it.

  3. #3
    Just 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?

  4. #4
    Linux 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/ \;

  5. #5
    Just 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:
    Code:
    find /media/cdrom -type f -exec file '{}' \;|grep image
    which finds all images of various types, but how do I pipe this into "cp"?
    Code:
    find ./t1 -type f -exec file '{}' \;|grep image|xargs -0 cp {} /home/user/pics
    gives "Not a directory" error citing the entire output of the "File" command.

  6. #6
    Linux Newbie
    Join Date
    Mar 2009
    Posts
    228
    For multiple filetypes, say .jpg and .mpeg

    Code:
    find /media/cdrom -type f \( -name "*.jpg" -o -name "*.mpeg" \) -exec cp {} /home/user/pics/ \;
    Checking the type of file with 'file':

    Code:
    find ./t1 -type f -exec file '{}' \; | grep image | awk '{print $1}' | \
    sed 's/:$//' | xargs -I xxx cp xxx /home/user/pics

  7. #7
    Just 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...