Rename multiple files in shell bash, changing elements order.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Rename multiple files in shell bash, changing elements order.
# 1  
Old 05-27-2014
Rename multiple files in shell bash, changing elements order.

Hi,

I want to rename several files like this:
example:

Code:
A0805120817.BHN
A0805120818.BHN
.....


to:

Code:
20120817.0805.N
20120818.0805.N
......


How can i do this via terminal or in shell bash script ?

thanks,
LP

Last edited by Don Cragun; 05-27-2014 at 06:22 PM.. Reason: Add CODE tags
# 2  
Old 05-27-2014
Is this a homework assignment?

If it is, this thread needs to be closed and you need to resubmit your query in the Homework & Coursework Questions forum using the rules specified in here.

If it isn't, this thread needs to be moved to an appropriate technical forum and you need to more clearly specify the renaming procedures. I can see several possible ways to rename files that would match your 2 file sample.
# 3  
Old 05-28-2014
Hi Don Cragun,

It's not a homework. But i understand if you want to close the thread. I'm new in this forums.

I will search in other links.

Sorry to bore you and other users,
LP
# 4  
Old 05-28-2014
You could try
Code:
for a in *.BHN; do echo mv $a 20${a:5:6}.${a:2:4}.N; done

It will print the resulting renaming commands. Once happy with that, remove the echo
# 5  
Old 05-28-2014
Welcome to the UNIX and Linux Forums.

We don't need to close the thread. I have moved it from the Post Here to Contact Site Administrators and Moderators forum (which is intended for resolving site issues) to a technical forum intended to help with relatively simple shell scripting problems.

Please explain the rules that govern how files are to be renamed. For instance with your sample input, any of the following would work:
  1. Change "BH" to "0805." and change the 1st 5 characters to "20".
  2. Delete the 1st character of the name, move the next 4 characters after the 1st period in the name, change "BH" to ".", and add "20" to the start of the name.
  3. Change the 1st 5 characters of the name to "20" and change the last 3 characters of the name to "0805.N".
Give us a clear statement that identifies what files are to be processed and how the names are to be transformed and we'll be happy to help you find a way to do it.
# 6  
Old 05-28-2014
What RudiC suggested doesn't seem to do what you want. A slight variation on his suggestion:
Code:
echo mv $a 20${a:5:6}.${a:1:4}.N

implements a different possibility: Change any file whose name ends in ".BHN" to "20" followed by the 6 characters in the original name starting with the 6th character followed by a period followed by the 4 characters in the original filename starting with the 2nd character followed by ".N".
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 05-28-2014
Hi Don Cragun,

The command that you gave me solves my problem:

Code:
'for a in *.BHN; do echo mv $a 20${a:5:6}.${a:2:4}.N'

the ouput after echo printing is:
Code:
mv A0770130514.BHZ       20130514.0770.N

Thanks a lot for the solution and the contribute of RudiC!
I understand the logic of variable a and the numbers position count.

LP

Last edited by radoulov; 05-28-2014 at 10:55 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SBATCH trinity for multiple files and rename/move the output files

Hey guys, I have wrote the following script to apply a module named "trinity" on my files. (it takes two input files and spit a trinity.fasta as output) #!/bin/bash -l #SBATCH -p node #SBATCH -A <projectID> #SBATCH -n 16 #SBATCH -t 7-00:00:00 #SBATCH --mem=128GB #SBATCH --mail-type=ALL... (1 Reply)
Discussion started by: @man
1 Replies

2. Shell Programming and Scripting

How to rename multiple files at one go?

Hi, I have hundreds of files with XXX in their file name and I want to rename all of them with YYY in place of XXX. for ex: $ ls -1 123XXX789 345XXX678 Output $ ls -1 123YYY789 345YYY678 I know we can loop in each file and sed to replace and rename each file but ren *XXX* *YYY*... (4 Replies)
Discussion started by: reddyr
4 Replies

3. Shell Programming and Scripting

Rename multiple files

Hi, In my directory I have many files, for e.g. file_123 file_124 file_125 file_126 file_127 Instead of renaming these files one by one, I would like to rename them at a same time using same command... they should appear like 123 124 125 126 127 What command(awk or ls or... (3 Replies)
Discussion started by: juzz4fun
3 Replies

4. UNIX for Dummies Questions & Answers

Rename multiple files, changing prefix, extension and dropping characters

I'm currently only able to perform some very basic functions, so hope this makes sense... I have a set of about 27 files that need to be renamed from something like this: 000012ABCDEFGHIJ.XXX.YYY.ZZZ 000078KLMNO.XXX.YYY.ZZZ 000099PQ.XXX.YYY.ZZZ to something like this: newa012.abc... (11 Replies)
Discussion started by: bbmcg
11 Replies

5. UNIX for Dummies Questions & Answers

Renaming files by changing date order

I'm looking for a simple solution to rename a batch of files. All of the files in this directory start with a date in the format mm.dd.yy followed by a space and then additional descriptive text. Example: 01.21.10 742 P.xlsx 02.24.09 730 Smith.xlsx The information following the date can... (3 Replies)
Discussion started by: kreisel
3 Replies

6. Shell Programming and Scripting

Need help for a Shell script to rename multiple files

Hi! I need help to create a shell script to search inside a file and then copy a portion of the search result as the new file name. Basically I was hacked over the weekend and the genius wipe out my drive from my server. I was able to recover alot of files, but biggest problem Is now the... (15 Replies)
Discussion started by: kidney514
15 Replies

7. UNIX for Dummies Questions & Answers

help with multiple files rename...

Hi everyone, I'm very green in Linux. Please help me to solve my problem. I have thousands of files and I want to change their names. They have naming convection: prefix_date_date+1_suffix.nc prefix: ext-GLORY date_date+1: 20020101_20020102 and two types of suffix: gridV_R20020130 and... (3 Replies)
Discussion started by: makikicindy
3 Replies

8. Shell Programming and Scripting

bash script to rename multiple directories

Hello I have a directory structure with year in format 4 digits, e.g 2009, below which is month format 1 or 2 digits, e.g 1 or 12, blow which is day format 1 or 2 digits, e.g 1 or 31. I want to change the names of lots of directories to the be Year - 4 digits , e.g 2009 - No change here... (4 Replies)
Discussion started by: garethsays
4 Replies

9. Shell Programming and Scripting

Average of elements throught multiple files

Hi, I got a lot of files looking like this: 1 0.5 6 All together there are ard 1'000'000 lines in each of the ard 100 files. I want to build the average for every line, and write the result to a new file. The averaging should start at a specific line, here for example at line... (10 Replies)
Discussion started by: chillmaster
10 Replies
Login or Register to Ask a Question