1

In my ubuntu 12.04 set-up, there are lots of wallpaper images in the directory /usr/share/wallpapers. For example, one might be

usr/share/wallpapers/Leafs_Labyrinth/contents/images/1600x1200.jpg

What I want to do is travel through the wallpapers directory, pick out all the images of size 1600x... or 1680x... and copy them to another folder, but renamed, so that the above image would end up being called Leafs_Labyrinth1600x1200.jpg.

I can't use find alone here; I expect I need to use some sort of shell script, of which I have very little experience. Is there an easy "natural" way to do this?

4

This should work based on the details of your question. You can save the following in a file, change mydirectory to the name of your target folder, and then run bash name_of_script:

#!/bin/bash

# * matches any string | [08] matches 0 and 8
for image in /usr/share/wallpapers/*/contents/images/16[08]0x*.jpg; do
    # create variables by cutting $image in pieces separated by /
    name=$(awk -F/ '{print $5}' <<<$image)
    file=$(awk -F/ '{print $8}' <<<$image)

    # copy to "mydirectory"
    cp "$image" mydirectory/"$name""$file"
done

The same can be simplified like this:

for image in /usr/share/wallpapers/*/contents/images/16[08]0x*.jpg; do
    cp "$image" mydirectory/"$(awk -F/ '{print $5 $8}' <<<$image)"
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.