Results 1 to 3 of 3
HI ,
Due to unsuccessful execution of a script all filenames under a directory have been changed .
Say "Sample" is the directory.The original file names under this are
sample1.txt
...
- 10-15-2009 #1Just Joined!
- Join Date
- Oct 2009
- Posts
- 2
Script to change all filenames in a directory recursively
HI ,
Due to unsuccessful execution of a script all filenames under a directory have been changed .
Say "Sample" is the directory.The original file names under this are
sample1.txt
sample2.txt
sample3.txt
Now these have been changed to
sample1.txt_new
sample2.txt_new
sample3.txt_new
so i want to rename them to original names.I require a small script which will remove "_new" in all the files present recursively under a directory.
Thanks in advance
- 10-15-2009 #2
If I have a directory like so:
Inside test.sh is:Code:bash-3.2$ ls -R .: 1.other 1.txt_new 2.txt_new 3.txt_new new test.sh ./new: 2.other sample1.txt_new sample2.txt_new sample3.txt_new
When I run test.sh the following is outputted:Code:#!/bin/sh for a in `find -name \*.txt_new` do newname=`echo $a | sed "s/txt_new/txt/g"` mv $a $newname done
It won't cover all your bases (i.e. if txt_new forms part of your filename as well) but it should probably workCode:bash-3.2$ ls -R .: 1.other 1.txt 2.txt 3.txt new test.sh ./new: 2.other sample1.txt sample2.txt sample3.txt
- 10-15-2009 #3Linux Guru
- Join Date
- Oct 2007
- Location
- Tucson AZ
- Posts
- 1,939
Tried the following rename command with some files I created and it worked:
I expect this will affect any files beginning with "sample".Code:rename txt_new txt"" sample*


Reply With Quote