how to rename all files that have a certain text in the filename using tcsh shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to rename all files that have a certain text in the filename using tcsh shell
# 1  
Old 07-16-2009
Java how to rename all files that have a certain text in the filename using tcsh shell

Hello~

I'm on AIX version 5 and I believe I have the tcsh shell environment to play in. Can you guys help me with a solution to rename all files that have "eclp" in the filename to "ecl" ? I basically want to rename the files and strip the "p" out.

i.e. original filenames:

grp_eclp_grp_cent_ADTA.xlt grp_seri_grp_eclp_ADTB.xlt
grp_eclp_grp_cern_ADTA.xlt grp_seri_grp_eclp_ADTC.xlt
grp_eclp_grp_cern_ADTB.xlt grp_sftmd_grp_eclp_ADT.xlt


altered filenames when done:

grp_ecl_grp_cent_ADTA.xlt grp_seri_grp_ecl_ADTB.xlt
grp_ecl_grp_cern_ADTA.xlt grp_seri_grp_ecl_ADTC.xlt
grp_ecl_grp_cern_ADTB.xlt grp_sftmd_grp_ecl_ADT.xlt


Thanks in advance! Smilie
# 2  
Old 07-16-2009
Why you ask to use tcsh, if you have nothing ? Even your interactive command shell is tcsh, you can do your script using any shell which you have. And execute it even you use tcsh.

Example, I'm sure that you have sh in /bin directory, so this works, even it is ksh88 version or BourneShell - save this text to file rename.sh :
Code:
#!/bin/sh
#rename.sh
for f in $*
do
      newname=`echo "$f" | sed "s/^grp_eclp_/grp_ecl_/" `
      if [ "$newname" = "$f" ] ; then
           echo "no rename $f"
           continue
      fi
      echo "rename $f -> $newname"
      mv  "$f"  "$newname"
done

And after you have created file rename.sh , give execute priviledges and then call rename.sh with those files which you like to rename.
Code:
chmod a+rx rename.sh
./rename.sh  grp*ADTA*.xlt
# or
./rename.sh grp*.xlt
# or ...


Last edited by kshji; 07-16-2009 at 02:56 PM..
# 3  
Old 07-17-2009
Kshji,

Thanks for your help.

One of team members used the following (can you tell me what the difference is between your method and his?):

#!/usr/bin/sh

# usage: call ./rename.sh .*.xlt
for file in *.xlt
do
newfile=`echo $file | sed s/$1/$2/g`
echo is $newfile
mv $file $newfile
done
# 4  
Old 07-17-2009
Both are almost perfect Smilie.
My version use dynamic filenames, your teams dynamic replace rule.
Your team version not chech, is there name change or not. My version check it.

So this is the best:
Code:
#!/bin/sh
#rename.sh
# arg1=old string
# arg2=new string
# arg3-n = files to make name editing
old="$1"
new="$2"
# remove 2 args, rest are filenames
shift 2
for f in $*
do
      newname=`echo "$f" | sed "s/$old/$new/" `
      if [ "$newname" = "$f" ] ; then
           echo "no rename $f"
           continue
      fi
      echo "rename $f -> $newname"
      mv  "$f"  "$newname"
done

Using:
Code:
./rename.sh "grp_eclp_" "grp_ecl_"  grp*ADTA*.xlt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to rename files repeats previous filename in directory

In the below bash processes substitution, if there are 3 files in a directory /home/cmccabe/medex.logs/analysis.log, the filename variable is set to where these files are located. The code does execute, the problem is that if there is a renamed file in the output directory below, it gets... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. UNIX for Beginners Questions & Answers

Rename files based on simple text file

Hello! New here although not completely new to Unix. I wonder how I could rename files based on the data found in a simple textfile. It goes like this: I have 4 files 1 ldfgkkfjslkdfjsldkfjsf.wav 2 nndsdflksdjf.wav 3 sdflksjdf jjsdflsdfl.wav 4 dkadsdddd.wav Textfile.txt looks like... (14 Replies)
Discussion started by: Oortone
14 Replies

3. Shell Programming and Scripting

Rename files based on name in text file

Hello, I have a text file "file.list" with the contents below. file1 filename1 file2 filename2 file3 filename3 file1, file2 and file3 are files existing in the same directory as the text file file.list. I want to rename file1 to filename1, file2 to filename2, as show in the text... (1 Reply)
Discussion started by: james2009
1 Replies

4. Shell Programming and Scripting

Loop through the dir and Rename zip files and their underlying text file.

I have files in the ABC_YYYYMMDD.zip format under a directory. Each zip file contains A text file in the ABC_YYYYMMDD.txt format. I am trying to create a script that will Rename the zip files and their underlying text file replacing the datepart in them with . For eg: in the case of... (1 Reply)
Discussion started by: bash987
1 Replies

5. Shell Programming and Scripting

Shell script to get one to one map and rename the filename

I have 2 files sorted by numerically. I need help with shell script to read these 2 files and do a 1:1 mapping and rename the filenames with the mapped case#; For example: cat case.txt 10_80 10_90 cat files.txt A BCD_x 1.pdf A BCD_x 2.pdf ls pdf_dir A BCD_x 1.pdf A BCD_x 2.pdf ... (2 Replies)
Discussion started by: iaav
2 Replies

6. Shell Programming and Scripting

Rename all files (filename with spaces) to different extension

Hi, I have files with filenames as below. SGM Daily Sales Email-en-us-05312012.xlwa I want to rename it in .xls. I am writing a script to change this, as there can be multiple files in subfolders. I have the following script. #!/bin/ksh for oldfile in $(find... (1 Reply)
Discussion started by: mac4rfree
1 Replies

7. Shell Programming and Scripting

Rename mass files with text from first line

I have a few hundred text files that are currently numbered files. I would like to rename each one with the text from the first line in the file. I would prefer this is perl script rather than a one liner as it wil be after many alterations to the file via an existing script. Any help would be... (1 Reply)
Discussion started by: GWhizz
1 Replies

8. Shell Programming and Scripting

Shell Script to rename files

Hi, i need a bit of help writting a tcsh script which renames all ascii text files in the current directory by adding a number to their names before the extension so for example, a directory containing the files Hello.txt Hello.t Hello should have the following changes, Hello.txt... (2 Replies)
Discussion started by: yakuzaa
2 Replies

9. Shell Programming and Scripting

How to Rename/Convert Files in Shell Scripting?

Hi All, I want to Rename/Convert all the .doc files in to .pdf format. I am using the following Script. But the final output is not proper. ########################################## cd /u13/prepaid/ftpdata/INfiles/sap/ for name in `ls *.doc` do name1=`echo $name | sed -e... (11 Replies)
Discussion started by: hanu_oracle
11 Replies

10. Shell Programming and Scripting

rename files using shell scripting

Hi all, i want to rename some files in my directory using korn shell scripting. 1) i want to rename files who have no extension so that they will have the format: filename.extension and 2) i want the files who has extension initially, to stay the same (they will not be... (4 Replies)
Discussion started by: gfhgfnhhn
4 Replies
Login or Register to Ask a Question