UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 4th, 2006, 04:15 AM
dyname_kebab dyname_kebab is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2006
Posts: 4 dyname_kebab User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 52 m 4 sec
Reputation Power: 0
Renaming file names

My problem is this.

I have a directory with around 1200 files. The file names go something like...

"Dev_Shed 101 - First topic.txt"
"Dev_Shed 102 - Second topic.txt"
"Dev_Shed 103 - Third topic.txt"

etc (without the quotes of course)

What I want is a script that will rename the files by stripping out the initial "Dev_Shed".

I have searched the site but couldn't find anything that will actually work.

Help please.

PS
I am using Cygwin.

Reply With Quote
  #2  
Old October 4th, 2006, 05:20 AM
Ehlanna's Avatar
Ehlanna Ehlanna is offline
Not a clue what to put ...
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2006
Location: in front of this keyboard
Posts: 814 Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 5 h 10 m 43 sec
Reputation Power: 243
Try something like:

Code:
for f in $(ls -1)
do
  newfn=$(echo $f | cut -d" " -c2-)
  mv $f $newfn
done


But add some error checking!
__________________
According to Sod's Law, buttered toast lands butter side down, when dropped.
Per nature, cats always land on their feet.
So, what happens when you strap buttered toast to the back of a cat and throw it out a window?
.

Last edited by Ehlanna : October 4th, 2006 at 05:23 AM.

Reply With Quote
  #3  
Old October 4th, 2006, 05:59 AM
dyname_kebab dyname_kebab is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2006
Posts: 4 dyname_kebab User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 52 m 4 sec
Reputation Power: 0
Thanks. I tried something similar but I couldn't get it to work.

Here is what I had, very much like yours.
Quote:
for f in $(ls Dev*)
do
newfn=$(echo $f | cut -d" " -c2-)
mv $f $newfn
done


And this is what I got. Remember I am doing this on Cygwin.

Quote:
(extract)
cut: an input delimiter may be specified only when operating on fields
mv: missing destination file operand after `Dev_Shed'
cut: an input delimiter may be specified only when operating on fields
mv: missing destination file operand after `101'


Also I have found that this

Quote:
ls Dev*


returns this...

Quote:
Dev_Shed 101 - First topic.txt Dev_Shed 103 - Third topic.txt
Dev_Shed 102 - Second topic.txt Dev_Shed 104 - Fourth topic.txt


whereas this

Quote:
for f in $(ls Dev*)
do
echo $f
done


returns this...

Quote:
Dev_Shed
101
-
First
topic.txt
Dev_Shed
102
-
Second
topic.txt
Dev_Shed
103
-
Third
topic.txt
Dev_Shed
104
-
Fourth
topic.txt


Any ideas?

Reply With Quote
  #4  
Old October 4th, 2006, 07:00 AM
Ehlanna's Avatar
Ehlanna Ehlanna is offline
Not a clue what to put ...
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2006
Location: in front of this keyboard
Posts: 814 Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 5 h 10 m 43 sec
Reputation Power: 243
Apart from anything else I did, of course, mean
Code:
-f2-
not
Code:
-c2-

The
Code:
ls -1
returns one file per line.

[edit]
And, as there are spaces in the file name, it woudl be better if you used:
Code:
mv "$f" "$newfn"

[/edit]

Last edited by Ehlanna : October 4th, 2006 at 07:02 AM. Reason: Corrections and improvements!

Reply With Quote
  #5  
Old October 4th, 2006, 09:55 AM
dyname_kebab dyname_kebab is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2006
Posts: 4 dyname_kebab User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 52 m 4 sec
Reputation Power: 0
I have made the changes as suggested and this is the error output from

Code:
for f in $(ls -l)
do
newfn=$(echo $f | cut -d" " -f2-)
mv "$f" "$newfn"
done


Quote:
(extract)
mv: cannot stat `total': No such file or directory
mv: cannot stat `0': No such file or directory
mv: invalid option -- r
Try `mv --help' for more information.
mv: cannot stat `1': No such file or directory
mv: cannot stat `None': No such file or directory
mv: cannot stat `0': No such file or directory
mv: cannot stat `Oct': No such file or directory
mv: cannot stat `4': No such file or directory
mv: cannot stat `08:40': No such file or directory
mv: cannot stat `Dev_Shed': No such file or directory
mv: cannot stat `101': No such file or directory
mv: cannot stat `-': No such file or directory
mv: cannot stat `First': No such file or directory
mv: cannot stat `topic.txt': No such file or directory


However, if I change
Code:
ls -l


to

Code:
ls


This is the error output

