You are not logged in.

#26 2009-01-25 19:30:00

n3y
Member
Registered: 2008-11-19
Posts: 4

Re: Mass renaming files (space to underscore)

I use this:

$ for i in *' '* ; do mv "$i" "${i// /_}" ; done

It changes only the file names of actual folder.
So you can build a script to change folders and apply this command if you wish.

Offline

#27 2009-01-25 19:57:49

n3y
Member
Registered: 2008-11-19
Posts: 4

Re: Mass renaming files (space to underscore)

To rename subdirectories files:
This script was downloaded from http://www.novell.com/coolsolutions/tools/15601.html

#!/bin/bash
# spacereplace v1.0.1 by Richard van Kampen - july 2005
function helpme {
echo ""
echo "Help for spacereplace"
echo ""
echo "spacereplace version 1.0.1 - written by Richard van Kampen"
echo "Tool to substitute spaces in file and directory names with underscores."
echo ""
echo "Usage: spacereplace [option] [PATH]"
echo ""
echo "List of options:"
echo ""
echo "-h or --help : show help."
echo "-r : recursive. Also commit changes to all sub-directories."
echo ""
echo "You have to specify the path to the directory you want to process. Not specifying a path will just display help"
echo ""
echo "Example: 'spacereplace -r /home/richard/video' changes all spaces to underscores in /home/richard/mp3 and all directories below /home/richard/mp3."
echo ""
exit
}

for arg in "$@" # grab command line options
do
if [ "$arg" != "-r" ] && [ "$arg" != "-h" ]; then
dir="$arg"
orgdir="$dir"
sub=" and sub-directories"
fi
case "$arg" in
-h | --help ) help=1 ;; # help option true.
-r ) recursive=1 ;; # recursive is true
esac
done

if [ "$dir" == "" ];then
helpme
fi
if [ ! -d "$dir" ]; then
echo "Directory $dir does not exist. Please try again with full path"
exit
fi

if [ "$help" = "1" ]; then # do this if -h option is used
helpme
fi

function delspaces {
strips=1;
teller=1; # iterate through 'contentgrab' array
cd $dir;
# rename @ because we need it. Do it 5 times
until [ $strips = "5" ];do
rename @ - *
let strips+=1
done
for i in $(ls | sed 's/\n//g' | sed 's/\ /@/g'); do
i=${i//@/ }
contentgrab[$teller]=$i
replacespace=${contentgrab[$teller]// /_}
if [ "${contentgrab[$teller]}" != "$replacespace" ] && [ ! -f "$replacespace" ] && [ ! -d "$replacespace" ]; then
mv -- "${contentgrab[$teller]}" "$replacespace"
fi
let teller+=1;
done
}

teller3=1;
function getdirs {
for i in $(echo */); do
if [ "$i" != "*/" ];then
j[$teller3]=${i%/}
contentdirs[$teller3]=$dir/${j[$teller3]}
let teller3+=1
fi
done
}

delspaces
getdirs
counter4=1
if [ "$recursive" = "1" ]; then
until [ "${contentdirs[$counter4]}" == "" ]; do
dir=${contentdirs[$counter4]}
let counter4+=1
delspaces
getdirs
done
orgdir="$orgdir$sub"
fi

echo ""
echo "Changed all spaces to underscores in $orgdir"

Last edited by n3y (2009-01-25 20:05:43)

Offline

#28 2009-01-25 21:31:01

test1000
Member
Registered: 2005-04-03
Posts: 834

Re: Mass renaming files (space to underscore)

there's a lot of multi-page answers here tongue hope none of those are needed.. (this thread makes the excellent point that trying to do anything fast in bash is a bad idea and you should rather resort to python or perl or whateva tongue)

that said though, this seemed to work for me:

shinings example worked with multiple files for me atleast and this worked also(in case shinings example didn't work because i'm missing something here because i'm tired..): find . -exec rename ' ' '_' {} \; (of which you could also easily add recursive functionality..)

Last edited by test1000 (2009-01-25 21:32:10)


KISS = "It can scarcely be denied that the supreme goal of all theory is to make the irreducible basic elements as simple and as few as possible without having to surrender the adequate representation of a single datum of experience." - Albert Einstein

Offline

#29 2009-01-25 21:35:25

SamC
Member
From: Calgary
Registered: 2008-05-13
Posts: 611
Website

Re: Mass renaming files (space to underscore)

However, this thread is getting close to 2 years old. Lock, please?

Offline

Board footer

Powered by FluxBB