[Klug-general] Space to underscore?

David Halliday david.halliday at gmail.com
Tue May 15 12:44:49 UTC 2012


Hi,

You should be able to modify this:
http://www.sharons.org.uk/taste/archives/2012/05/index.html#e2012-05-01T00_50_12.txt


   - If you want to rename directories also, then just remove the: -type f
   - If you want to do ALL files then remove the sections relating to
name: -name
   '*.MP3'
   - If you want to go based on a name filter, you can actualy keep the
   command more simple using case insensitive name matching: -iname "*.mp3"
   - Rather than the more convoluted: \( -name '*.mp3' -o -name '*.MP3' \)

You should end up with something like this:

#!/bin/bash

find $1 -name "* *" |
while IFS= read -r NAME;
do
  DEST=`echo "$NAME" | tr ' ' '_'`
  mv -v "${NAME}" "${DEST}"
done
Tested by adding that to a script: test.sh

Sample usage:
[user at host]$ mkdir test
[user at host]$ touch test/"file one.txt"
[user at host]$ touch test/"file two.txt"
[user at host]$ touch test/"filethree.txt"
[user at host]$ ls test/
file one.txt  filethree.txt  file two.txt
[user at host]$ ./test.sh test/
`test/file one.txt' -> `test/file_one.txt'
`test/file two.txt' -> `test/file_two.txt'
[user at host]$ ls test/
file_one.txt  filethree.txt  file_two.txt


The -name "* *" searches for anything with a name containing a space (no
pint in modifying files without spaces). You can change it to "* *.mp3" for
mp3 files only. Or use -iname for case insensitive searches.


On 14 May 2012 23:04, sharon kimble <boudiccas at talktalk.net> wrote:

> This is probably quite easy if you already know the answer, but I'm not
> very
> good at scripting.What I'm looking for is a script to go through my
> /home/foo/Music directory converting all the album titles with spaces in to
> convert them to underscores.
>
> Can anyone help please?
>
> Thanks
> Sharon.
> --
> A taste of linux = http://www.sharons.org.uk/taste/index.html
> efever = http://www.efever.blogspot.com/
> efever = http://sharon04.livejournal.com/
> Debian 6,0.5, Gnome 1:2.30+7, LibreOffice 3.4.6
> Registered Linux user 334501
>
> _______________________________________________
> Kent mailing list
> Kent at mailman.lug.org.uk
> https://mailman.lug.org.uk/mailman/listinfo/kent
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.lug.org.uk/pipermail/kent/attachments/20120515/9cbf3649/attachment.htm>


More information about the Kent mailing list