More spaces in filenames [was: No batch processing on Linux?]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sat, Sep 25, 2010 at 8:49 AM, Marc Groenewegen <marcg at dinkum.nl> wrote:
Why would one want spaces in filenames anyway ?

Cheers,
Marc

#! /bin/bash
#
# spaces2underscores.sh

echo
echo "This command changes all spaces in file names into underscores"
echo "  for ALL FILES IN THIS DIRECTORY !"
echo "  Type ENTER to continue or ^C to quit"

read dummy

for f in *; do
   oldname=`echo $f |sed 's/ /~/g'`
   newname=`echo $f |sed 's/ /_/g'`
   if [ $oldname != $newname ]
   then
     echo mv $f $newname
     mv "$f" $newname
   fi
done

The  if [ $oldname != $newname ] bit is nice; avoids errors with mv.

You could also get rid of other nasties with

newname=`echo $f | tr ' ' '_' | tr -d '[{}(),\!]' | tr -d "\'" | sed 's/_-_/_/g'`

But if directories have spaces as well, one solution is (from somewhere on the Arch Linux forums)

#! /bin/bash
# remove spaces from directory names and contents recursively

IFS=$'\n'

function cleanup {
    if [ -d "$1" ];
    then
        DIR="$1"
        cd "$DIR"
    fi

    pwd

    for f in `find . -maxdepth 1`; do
    [ -d "$f" -a "$f" != "." ] && (cleanup "$f")

        file=$(echo $f | tr ' ' '_' )
        [ -e "$f" ] && [ ! -e "$file" ] && mv "$f" "$file"

    done
    [ "$DIR" ] && cd - > /dev/null
}
cleanup .

unset IFS

Even better is this (from perlmonks.org/?node_id=27739)

find2perl . -depth -eval 'my $o=$_; tr/_/ /; -e or rename $o,$_ or warn "cannot rename $o to $_: $!"' | perl

Does the lot in one hit.
_______________________________________________
Linux-audio-user mailing list
Linux-audio-user@xxxxxxxxxxxxxxxxxxxx
http://lists.linuxaudio.org/listinfo/linux-audio-user


[Index of Archives]     [Linux Sound]     [ALSA Users]     [Pulse Audio]     [ALSA Devel]     [Sox Users]     [Linux Media]     [Kernel]     [Photo Sharing]     [Gimp]     [Yosemite News]     [Linux Media]

  Powered by Linux