LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 07-03-2007, 07:03 PM   #1
djeikyb
Member
 
Registered: Nov 2005
Location: California
Distribution: ubuntu 10
Posts: 162

Rep: Reputation: 33
Batch rename troubles


Code:
for i in *; do mv "$i" 'echo $i | tr ' ' '_''; done
This should convert spaces to underscores for all files in the directory, right?

When run, I get an error: mv: target ' _' is not a directory.

I'm using cygwin in XP home.
 
Old 07-03-2007, 07:11 PM   #2
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,123

Rep: Reputation: 151Reputation: 151
You need to use back ticks, not single quotes. Try the following:
Code:
for i in *; do mv "$i" `echo $i | tr ' ' '_'`; done
 
Old 07-03-2007, 09:06 PM   #3
djeikyb
Member
 
Registered: Nov 2005
Location: California
Distribution: ubuntu 10
Posts: 162

Original Poster
Rep: Reputation: 33
Oh! Works great now, thanks! Never knew that key was useful.
 
Old 07-04-2007, 11:43 AM   #4
dawkcid
Member
 
Registered: May 2007
Location: UK
Distribution: LFS,Slackware,Slamd64,NetBSD
Posts: 102

Rep: Reputation: 15
The shell can do this itself,

Code:
for i in *\ *; do mv "$i" "${i// /_}"; done
Note that I've changed the * to *\ *, this will skip any files that don't have a space in them (it doesn't do any harm to glob everthing, but it stops mv complaining about the filenames being the same if there are no spaces).

Of course, if you have subdirectories, you probably should use find...
 
Old 07-04-2007, 02:38 PM   #5
djeikyb
Member
 
Registered: Nov 2005
Location: California
Distribution: ubuntu 10
Posts: 162

Original Poster
Rep: Reputation: 33
What I'm really trying to do is strip a bunch of windows shortcuts of the prefix "Shortcut to ". I'm having trouble I think because of the whitespace, so I converted it to underscores, which really isn't desirable. I'm quite new to anything cl beyond basic navigation, but I'm hopefully learning as I piece together googled scraps of code.

Code:
for FILE in *lnk; do NEWFILE=`echo $FILE | sed 's/Shortcut to //g'`; echo "$FILE becomes $NEWFILE"; done
for FILE in *.lnk; do NEWFILE=`echo $FILE | sed 's/Shortcut to //g'`; mv $FILE $NEWFILE; done
The first displays as expected, but the second errors: mv: target `[X].lnk' is not a directory

To incorporate find for subdirectories, should it be "for i in find .lnk ./;"?
 
  


Reply

Tags
bash, tr


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 On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Batch Script to rename files... jamie_barrow Linux - Newbie 16 06-14-2009 01:26 PM
Batch Rename shelfitz Linux - Newbie 19 10-12-2006 10:23 PM
Bash - Batch File Rename Help... emailarron Linux - Newbie 4 01-26-2006 07:35 AM
Batch rename question hellblade Linux - Software 4 05-03-2004 03:57 PM
Got a script to rename a batch of files? jamie_barrow Linux - General 1 08-08-2003 06:52 AM


All times are GMT -5. The time now is 03:13 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