|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
|
#2
|
||||
|
||||
|
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. |
|
#3
|
||||||||
|
||||||||
|
Thanks. I tried something similar but I couldn't get it to work.
Here is what I had, very much like yours. Quote:
And this is what I got. Remember I am doing this on Cygwin. Quote:
Also I have found that this Quote:
returns this... Quote:
whereas this Quote:
returns this... Quote:
Any ideas? |
|
#4
|
||||
|
||||
|
Apart from anything else I did, of course, mean
Code:
-f2- Code:
-c2- The Code:
ls -1 [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! |
|
#5
|
||||
|
||||
|
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:
However, if I change Code:
ls -l to Code:
ls This is the error output Quote:
|
|
#6
|
||||
|
||||
|
It should be "ls -1" (one) not "ls -l"
|
|
#7
|
||||
|
||||
|
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
$
|
|
#8
|
||||
|
||||
|
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!). |
|
#9
|
|||
|
|||
|
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
Last edited by vlsimpson : October 4th, 2006 at 06:38 PM. |
|
#10
|
|||
|
|||
|
Quote:
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)
|
|
#11
|
|||
|
|||
|
Quote:
Thanks vlsimpson. This worked. ![]() |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > Renaming file names |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|