Find and replace portion of file names


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Find and replace portion of file names
# 1  
Old 09-04-2008
Find and replace portion of file names

Hey all,

So I know you can easily find and replace words and strings in text files, but is there an easy way to find and replace just a sub-portion of text in the file name. For example, in a directory I have tons of file names that start with F00001-0708, and I want to change all the files to start with F0001-0708 (subtract a 0).

Example.
F00001-0708-RG-biasliuyda
F00001-0708-CS-akgdlaul
F00001-0708-VF-hioulgigl

and I want them to be:
F0001-0708-RG-biasliuyda
F0001-0708-CS-akgdlaul
F0001-0708-VF-hioulgigl

Thanks in advance!
-M
# 2  
Old 09-04-2008
Try this:

Code:
ls F00001-0708-*|sed 's/\(.\).\(.*\)/mv & \1\2/'

If you get the right command you can pipe the command to sh:

Code:
ls F00001-0708-*|sed 's/\(.\).\(.*\)/mv & \1\2/' | sh

# 3  
Old 09-04-2008
Hi,

You could try something like this, substring replacement in bash,

Code:
lakris@ubuntu:~/projekt/scripts/rename$ ls
F00001-0708-CS-akgdlaul  F00001-0708-RG-biasliuyda  F00001-0708-VF-hioulgigl
lakris@ubuntu:~/projekt/scripts/rename$ for x in F00001*;do echo $x === WILL BECOME === ${x/0000/000};done
F00001-0708-CS-akgdlaul === WILL BECOME === F0001-0708-CS-akgdlaul
F00001-0708-RG-biasliuyda === WILL BECOME === F0001-0708-RG-biasliuyda
F00001-0708-VF-hioulgigl === WILL BECOME === F0001-0708-VF-hioulgigl
lakris@ubuntu:~/projekt/scripts/rename$

Just remove the " === WILL..." and change echo to mv when You are confident that this is what You want.
Should work in ksh as well.

/Lakris

Edit - so essentially it is

Code:
for x in F00001*;do mv $x  ${x/0000/000};done


Last edited by Lakris; 09-04-2008 at 03:40 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find matching file in bash with variable file names but consisent prefixs

As part of a bash the below line strips off a numerical prefix from directory 1 to search for in directory 2. for file in /home/cmccabe/Desktop/comparison/missing/*.txt do file1=${file##*/} # Strip off directory getprefix=${file1%%_*.txt} ... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. Shell Programming and Scripting

Replace names in a file

Hi, I have a file like this: >Contig1 TTTTCATCAGCAATAGGTGCCTTCAACAAGTTTGAGAATTTTTTACACATGACTACAGGG CACGTGCACAATGTTTTGAGCACTGCCAATGTCGTCTTTTCTTCCCTGCTGATTCTATTT AGCTAGATTCACTTTGTTTCCAAAAAACGACAACAAATTCCAAGGTGTACCAAGGTGTAT >Contig2 CCCGTAGTAAAGATCAAAACTATCCCCTGCTGTATTCTCAACCAAATCCAACAAACTCTC... (3 Replies)
Discussion started by: the_simpsons
3 Replies

3. Shell Programming and Scripting

find specific file names and execute a command depending on file's name

Hi, As a newbie, I'm desperate ro make my shell script work. I'd like a script which checks all the files in a directory, check the file name, if the file name ends with "extracted", store it in a variable, if it has a suffix of ".roi" stores in another variable. I'm going to use these two... (3 Replies)
Discussion started by: armando110
3 Replies

4. Shell Programming and Scripting

How to find complete file names in UNIX if i know only extention of file

Suppose I have a file which contains other file names with some extention . text file containt gdsds sd8ef g/f/temp_temp.sum yyeta t/unix.sum ghfp hrwer h/y/test.text.dat if then.... I want to get the complete file names, like for above file I should get output as temp_temp.sum... (4 Replies)
Discussion started by: panchal
4 Replies

5. Shell Programming and Scripting

How to find pattern in file names?

I have some files, those are abbreviated (ed,ea, and bi) company_ed_20100719.txt company_ea_20100719.txt company_bi_20100719.txt I would like to rename these files by replacing ed with EmployeeDetails ea with EmployeeAddress bi with BankInfomration as company_... (3 Replies)
Discussion started by: LinuxLearner
3 Replies

6. Shell Programming and Scripting

Replace variable names in text file with its value

Hi I have a text file (mytext.txt), the content of which is as following: My name is <@MY_NAME@> and my age is <@MY_AGE@> . Now i have another property file (myprops) which is as following: MY_NAME=abcdefgh MY_AGE=000000 I was wondering, how can i replace the tags of mytext.txt, with its... (1 Reply)
Discussion started by: vigithvg
1 Replies

7. AIX

find for specific content in file in the directory and list only file names

Hi, I am trying to find the content of file using grep and find command and list only the file names but i am getting entire file list of files in the directory find . -exec grep "test" {} \; -ls Can anyone of you correct this (2 Replies)
Discussion started by: madhu_Jagarapu
2 Replies

8. Shell Programming and Scripting

Find the file names from date/time: Need help

Hi All, I really need help in figuring out how to determine the filenames from the time that is specified as parameter. The script should take as input - the start time and end time in minutes and also start date and end date. Example: reporter.sh -instance Instance_Name -startTime 13:10... (0 Replies)
Discussion started by: chiru_h
0 Replies

9. UNIX for Advanced & Expert Users

Find File names with sustitution

Hi All, Iam trying to find two kinds of files while ignoring rest of the files in a directory The files are like below Files to be found -------------------- perp45560 oerp4556 Files to be ignored ---------------------- oerp4556123450 oerp4556123470 I was trying the following... (4 Replies)
Discussion started by: baanprog
4 Replies

10. Shell Programming and Scripting

Replace characters in all file names in a particular directory

Hi, I have searched the forum on how to mass replace the file names. We are doing the migration and I am trying to accomplish a task where I have to replace all UNIX scripts in a particular directory that start with bdw to fdm... For example: bdw0110137.sh should be fdm0110137.sh Keep the... (4 Replies)
Discussion started by: madhunk
4 Replies
Login or Register to Ask a Question