Replacing part of filename


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Replacing part of filename
# 1  
Old 01-28-2014
Replacing part of filename

Hi guys!

I have quite a lot of files like

all_10001_ct1212307460308.alf*

and I want to get rid of the first number for all at once like:

all_ct1212307460308.alf*

How can I do this in the shell?
# 2  
Old 01-28-2014
What shell and operating system are you using?
# 3  
Old 01-28-2014
Oh sorry:

It is GNU/Linux 6.0.8
how do I know which shell I have?
# 4  
Old 01-28-2014
echo $SHELL
# 5  
Old 01-28-2014
/bin/bash
# 6  
Old 01-28-2014
If your filenames always have (exactly) two underscore characters, you could try something like:
Code:
for i in *_*_*.alf*
do      echo mv "$i" "${i/_*_/_}"
done

and, if it looks like that is correctly transforming the filenames, remove the echo and run it again.
# 7  
Old 01-29-2014
Thank you very much Don. But there is still one little detail.
If I use your code it is also replacing the second number but I just want to get rid of the first one. Probably because I have more than 2 underscores like:

mar10lan_all_1000_ct1210111172602.alf_gts_mar10.list
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing .dat.gz to .txt.gz in filename

Hi, I tried below method; mv -v /oracle1/scr/tilki/willsendtilkiNew/VOICE-MO_$nfname.gz \ $(echo /oracle1/scr/tilki/willsendtilkiNew/VOICE-MO_$nfname.gz | tr 'dat' 'txt'); nfame variable has the string "dat" . I need to rename files like below; ASIS: 20140902103700_100319.dat.gz... (8 Replies)
Discussion started by: snr_silencer
8 Replies

2. Programming

to extract all the part of the filename before a particular word in the filename

Hi All, Thanks in Advance I am working on a shell script. I need some assistance. My code: if then set "subscriber" "promplan" "mapping" "dedicatedaccount" "faflistSub" "faflistAcc" "accumulator"\ "pam_account"; for i in 1 2 3 4 5 6 7 8;... (0 Replies)
Discussion started by: aealexanderraj
0 Replies

3. UNIX for Dummies Questions & Answers

to extract all the part of the filename before a particular word in the filename

Hi All, Thanks in Advance I am working on a shell script. I need some assistance. My Requirement: 1) There are some set of files in a directory like given below OTP_UFSC_20120530000000_acc.csv OTP_UFSC_20120530000000_faf.csv OTP_UFSC_20120530000000_prom.csv... (0 Replies)
Discussion started by: aealexanderraj
0 Replies

4. Shell Programming and Scripting

Getting part of a filename

Hi All, I'm trying to get part of a filename and my skill with regular expression are lacking. I know I need to use SED but have no idea how to use it. I'm hoping that someone can help me out. The file names would be: prefix<partwewant>suffix.extension the prefix and suffix are always 3... (4 Replies)
Discussion started by: imonkey
4 Replies

5. Shell Programming and Scripting

Replacing white spaces in filename

Hi; In following code find LOG_DIR -type f | while read filename; do echo $filename; done I want to precede each white space encountered in filename with \ so that when i use $filename for running some commands in do...done,it wont give me an error. will appreciate ur help in this.... (1 Reply)
Discussion started by: ajaypadvi
1 Replies

6. Shell Programming and Scripting

Replacing some part of file

Hello, I have two files, consider that as file1 and file2. Here file1 is the master file. file1 will contain data like GS*RA*071000013*102562451P*091130*0520*334052023*X*003050 ST*820*334052023 BPR*C*509.77*C*ACH*CTX*01*071000013*DA*5529085*9000002008**01*071000013*DA*5529085*091130... (8 Replies)
Discussion started by: atlantis
8 Replies

7. Shell Programming and Scripting

Replacing the part of file name?

Hi All One of my script generate following files. These files has static TIMESTAMP 20080227. AccAdd_20080227_1000.dat AccBal_20080227_1000.dat Acc_20080227_1000.dat AccGrpMem_20080227_1000.dat AccToCust_20080227_1000.dat What i need to do is, once the file has been generated, it... (7 Replies)
Discussion started by: Amit.Sagpariya
7 Replies

8. UNIX for Dummies Questions & Answers

Replacing string with filename

Hi All, I've recently run a script that inserts the filename into all files of my active directory. Now I want to move the filename string and have it replace text a few lines down. In other words, here's what I'm trying to do. Here is a file called 'goodtimes': " goodtimes Hi, Welcome... (1 Reply)
Discussion started by: calrog
1 Replies

9. Shell Programming and Scripting

Need help regarding replacing a part of string

Hi all suppose i have a string "abacus sabre", i need to replace occurences 'ab' with 'cd' and i need to store this result into same string and i need to return this result from script to the calling function, where as the string is passed from calling function. i tried like this ... (1 Reply)
Discussion started by: veerapureddy
1 Replies

10. Shell Programming and Scripting

part of a filename

Hi, I need to extract only a part of the filenames of some files. The files are named this way : .tap_profile_SIT02 I want the "SIT02" part, which is not the same for each file. I was able to get what I want with bash, but not with ksh. Here is the command I used in bash : find... (8 Replies)
Discussion started by: flame_eagle
8 Replies
Login or Register to Ask a Question