Quote:
(extract)
mv: cannot stat `Dev_Shed': No such file or directory
mv: cannot stat `101': No such file or directory
mv: cannot stat `-': No such file or directory
mv: cannot stat `First': No such file or directory
mv: cannot stat `topic.txt': No such file or directory

Reply With Quote
  #6  
Old October 4th, 2006, 11:03 AM
mu's Avatar
mu mu is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2006
Location: Dublin, Ireland
Posts: 174 mu User rank is Sergeant Major (2000 - 5000 Reputation Level)mu User rank is Sergeant Major (2000 - 5000 Reputation Level)mu User rank is Sergeant Major (2000 - 5000 Reputation Level)mu User rank is Sergeant Major (2000 - 5000 Reputation Level)mu User rank is Sergeant Major (2000 - 5000 Reputation Level)mu User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 23 h 39 m 56 sec
Reputation Power: 39
It should be "ls -1" (one) not "ls -l"

Reply With Quote
  #7  
Old October 4th, 2006, 12:33 PM
stanleypane's Avatar
stanleypane stanleypane is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Baltimore, MD
Posts: 208 stanleypane User rank is Sergeant (500 - 2000 Reputation Level)stanleypane User rank is Sergeant (500 - 2000 Reputation Level)stanleypane User rank is Sergeant (500 - 2000 Reputation Level)stanleypane User rank is Sergeant (500 - 2000 Reputation Level)stanleypane User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 4 Days 7 h 4 m 19 sec
Reputation Power: 21
The problem is more than just correcting the "ls -1" command.

The "for" statement will use a space as it's standard delimiter, so doing:

Code:
for i in `ls -1`
do
  echo $i
done

Will read each value using a single space as a delimiter, so your output would be:

Code:
Dev_Shed
101
-
First
topic.txt

Try this instead:
Code:
ls -1 | while read line
do
  mv -f "${line}" "${line:9}"
done

I just did this under Cygwin with the following results:
Code:
username@PC-020 ~/temp
$ ls
Dev_Shed 101 - first topic.txt   Dev_Shed 103 - third topic.txt
Dev_Shed 102 - second topic.txt

username@PC-020 ~/temp
$ ls -1 | while read line
> do
> mv -f "${line}" "${line:9}"
> done

username@PC-020 ~/temp
$ ls
101 - first topic.txt  102 - second topic.txt  103 - third topic.txt

username@PC-020 ~/temp
$
Comments on this post
Ehlanna agrees!

Reply With Quote
  #8  
Old October 4th, 2006, 12:41 PM
Ehlanna's Avatar
Ehlanna Ehlanna is offline
Not a clue what to put ...
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2006
Location: in front of this keyboard
Posts: 814 Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 5 h 10 m 43 sec
Reputation Power: 243
Oh dammit, it went through my mind then dropped out again - yes, spaces in the file name (one reason I do not use them!!!!) would be adding to the problem.
Apologies about that.
Other ways of dealing with this can be found using awk and find with xargs, even 'cheating' using tr (I have used most methods!).

Reply With Quote
  #9  
Old October 4th, 2006, 06:34 PM
vlsimpson vlsimpson is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2005
Location: The grassy knoll
Posts: 115 vlsimpson User rank is Sergeant Major (2000 - 5000 Reputation Level)vlsimpson User rank is Sergeant Major (2000 - 5000 Reputation Level)vlsimpson User rank is Sergeant Major (2000 - 5000 Reputation Level)vlsimpson User rank is Sergeant Major (2000 - 5000 Reputation Level)vlsimpson User rank is Sergeant Major (2000 - 5000 Reputation Level)vlsimpson User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 5 Days 2 h 1 m 30 sec
Reputation Power: 41
You don't need ls at all:
Bash code (I've noticed the latest bash (V.3) escapes spaces automagically).

Code:
for f in Dev*
do
cp "$f" "${f/Dev_Shed/}"
done


You may as well get rid of the annoying spaces:
Code:
for f in Dev*
do
nospaces=$(echo "$f" | tr ' ' '_')
echo "$f" "${nospaces/Dev_Shed_/}"
done
Comments on this post
dyname_kebab agrees: Great answer.

Last edited by vlsimpson : October 4th, 2006 at 06:38 PM.

Reply With Quote
  #10  
Old October 4th, 2006, 08:44 PM
ghostdog74 ghostdog74 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 99 ghostdog74 User rank is Lance Corporal (50 - 100 Reputation Level)ghostdog74 User rank is Lance Corporal (50 - 100 Reputation Level)ghostdog74 User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Day 19 h 49 m 58 sec
Reputation Power: 3
Quote:
Originally Posted by dyname_kebab
My problem is this.

I have a directory with around 1200 files. The file names go something like...

"Dev_Shed 101 - First topic.txt"
"Dev_Shed 102 - Second topic.txt"
"Dev_Shed 103 - Third topic.txt"

etc (without the quotes of course)

What I want is a script that will rename the files by stripping out the initial "Dev_Shed".

I have searched the site but couldn't find anything that will actually work.

Help please.

PS
I am using Cygwin.



If you have Python:
Code:
import os
os.chdir("/home")
for files in os.listdir(os.getcwd()):
 	if files.startswith('Dev_Shed'):
 		newfilename = files.replace('Dev_Shed ','')
 		os.rename(files,newfilename)

Reply With Quote
  #11  
Old October 5th, 2006, 04:04 AM
dyname_kebab dyname_kebab is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2006
Posts: 4 dyname_kebab User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 52 m 4 sec
Reputation Power: 0
Quote:
Originally Posted by vlsimpson
You don't need ls at all:
Bash code (I've noticed the latest bash (V.3) escapes spaces automagically).

You may as well get rid of the annoying spaces:
Code:
for f in Dev*
do
nospaces=$(echo "$f" | tr ' ' '_')
echo "$f" "${nospaces/Dev_Shed_/}"
done


Thanks vlsimpson. This worked.

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Renaming file names


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |