0

I have a folder with several pictures. I would like to convert the file names all into small letters and then delete all occurrences of certain strings.

For example, from BILD02_CMYK_freigestellt_RENDER_3D.jpg then the cmyk, render, 3d and _ should be deleted when connected to the words. So far, I have the following combo, but it will be the first passage only the first occurrence removed and not every.

// Rename names to lowercase
for file in *; 
do 
   mv "$file" "$file.tmp"; 
   mv "$file.tmp" "`echo $file | tr "[:upper:]" "[:lower:]"`"; 
done

// Remove cmyk from filename
// optimierung: macht es nur einmal bei einer datei - ----
for file in *; do mv "${file}" "${file/cmyk/}"; done
for file in *; do mv "${file}" "${file/freigestellt/}"; done
for file in *; do mv "${file}" "${file/-/_}"; done

1 Answer 1

1
rename -n '$_=lc; s/(cmyk|freigestellt|render|3d|_|-)//g' *.jpg

This uses the perl rename command (also known as prename and file-rename). It is not to be confused with the rename command from util-linux, which has completely different command-line options and capabilities.

It first converts the filename to lowercase using perl's lc() function, then removes all the words and characters you mentioned in either your text or your example code.

The -n option causes the rename to be a dry-run - it will only show you what it would do if you let it. To make it actually rename the files, remove the -n or replace it with -v for verbose output.

e.g.

$ touch BILD{00..05}_CMYK_freigestellt_RENDER_3D.jpg
$ rename -v '$_=lc; s/(cmyk|freigestellt|render|3d|_|-)//g' *.jpg
BILD00_CMYK_freigestellt_RENDER_3D.jpg renamed as bild00.jpg
BILD01_CMYK_freigestellt_RENDER_3D.jpg renamed as bild01.jpg
BILD02_CMYK_freigestellt_RENDER_3D.jpg renamed as bild02.jpg
BILD03_CMYK_freigestellt_RENDER_3D.jpg renamed as bild03.jpg
BILD04_CMYK_freigestellt_RENDER_3D.jpg renamed as bild04.jpg
BILD05_CMYK_freigestellt_RENDER_3D.jpg renamed as bild05.jpg
2
  • BTW, if you're running debian or similar sudo apt-get install file-rename. Other distros probably have it packaged too. You can also find it on CPAN at metacpan.org/release/File-Rename
    – cas
    Commented Sep 27, 2019 at 10:58
  • Thank you very much. rename -v '$_=lc; s/(front|cmyk|freigestellt|render|3d|_|-)//g' *.(jpg|png)
    – Julian
    Commented Sep 27, 2019 at 11:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .