LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-04-2005, 08:12 AM   #1
scuzzman
Senior Member
 
Registered: May 2004
Location: Hilliard, Ohio, USA
Distribution: Slackware, Kubuntu
Posts: 1,851

Rep: Reputation: 47
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?
 
Old 04-04-2005, 08:28 AM   #2
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 621

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

End
 
Old 04-04-2005, 09:01 AM   #3
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,445

Rep: Reputation: 94
Try :
Code:
for f in *; do
     file=$(echo $f | tr A-Z a-z | tr ' ' _)
     [ ! -f $file ] && mv "$f" $file
done
 
Old 04-04-2005, 08:25 PM   #4
Dave Kelly
Member
 
Registered: Aug 2004
Location: Todd Mission Texas
Distribution: Linspire
Posts: 213

Rep: Reputation: 31
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?
 
Old 04-05-2005, 02:30 AM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,493

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
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
 
Old 04-05-2005, 03:16 AM   #6
scuzzman
Senior Member
 
Registered: May 2004
Location: Hilliard, Ohio, USA
Distribution: Slackware, Kubuntu
Posts: 1,851

Original Poster
Rep: Reputation: 47
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 ]
 
Old 04-05-2005, 07:07 AM   #7
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,445

Rep: Reputation: 94
Why did you have to remove the file test ? I don't get it
 
Old 04-23-2007, 09:37 PM   #8
luckie
LQ Newbie
 
Registered: Apr 2007
Posts: 2

Rep: Reputation: 0
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.
 
Old 04-23-2007, 10:39 PM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
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)
	}'

Last edited by ghostdog74; 04-23-2007 at 10:41 PM.
 
Old 04-23-2007, 11:06 PM   #10
luckie
LQ Newbie
 
Registered: Apr 2007
Posts: 2

Rep: Reputation: 0
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?
 
Old 04-23-2007, 11:24 PM   #11
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
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

Last edited by ghostdog74; 04-23-2007 at 11:28 PM.
 
Old 05-18-2008, 12:28 PM   #12
gphilip
LQ Newbie
 
Registered: May 2008
Posts: 1

Rep: Reputation: 0
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
script to remove spaces from multiple filenames jeffreybluml Linux - Newbie 36 07-31-2013 02:10 AM
Script: lame stumbles on spaces in filename browny_amiga Linux - General 5 05-14-2008 08:14 AM
bash script with spaces Quantum0726 Programming 2 11-14-2005 09:26 PM
Getting the first part of a filename in a BASH script trevelluk Programming 3 02-15-2005 01:06 AM
bash script - incrementing a filename in a script tslinux Programming 10 08-05-2003 11:58 PM


All times are GMT -5. The time now is 01:17 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration