Need help in batch renaming files with bash shell script.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help in batch renaming files with bash shell script.
# 1  
Old 08-18-2014
Need help in batch renaming files with bash shell script.

I have some 50+ files in the following format :

Code:
abcd_vish_running_ZEBRA_20140818.dat_08-14-2014_23:08:23
abcd_vish_running_ZEB-RA_20140818.dat_08-14-2014_23:08:35
abcd_vish_running_ZEB_RA_20140818.dat_08-14-2014_23:08:37
abcd_vish_running_RI-NG_20140818.dat_08-14-2014_23:08:42
abcd_vish_running_RING_20140818.dat_08-14-2014_23:08:45
abcd_vish_running_RI_NG_20140818.dat_08-14-2014_23:08:52

(ZEBRA,ZEB-RA,ZEB_RA,RI-NG,RING,RI_NG are given to just show the pattern)

i need the to rename the above files to following format :

Code:
abcd_vish_running_ZEBRA_20140818.dat
abcd_vish_running_ZEB-RA_20140818.dat
abcd_vish_running_ZEB_RA_20140818.dat
abcd_vish_running_RI-NG_20140818.dat
abcd_vish_running_RING_20140818.dat
abcd_vish_running_RI_NG_20140818.dat

Now how to achieve this with shell scripting ? I'm fairly new to scripting. Kindly help.

Cheers.

Last edited by Don Cragun; 08-18-2014 at 03:52 PM.. Reason: Add CODE tags.
# 2  
Old 08-18-2014
Is this a homework assignment?

What have you tried.
# 3  
Old 08-18-2014
We are doing some kind of a data load. For this we have to copy a set of source files from archival directory through ftp ,then unzip it and have to rename it before starting the data load. This is being done on a daily basis. So I'm trying to automate this process. So far I'm able to FTP download the files and rename them. Now I'm stuck at this step. As I already said I'm new to scripting. So far, whatever I have done is with the help of Google, and forums like this.

Hope this helps.

Cheers.
# 4  
Old 08-18-2014
try this:
Code:
#!/bin/bash

ls *.dat_* | while read line
 do
 part1=$(echo "$line" | cut -d"." -f1)
 mv "$line" "$part1.dat"
 done

edit / another approach:
Code:
rename 's/\.dat_.*/\.dat/' *.dat_*


Last edited by junior-helper; 08-18-2014 at 05:05 PM..
This User Gave Thanks to junior-helper For This Post:
# 5  
Old 08-18-2014
Try:
Code:
for i in *.dat_*
do      mv "$i" "${i%.dat_*}.dat"
done

This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 08-19-2014
Thanks a million for the prompt reply. Will try it and let know as soon as possible.

Cheers.

---------- Post updated at 09:16 PM ---------- Previous update was at 01:32 AM ----------

Quote:
Originally Posted by junior-helper
try this:
Code:
#!/bin/bash

ls *.dat_* | while read line
 do
 part1=$(echo "$line" | cut -d"." -f1)
 mv "$line" "$part1.dat"
 done

edit / another approach:
Code:
rename 's/\.dat_.*/\.dat/' *.dat_*

Quote:
Originally Posted by Don Cragun
Try:
Code:
for i in *.dat_*
do      mv "$i" "${i%.dat_*}.dat"
done

That worked like a charm. Thanks a lot guys!SmilieSmilie

Cheers.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script for batch renaming

What is wrong with this script to rename batch of similar files? For example renaming all *.txt files to *.tab . $ for i in seq {01..10}; do touch ${i}.txt; done $ ./rename.sh *.txt txt tab Error: mv: ‘01.txt’ and ‘01.txt’ are the same file. Code is: #!/usr/bin/bash # renames.sh #... (6 Replies)
Discussion started by: yifangt
6 Replies

2. Shell Programming and Scripting

BASH Batch renaming insert additional zero into filename

Hi all, Wondering how this could be accomplished........ a directory contains sequentially numbered files from fw01 to fw999. How would I insert an additional zero so that the directory lists these files in a proper manner? (i.e. all double digit files from fw01 to fw99 would become... (3 Replies)
Discussion started by: putter1900
3 Replies

3. Shell Programming and Scripting

Executing a batch of files within a shell script with option to refire the individual files in batch

Hello everyone. I am new to shell scripting and i am required to create a shell script, the purpose of which i will explain below. I am on a solaris server btw. Before delving into the requirements, i will give youse an overview of what is currently in place and its purpose. ... (2 Replies)
Discussion started by: goddevil
2 Replies

4. Shell Programming and Scripting

script for renaming a batch of files

hi i have a folder full of files. some of the names are quite off because the dimensions were the same and i had to put a 'b' after the initial number so that it didnt overwrite. what i want is a script in unix to overwrite the filwe name leaving some of the title intact, e.g. below are some... (3 Replies)
Discussion started by: shabs1985
3 Replies

5. UNIX for Dummies Questions & Answers

Batch Renaming of Files

Hello all, thanks for your time (and this forum, what an awesome resource for newbs like myself!) Anyways, I've been given the task of importing content from a directory of about...7000 HTML files. They are all named appropriately and broken down by name depending on what book they belong too.... (8 Replies)
Discussion started by: gratefulhokie
8 Replies

6. UNIX for Dummies Questions & Answers

Renaming Files using Shell Script

Hi Gurus, I have some files(all ending with .out as extension). Ex: aa1.out aa2.out aa3.out I would like to append each file with the current date to the end of the file so that they should become aa1_20090504.out. So I am using rename as follows: for i in path/aa* ; do mv $i... (5 Replies)
Discussion started by: asmfloyd
5 Replies

7. UNIX for Dummies Questions & Answers

Batch Renaming: Change files' extensions in many sub-directories

Hi all - I'm trying to rename a large number of files all at once and need some help figuring out the command line syntax to do it. I've already done quite a bit of research with the rename and mv commands, but so far haven't found a solution that seems to work for me. So: The files exist... (10 Replies)
Discussion started by: dave920
10 Replies

8. Shell Programming and Scripting

bash - batch script for extracting one file from multiple tar files

so i have hundreds of files named history.20071112.tar (history.YYYYMMDD.tar) and im looking to extract one file out of each archive called status_YYYYMMDDHH:MM.lis here is what i have so far: for FILE in `cat dirlist` do tar xvf $FILE ./status_* done dirlist is a text... (4 Replies)
Discussion started by: kuliksco
4 Replies

9. Shell Programming and Scripting

Converting Shell script to Dos batch files

Hi friends! I am having some simple shell script files to build postgresql database and all. Now i want to convert those scripts to dos batch scripts(to run on windows XP/2000/NT) because there is no need of unix emulation for latest release of postgresql. Please somebody help me. (1 Reply)
Discussion started by: darwinkna
1 Replies

10. Shell Programming and Scripting

Renaming files in a bash script

I'm doing a short batch script to compile po files producing output binary mo files. The compilation command is: msgfmt -o file.mo file.po so in order to compile I am appending .mo to the varible in a loop. It goes something like this: for i in `find . -name "*.po"` do echo... (2 Replies)
Discussion started by: Breen
2 Replies
Login or Register to Ask a Question