LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-28-2008, 03:01 PM   #1
ArthurHuang
Member
 
Registered: Jan 2006
Posts: 174

Rep: Reputation: 30
bash script help


This is tough, Uhhh, who can help me?!

There is a directory tree and I want to change the ".exe" token of all files into ".g43". How can I implement this???


Thanks!!!
 
Old 03-28-2008, 03:49 PM   #2
prad77
Member
 
Registered: Mar 2008
Posts: 101

Rep: Reputation: 15
ls -lR *.exe | while read line do
length=`expr length $line`
path=`expr substr $line 1 $length-4`
rename $line ${path}.g43
done

Ofcourse it is not debuged and there can be better ways too... Try taking the substr of the file's path without ".exe". Then rename or move it with required extension. ls -lR will display full path names. check substr starting position is 1 not 0.

Gentoo

Last edited by prad77; 04-17-2008 at 03:28 AM.
 
Old 03-28-2008, 03:53 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Something like this should do the work:
Code:
find . -name "*.exe" -exec rename .exe .g43 {} \;
 
Old 03-28-2008, 04:33 PM   #4
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
If you are using the Perl version of rename (the one found here), you can also do this (it has fewer forks):
Code:
find . -name "*.exe" | rename 's/exe$/g43/'
 
Old 03-31-2008, 09:30 AM   #5
ArthurHuang
Member
 
Registered: Jan 2006
Posts: 174

Original Poster
Rep: Reputation: 30
Thanks a lot !
But I can't use rename, because I want to change the exe tokens INSIDE the file into g43, not rename the file....
 
Old 03-31-2008, 09:41 AM   #6
ArthurHuang
Member
 
Registered: Jan 2006
Posts: 174

Original Poster
Rep: Reputation: 30
I can use this script
find . -name "*.sh" then using sed -e 's/.exe/.g43/g'

however, how can I pipe? I mean replace the string without changing the file name?

thanks!
 
Old 03-31-2008, 09:48 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
In this case you have to use sed, like this
Code:
sed -i 's/[.]exe/.g43/g' testfile
Please note: in a regular expression the dot "." means any single character, so you have to let sed interpret it literally using the square brackets. Otherwise any occurence of the string "exe" preceeded by any character will be substituted.

Test the command above on a single file without the -i option (output is displayed in the terminal). When you are sure of the result, add the -i option to edit files in place. To do this recursively use the find command, like this:
Code:
find /path/to/dir -type f -exec sed -i 's/[.]exe/.g43/g' {} \;
Here I used -type f to select files only, but you can refine the search criteria if you want to do this to some specific files only. Take in mind that sed will substitute strings even in binary files.
 
Old 04-01-2008, 01:18 AM   #8
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
similar codes:
Code:
replace '.exe' '.g43' -- $(find /path/to/dir -type f -maxdepth 1)
Code:
replace '.exe' '.g43' -- path/to/dir/*
Code:
find /path/to/dir -type f -maxdepth 1 --exec replace '.exe' '.g43' -- '{}' \;
Code:
sed -i 's/\.exe/.g43/g' $(find /path/to/dir -type f -maxdepth 1)
Code:
sed -i 's/\.exe/.g43/g' path/to/dir/*
where 'maxdepth -1' is only optional. do 'man find' for more info.

Last edited by konsolebox; 04-01-2008 at 01:30 AM.
 
  


Reply



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
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
set variables in a bash script; ansi PS1 color script donnied Programming 4 11-21-2007 11:33 AM
Bash script to create bash script jag7720 Programming 10 09-10-2007 07:01 PM
[bash] having trouble debugging this bash script. jons Programming 4 02-08-2007 06:51 AM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:53 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration