Sequentially rename multiple files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sequentially rename multiple files
# 1  
Old 11-18-2015
Sequentially rename multiple files

Hello team,

We wish to develop a script as follows :

1. Rename multiple files in the following way:
example
Original file names : test.txt and dummy.txt
New file names : test.$(date +"%F").AAAAA<serialno_1>.BBBBBB.p and dummy.$(date +"%F").AAAAA<serialno_2>.BBBBBB.p

2. The script would run every hour where the serialno counter would continue from the count of the last run.

3. The serialno counter would reset at 00:00:00 everyday.

4. The files with the new filenames will be moved to a different location.

We have done the following implementation till now. However, I am finding it a little difficult given my limited knowledge of Linux shell scripting

This is just a test implementation :

Code:
 
for i in {1..5}
do
   mv *.txt test.$(date +"%F").AAAAA$i.111111.p
   echo "file renamed"
done

Could you please help.
Thanks,
Haider
# 2  
Old 11-18-2015
Hello Haider,

Not tested though, could you please try following and let us know how it goes. Also make sure while setting this script to crontab you run it to 00:00 AM of your box time.
Code:
DATE_MIN=`date +%H%M`
if [[ ! -f count_file ]]
then
        count=1
else
        count=`cat count_file`
fi
if [[ $DATE_MIN == 0000 ]]
then
        count=1
fi
 
one=1
for i in *.txt
do
        DATE=`date +"%F"`
        mv $i "$i.$DATE<serialno_$count>.BBBBBB.p"
        if [[ $? == 0 ]]
        then
        count=`expr $count + $one`
        fi
done
echo $count > count_file

Also not sure what do you meant by point like files should move, question is when they should move at 12:00 AM ? or every hour ?


Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 11-18-2015
There's a decision to take: starting the script once at midnight to run all day long sleeping most of the day or having cron start it on the quarter hour. The latter might be somewhat more dependable while the former would make the counter and date handling way easier.
# 4  
Old 11-18-2015
If your script runs every hour, and the serialno resets at 00:00, then you can simply take the current hour as serialno.
The following is suitable for hourly crontab
Code:
ext=$(date +"%F").AAAAA$(date +"%H").BBBBBB.p

renamefile(){
newf=${1%.*}.$ext
test -f "$newf" || mv "$1" "$newf"
}

for i in *.txt
do
  test -f "$i" &&
  renamefile "$i"
done

This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rename multiple files in one go

OS : Oracle Linux 6.8 shell : bash As shown below, I have multiple files like below (query1-extract_aa, query1-extract_ab, query1-extract_ac, ....) $ ls -l total 235680 -rw-rw-r-- 1 reportusr reportusr 30M May 3 11:25 query1-extract_aa -rw-rw-r-- 1 reportusr reportusr 30M May 3 11:25... (5 Replies)
Discussion started by: kraljic
5 Replies

2. 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

3. Shell Programming and Scripting

Rename a multiple files

I have multiple files in folder which i want to rename. hence I am using the below command in my script by I get an error: export XXX_LOG_DIR="${LOG_DIR}/${XXX_HOST}/xxx/${REPORT_DATE}" mv $XXX_LOG_DIR/*.audit.gz $XXX_LOG_DIR/*.audit.log.gz But I get the below error: mv: target... (5 Replies)
Discussion started by: karan8810
5 Replies

4. 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

5. Shell Programming and Scripting

Rename multiple files

hello: I have multiple files with names like: somestring_y2010m01d01 somestring_y2010m01d02 .......... somestring_y2010m12d31 How... (4 Replies)
Discussion started by: sylcam
4 Replies

6. Shell Programming and Scripting

Rename the multiple files

Hi I need to reanme the multiple file using unix script I have multiple file like: sample_YYYYMMDD.xls test new_YYYYMMDD.xls simple_YYYYMMDD.xls I need to rename this file sample.xls testnew.xls SIMPLE.xls thanks (8 Replies)
Discussion started by: murari83.ds
8 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

rename multiple files

Hi all, I have some files like: pickup.0000043200.t001.t001.data pickup.0000043200.t001.t002.data pickup.0000043200.t002.t001.data pickup.0000043200.t002.t002.data pickup.0000043200.t003.t001.data pickup.0000043200.t003.t002.data I need to rename these files to ... (3 Replies)
Discussion started by: a_dor8
3 Replies

9. UNIX for Dummies Questions & Answers

Copying files multiple times increasing sequentially

I am looking for the easiest way to copy a set of files 1000 times and increment sequentially. I want to copy these 2 files: Scenario.1.1.ud Scenario.1.2.ud So that it creates the following: Scenario.2.1.ud Scenario.2.2.ud Scenario.3.1.ud Scenario.3.2.ud .. .. Scenario.1000.1.ud... (2 Replies)
Discussion started by: JPOrlando
2 Replies

10. UNIX for Dummies Questions & Answers

Rename multiple files

Hello, I want to rename multiple files at a time and I don't know how to do it. I have various ".mp3" files, like "band name - music name.mp3" and I want to remove the "band name" from all files. Anybody knows how to do it using shell script or sed or even perl? Thanks (7 Replies)
Discussion started by: luiz_fer10
7 Replies
Login or Register to Ask a Question