Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File Rename in Script

Status
Not open for further replies.

Autosys

Programmer
Jun 1, 2004
90
GB
Hi All

Was wondering if you could help.

Need to write a little ksh script that will loop through files in a directory and rename all the files or directories that contain the letter bbs in them. The script should rename these 3 letters to aaa.

For example if I have the following files in the directory:

testbbskka.txt
hello
echokkbbs.txt

it should rename only the 1st and last files by replacing bbs to aaa

Thanks a lot!
 
A starting point:
for f in $(ls *bbs*); do mv $f $(echo $f | sed 's!bbs!aaa!'); done


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
knock-knock, UUOls is here:
also considering embedded spaces in the file names.
Code:
#!/bin/ksh
IFS='#'
for f in *bbs*; do mv "$f" $(echo "$f" | sed 's!bbs!aaa!'); done

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
vlad, what happens if no *bbs* file ?
I persist in the Usefull Use Of ls ;-)
I agree for the embedded space issue.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
true, but you can always '2>/dev/null' ;)
yeah, maybe it's not so useless....

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanks for all your help guys!! My script now does what I've originally requested from you!

I'm now however faced with another small problem. When my script goes into my loop (for file in ls), it renames all the files matching the pattern but not any directories?

Ideally it also needs to recursively drill into the sub-directories to rename those as well.

Will a find command somehow in that case be a better approach?

Thanks!
S
 
Perhaps this ?
for f in $(find . -depth *bbs*); do mv $f $(echo $f | sed 's!bbs!aaa!'); done

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top