Filename manipulation scripts

Bash script to convert filenames to lowercase and spaces in hypens

#!/bin/bash

for x in ./*; do
  if [ -f "$x" ]; then
    lc=${x,,}            # convert all letters to lowercase
    y=${lc// /-}         # replace spaces by hyphens
    if [ "$x" != "$y" ]; then
      mv "$x" "$y"
    fi
  fi
done

Bash script to capitalise filenames recursively

#!/bin/bash
for i in /files/to/covert/directory/*.extension;
    do new=`echo "$i" | sed -e 's/^./\U&/'`;
    mv "$i" "$new";
done;

Perl script to rename LOT of files based on a list with the wanted filenames

#!/usr/bin/perl

my $dir = '/path/to/imgs/directory';
my $names = '/path/to/file/with/the/wanted/names';
my $files = '/path/to/file/with/the/real/name';

chdir $dir or
    die "Could not change dir to $dir: $!\n";

open NAMES, $names or die "Could not open $names: $!\n";

open FILES, $files or die "Could not open $files: $!\n";

my $name = <NAMES>;
chomp $name;

my $file = <FILES>;
chomp $file;

while($name && $file) {
    print "renaming $file to $name...\n";
    rename $file, $name;
    $name = <NAMES>;
    chomp $name;
    $file = <FILES>;
    chomp $file;
}

close NAMES;
close FILES;

Tags: