1

I have a folder with images and I need to copy them elsewhere removing special chars in the progress. Lets say I have this

Folder1/ImageÑ%1.jpg
Folder1/ImageÑ%1-70x70.jpg
Folder1/ImageÑ%2.jpg
Folder1/ImageÑ%2-70x70.jpg
Folder1/ImageÑ%3.jpg
Folder1/ImageÑ%3-100x100.jpg
Folder1/ImageÑ%4.jpg
Folder1/ImageÑ%4-100x100.jpg

And I want to copy just the files that doesn't have "-70x70" or "-100x100" in the name to Folder2 and be like this (without any special char):

Folder2/Image1.jpg
Folder2/Image2.jpg
Folder2/Image3.jpg
Folder2/Image4.jpg

I've managed to copy the files, but I have no clue how to rename the files (and remove the special chars) in the same step. I think it's using SED but I can't figure out how.

find Folder1 -type f -regextype posix-extended \( ! -regex '.+\-[0-9]{2,4}x[0-9]{2,4}\.jpg' \) -print0 |  xargs -0 cp -p --target-directory=Folder2

Thanks!

3 Answers 3

4

To remove weird characters, I recommend you detox

Then :

find Folder1 \( ! -name 'Image*-70x70*' -a ! -name 'Image*-100x100*' \) |
    xargs -i% cp -p % Folder2

edit

find explanations :

  • the ! character is a negation
  • -a option is a AND condition
  • the parenthesis are there to enclose the conditions

Edit2

To do it on the fly like you ask (for substituting weird characters):

find Folder1 \( ! -name 'Image*-70x70*' -a ! -name 'Image*-100x100*' \) \
    -exec bash -c '
        file=$(echo "$1" | perl -pe "s/\303\221%/_weird_/g")
        cp -p "$1" "Folder2/$file"
    ' -- {} \;
2
  • Edited mistake on find option Oct 1, 2012 at 17:23
  • Is it possible to run detox in the same step? I mean I can't rename files from Folder1 Oct 1, 2012 at 17:33
2

This maybe works for you:

#!/bin/bash
DIR=/your/Folder1
DEST=/your/Folder2

for f in `ls -1 $DIR | grep .jpg | egrep -v ".+\-[0-9]{2,4}x[0-9]{2,4}\.jpg"`
do
        f=$(basename $f)
        F=$(echo "$f" | sed "s/[^A-Z|a-z|0-9|\.]//g;")
        cp -p "$DIR/$f" "$DEST/$F"
done

You can use MV instead of CP if you needit.

EDITED

This will get only the files you have to copy:

ls -1 $DIR/*.jpg | egrep -v ".+\-[0-9]{2,4}x[0-9]{2,4}\.jpg

basename command get only the file name in order to replace weir characters and prepare to move sed it's replacing all character if the aren't letters and numbers (and a dot for file extension)

1

Something like this perhaps?

for file in Folder1/*.jpg; do
  case $file in
    *-70x70.jpg | *-100x100.jpg) ;;
    *) cp "$file" "Folder2/Image${file#Folder1/Image?}" ;;
  esac
done

The construct ${variable#pattern} removes any match for pattern from the beginning of the value of $variable. The pattern Folder1/Image? matches the literal text Folder1/Image (we prepend back the static Image part then) and one arbitrary character.

If the offending character is not a single byte, try doubling the question mark for a start; that will match two arbitrary characters (or bytes, if your locale etc are configured that way). If you have a combining Unicode sequence, it takes much more than that, though -- a hex dump of the actual bytes used to represent the file name would be helpful for diagnosing this. Anyway, you can simply trim from the other end instead, although that gets a bit more involved;

  *) base=${file#Folder1/}
     prefix=${base%%[0-9]*}
     cp "$file" "Folder2/Image${base#$prefix}" ;;

The variant ${variable%pattern} trims from the end instead of the beginning, and doubling the # or % causes the shell to trim the longest possible match instead of the shortest. (I believe the longest-possible matching is a Bash extension?)

2
  • Nope, the weird character is still present in Folder2. Fail. Oct 1, 2012 at 17:57
  • Works for me. But I edited my answer; perhaps it works for you now?
    – tripleee
    Oct 1, 2012 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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