Results 1 to 10 of 10
Hello,
Would someone please help me by edit a rename script?
I would like a script that can rename file names without disturbing its numbers. Example:
Audit_trail_YYYYMMDD.txt
Audit_trail_YYYYMMDD.txt
Audit_trail_YYYYMMDD.txt
Audit_trail_YYYYMMDD.txt
...
- 09-18-2009 #1Just Joined!
- Join Date
- Sep 2009
- Posts
- 5
Rename bash script help
Hello,
Would someone please help me by edit a rename script?
I would like a script that can rename file names without disturbing its numbers. Example:
Audit_trail_YYYYMMDD.txt
Audit_trail_YYYYMMDD.txt
Audit_trail_YYYYMMDD.txt
Audit_trail_YYYYMMDD.txt
Audit_trail_YYYYMMDD.txt
Will be renamed to:
KVAT_YYYYMMDD.txt
KVAT_YYYYMMDD.txt
KVAT_YYYYMMDD.txt
KVAT_YYYYMMDD.txt
KVAT_YYYYMMDD.txt
Only the name part will be renamed, and the numbers are left undisturbed.
I tried searching for something like that. But all the scripts I found does the opposite, the result as KVATAudit_trail_YYYYMMDD.txt
here is the script that i found.
originalFiles=$(ls Audit_trail*.txt)
for loopFile in $originalFiles
do
mv $loopFile KVAT$loopFile
done
Please help. Thank in advance.
- 09-18-2009 #2Just Joined!
- Join Date
- Sep 2007
- Posts
- 2
oneline sed solution
Hello,
certainly there are countless different possibilities.
What I usually use for this is 'sed' and a pipe, like this:
or for example:Code:ls Audit_trail* | sed 's/\(.*\)_\([0-9]*\)\.\(.*\)/mv \1_\2.\3 KVAT_\2.\3/'
You can add ' | head ' at the end to see if it works and then add ' | sh ' instead, to make it work.Code:ls Audit_trail* | sed 's/Audit_trail_\(.*\).txt/mv Audit_trail_\1.txt KVAT_\1.txt/'
Color output of 'ls' can sometimes make troubles, then use 'ls --color=none' instead.
- 09-18-2009 #3Just Joined!
- Join Date
- Sep 2009
- Posts
- 5
Hi,
It not work for me !
When i try to run the script, it show
-bash: sed: command not found
Any solution for it ?
Thanks
- 09-18-2009 #4Just Joined!
- Join Date
- Sep 2007
- Posts
- 2
Well mambo84, maybe you don't have sed installed.
Then install it - but that is no more a question to a forum! Google first
Enjoy learning your OS
and good luck!
- 09-18-2009 #5Just Joined!
- Join Date
- Sep 2009
- Posts
- 5
Hi,
i dont hv permission to install sed at FTP server,
i've search from google regarding Rename bash script but all getting same result.
Can use another function other than sed?
Please advice
- 09-18-2009 #6Linux Guru
- Join Date
- Oct 2007
- Location
- Tucson AZ
- Posts
- 1,939
Tested the above on a few files I created and it worked. It will affect any file beginning with the "Audit_trail_"Code:rename Audit_trail_ KVAT_ Audit_trail_*
You could test it on a few files you move to the /tmp directory.
- 09-18-2009 #7Linux Newbie
- Join Date
- Mar 2009
- Posts
- 228
Maybe the directory sed is in isn't in your PATH variable. Try specifying the path which should be:
/bin/sed
- 09-18-2009 #8Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
Bash can do without sed or any other additive.
If you like the output, remove "echo" to do the real work.Code:for file in Audit_trail_*.txt; do echo mv "$file" "${file#Audit_trail}KVAT" done
However, it's really strange that sed is not installed. It must be some problem with paths or whatever as someone says above.
- 09-23-2009 #9Just Joined!
- Join Date
- Sep 2009
- Posts
- 5
- 09-23-2009 #10Just Joined!
- Join Date
- Sep 2009
- Posts
- 5
Hi All
Problem solve,
By the way, assume that i got different test file,Code:for file in Audit_trail_*.txt; do echo mv "$file" "KVAT${file#Audit_trail}" done
Audit_trail_YYYYMMDD.txt --> KVATyymmdd.txt
Mer_net_settle_part1_YYYYMMDD.txt -->KVSAyymmdd.txt
How can i do it in 1 shot?
Please advice.


Reply With Quote
