LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux > Linux - General
User Name
Password
Linux - General This 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

Tags used in this thread
Popular LQ Tags , ,

Reply
 
Thread Tools
Old 07-03-2007, 08:03 PM   #1
djeikyb
Member
 
Registered: Nov 2005
Location: California
Distribution: ubuntu 8
Posts: 149
Thanked: 5
Batch rename troubles


[Log in to get rid of this advertisement]
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.
djeikyb is offline  
Tag This Post , ,
Reply With Quote
Old 07-03-2007, 08:11 PM   #2
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware 12.1, Slackware 13.0
Posts: 3,788
Thanked: 20
You need to use back ticks, not single quotes. Try the following:
Code:
for i in *; do mv "$i" `echo $i | tr ' ' '_'`; done
gilead is offline     Reply With Quote
Old 07-03-2007, 10:06 PM   #3
djeikyb
Member
 
Registered: Nov 2005
Location: California
Distribution: ubuntu 8
Posts: 149
Thanked: 5

Original Poster
Oh! Works great now, thanks! Never knew that key was useful.
djeikyb is offline     Reply With Quote
Old 07-04-2007, 12:43 PM   #4
dawkcid
Member
 
Registered: May 2007
Location: UK
Distribution: LFS,Slackware,Slamd64,NetBSD
Posts: 102
Thanked: 0
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...
dawkcid is offline     Reply With Quote
Old 07-04-2007, 03:38 PM   #5
djeikyb
Member
 
Registered: Nov 2005
Location: California
Distribution: ubuntu 8
Posts: 149
Thanked: 5

Original Poster
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 ./;"?
djeikyb is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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
Batch Script to rename files... jamie_barrow Linux - Newbie 16 06-14-2009 02:26 PM
Batch Rename shelfitz Linux - Newbie 19 10-12-2006 11:23 PM
Bash - Batch File Rename Help... emailarron Linux - Newbie 4 01-26-2006 08:35 AM
Batch rename question hellblade Linux - Software 4 05-03-2004 04:57 PM
Got a script to rename a batch of files? jamie_barrow Linux - General 1 08-08-2003 07:52 AM


All times are GMT -5. The time now is 02:59 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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration