Command / script to partially rename file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Command / script to partially rename file
# 1  
Old 05-20-2014
Command / script to partially rename file

Hi
I have numerous files names
Code:
product_host_result_B1000842.txt
product_host_result_B1000847.txt
product_host_result_C1000842.txt
product_host_result_C1000848.txt

etc. I need them renamed so that the 'product_host_result' becomes 'output_product_host' but the rest of the filename is retained (I've given some examples but it could be almost anything!)
Any help much appreciated

Last edited by bartus11; 05-20-2014 at 11:40 AM.. Reason: Please use [code][/code] tags.
# 2  
Old 05-20-2014
If you have rename utility


Code:
rename product_host_result output_product_host product_host_result*.txt

Please test first.
This User Gave Thanks to clx For This Post:
# 3  
Old 05-20-2014
Would that work?

Code:
ls product_host_result_*.txt | xargs -n1 -I {} mv -v {,output_}{}

# 4  
Old 05-20-2014
Try:
Code:
for i in *; do mv $i output_product_host_${i##*_}; done

# 5  
Old 05-20-2014
If you don't get any joy with that, can you show us what you want to convert from and what you want to convert to?
# 6  
Old 05-21-2014
Quote:
Originally Posted by clx
If you have rename utility


Code:
rename product_host_result output_product_host product_host_result*.txt

Please test first.
I just tried rename as you posted to check it and it didn't do anything. So I changed to this,

Code:
rename 's/product_host_result/output_product_host/' product_host_result*.txt

and this worked to me.

I am using Bash in Linux and you may be using something different.
This User Gave Thanks to Kibou For This Post:
# 7  
Old 05-22-2014
I tried this one and it renamed every file in the directory ! Luckily I tested it in my $home first !

Apologies should have clarified this is a shared filesystem - not just the ones I want to rename..

---------- Post updated at 10:36 AM ---------- Previous update was at 10:35 AM ----------

Hi clx

This doesn't work for me. The rename doesn't return any errors so I don't know why..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sftp transfers file partially

Hi ALL, I have a shell script using except : #!/bin/bash HOST=abc.com USER=abc PASSWORD=123 SOURCE_FILE=file1.zip TARGET_DIR=/A/B/C /usr/bin/expect <<- EOF spawn /usr/bin/sftp $USER@$HOST expect "password:" send "$PASSWORD\r" expect "sftp>" send "cd patch1\n" ... (11 Replies)
Discussion started by: Asad
11 Replies

2. Shell Programming and Scripting

Help with file rename script

Hello, I'm new in this shell scripting subject, I´m looking forward for someone to give me a hint or advice as to how to tackle my requirement, which is as follows: We have a Linux process that runs periodically every day, this process dumps a text file with always the same name overwriting... (2 Replies)
Discussion started by: netosv
2 Replies

3. UNIX for Dummies Questions & Answers

Script partially executes??

Hi All, I am calling a shell script from another shell script, however, it only executes part of it (the echo commands only). What could be some causes for that? For example: ShellScriptA.sh: ... ... ... . ShellScriptB.sh ShellScriptB.sh contents: echo date echo... (7 Replies)
Discussion started by: DBnixUser
7 Replies

4. Shell Programming and Scripting

Need bash script to ping the servers and rename the output file each time the script is ran

HI, I have a file serverlist in that all host names are placed. i have written a small script #./testping #! /bin/bash for i in `cat serverlist` do ping $i >> output.txt done so now it creates a file output.txt till here fine.. now each time i run this script the output file... (4 Replies)
Discussion started by: madhudeva
4 Replies

5. Shell Programming and Scripting

Multiple file rename (change in filename in unix with single command

Dear All, Please help ! i ham having 300 file with E.G. PMC1_4567.arc in seq. like PMC1_4568.arc,PMC1_4569.arc ...n and so on.. i want all those file to be rename like PMC_4567.arc ,PMC_4568.arc .. mean i want to remove 1 from first file name .. pls help.. (6 Replies)
Discussion started by: moon_22
6 Replies

6. Shell Programming and Scripting

Rename file using sed command

Greetings, I am very new to the UNIX shell scripting and would like to learn. However, I am currently stuck on how to process the below sample : Filename : DOCabcdef24387987ab90d.xml Pattern "DOC"+any character using and +".xml" And i want to change the second part of that file (any... (20 Replies)
Discussion started by: fanny_tirtasari
20 Replies

7. UNIX for Advanced & Expert Users

Unix Command to rename a file in a zipped folder

Hi, I am into automation work. Is there any UNIX command to rename a file in a zipped folder, without unzipping it..??? Thanks in advance.. (1 Reply)
Discussion started by: Victoria.Sam
1 Replies

8. Shell Programming and Scripting

A script that will move a file to a directory with the same name and then rename that file

Hello all. I am new to this forum (and somewhat new to UNIX / LINUX - I started using ubuntu 1 year ago).:b: I have the following problem that I have not been able to figure out how to take care of and I was wondering if anyone could help me out.:confused: I have all of my music stored in... (7 Replies)
Discussion started by: marcozd
7 Replies

9. Shell Programming and Scripting

mv command to rename multiple files that retain some portion of the original file nam

Well the title is not too good, so I will explain. I need to move (rename) files using a simple AIX script. ???file1.txt ???file2.txt ???file1a.txt ???file2a.txt to be: ???renamedfile1'date'.txt ???renamedfile2'date'.txt ???renamedfile1a'date'.txt ???renamedfile2a'date'.txt ... (4 Replies)
Discussion started by: grimace15
4 Replies

10. Shell Programming and Scripting

Logon profile kill script only partially working

I have been having an issue with the new motorola rf scan guns opening up too many sessions at once. It seems they will open a new connection for no reason, leaving the old one in the background and the user has no idea it is happening. To combat this, I have added the following code to the logon... (1 Reply)
Discussion started by: raidzero
1 Replies
Login or Register to Ask a Question