LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Bash script to remove capitalisation and spaces form a filename (http://www.linuxquestions.org/questions/programming-9/bash-script-to-remove-capitalisation-and-spaces-form-a-filename-309567/)

scuzzman 04-04-2005 09:12 AM

Bash script to remove capitalisation and spaces form a filename
 
I need some help removing capitalisation and spaces from a filename. This is what I have so far, but you'll see the errors it's generating:

Code:

[scuzzy@slackdell /home/scuzzy/test]$ cat /usr/bin/upper2lower
#!/bin/bash

# upper2lower - convert all files in a directory from upper
# to lower case filenames and replace spaces with _

for f in *; do # turn all spaces into underscores
  mv \"$f\" `echo \"$f\" | tr [:space:]  _`
done

for g in *; do # make all characters lowercase
  mv $g `echo $g | tr A-Z a-z`
done

# EOF

[scuzzy@slackdell /home/scuzzy/test]$ ls
file\ with\ SPACES\ and\ CAPS
file\ with\ spaces_in_name
[scuzzy@slackdell /home/scuzzy/test]$ upper2lower
mv: when moving multiple files, last argument must be a directory
Try `mv --help' for more information.
mv: when moving multiple files, last argument must be a directory
Try `mv --help' for more information.
mv: when moving multiple files, last argument must be a directory
Try `mv --help' for more information.
mv: when moving multiple files, last argument must be a directory
Try `mv --help' for more information.
[scuzzy@slackdell /home/scuzzy/test]$

Any ideas what I'm doing wrong?

AnanthaP 04-04-2005 09:28 AM

Maybe, you should exclude . and .. (Just my 2 bits) or apply the `test` for regular files.

End

keefaz 04-04-2005 10:01 AM

Try :
Code:

for f in *; do
    file=$(echo $f | tr A-Z a-z | tr ' ' _)
    [ ! -f $file ] && mv "$f" $file
done


Dave Kelly 04-04-2005 09:25 PM

Re: Bash script to remove capitalisation and spaces form a filename
 
Quote:

Originally posted by scuzzman

[code]
[scuzzy@slackdell /home/scuzzy/test]$ upper2lower
mv: when moving multiple files, last argument must be a directory
Try `mv --help' for more information.

What variable holds the name of the directory?

jlliagre 04-05-2005 03:30 AM

Try that one:
Code:

  ls | while read src
  do
    dest="$(echo "$src" | tr ' ' _ | tr "[:upper:]" "[:lower:]")"
    if [ "$src" != "$dest" ]
    then
      if [ -f "$dest" ]
      then
        ls -l "$src" "$dest"
        echo "Can't rename \"$src\", \"$dest\" already exist"
      else
        echo "$src" "->" "$dest"
        mv "$src" "$dest"
      fi
    fi
  done


scuzzman 04-05-2005 04:16 AM

Quote:

Originally posted by keefaz
Try :
Code:

for f in *; do
    file=$(echo $f | tr A-Z a-z | tr ' ' _)
    [ ! -f $file ] && mv "$f" $file
done


Thanks, this got it done, but I had to remove this:
Code:

[ ! -f $file ]

keefaz 04-05-2005 08:07 AM

Why did you have to remove the file test ? I don't get it

luckie 04-23-2007 10:37 PM

Quote:

Originally Posted by keefaz
Try :
Code:

for f in *; do
    file=$(echo $f | tr A-Z a-z | tr ' ' _)
    [ ! -f $file ] && mv "$f" $file
done


This worked perfect. How would this be made recursive for all sub directories.

ghostdog74 04-23-2007 11:39 PM

here's an awk implementation
Code:

awk 'BEGIN {
        command = "ls"
        while ( ( command | getline  line  ) > 0) {               
                linelower = tolower(line)
                if ( linelower ~ /[[:space:]]/ )  {
                        gsub(/[[:space:]]/,"",linelower)
                        mvcmd = "mv \047" line "\047 " linelower
                        system(mvcmd)
                }
        }
        close(command)
        }'


luckie 04-24-2007 12:06 AM

Quote:

Originally Posted by ghostdog74
here's an awk implementation
Code:

awk 'BEGIN {
        command = "ls"
        while ( ( command | getline  line  ) > 0) {               
                linelower = tolower(line)
                if ( linelower ~ /[[:space:]]/ )  {
                        gsub(/[[:space:]]/,"",linelower)
                        mvcmd = "mv \047" line "\047 " linelower
                        system(mvcmd)
                }
        }
        close(command)
        }'


how is that going to be implemented into the pre-existing script?

ghostdog74 04-24-2007 12:24 AM

Quote:

Originally Posted by luckie
how is that going to be implemented into the pre-existing script?

same as you would write a shell script...
Code:

#!/usr/bin/bash
## awk code...
...
..## end awk code

then from the shell, call ./myscript.sh.
you can also save the script as .awk (not necessary though) and call it with awk
eg
Code:

#awk -f myawkscript.awk

gphilip 05-18-2008 01:28 PM

I found the following modified version of a script posted earlier in this thread useful for removing whitespace from filenames, recursively:

Code:

IFS=$'\n'
for f in `find .`; do
  file=$(echo $f | tr [:blank:] '_')
  [ -e $f ] && [ ! -e $file ] && mv "$f" $file
done
unset IFS

Or in one line on the shell:

Code:

IFS=$'\n';for f in `find .`; do file=$(echo $f | tr [:blank:] '_'); [ -e $f ] && [ ! -e $file ] && mv "$f" $file;done;unset IFS
-- Geevarghese Philip


All times are GMT -5. The time now is 03:49 AM.