mv files add date string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting mv files add date string
# 1  
Old 01-12-2012
mv files add date string

I have a requirement where I need to move files that are being written too. I am unable to use sed since I believe this will stop the files from getting written. There is a directory containing about 50 unique files, the date is the same on all of them.

I need to change the date string on each file name to yesterday. So example:

Code:
224.0.26.10_10017_20120112.mcrec
224.0.26.10_10017_20120112.mcrec.idx
224.0.26.102_10044_20120112.mcrec
224.0.26.102_10044_20120112.mcrec.idx

Needs to become:

Code:
224.0.26.10_10017_20120111.mcrec
224.0.26.10_10017_20120111.mcrec.idx
224.0.26.102_10044_20120111.mcrec
224.0.26.102_10044_20120111.mcrec.idx

I know I can try a loop of some sort. I just don't know how to target just the date since the files are unique.


Code:
dateYest=$(date -d yesterday +%Y%m%d)

for i in /dir/* 
do

mv SomeMagic > MoreMagic$dateYest 

done.

Maybe I am wrong and I can get away with the -i in place sed feature. If that is the case can someone confirm that I will not disrupt the file getting written too?

thanks,
jaysunn
# 2  
Old 01-12-2012
Hi,
based on the hypothesis that filenames structure is always the same:
nnn.nnn.nnn.nnn_nnnnn_yyyymmdd.xxxx
or
nnn.nnn.nnn.nnn_nnnnn_yyyymmdd.xxxx.idx
you may obtain replacement of the <_yyyymmdd.> substring with a new value with instructions like the following:
Code:
dateYest=$(date -d yesterday +%Y%m%d)

for i in `ls /dir/*` 
do

   newfilename=`echo $i | awk -v newdate="$dateYest" { sub(/_[0-9]*\./ , "_" newdate "." ) ; print }`
   mv $i $newfilename 

done

The result will be that the each file in the loop is renamed with the value of variable $newfilename.
At this time I didn't have a chance of testing myself, so do some tests on copies of the original files.

see ya
fra
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert string to date and add 20 days

Hi, I have a requirement where I am getting date in string format (20161130). I need to add 20 days(not no. 20) to the above string. The o/p should 20161220. In case of 20170228, it should show 20170320. Could you please help me with the command to achieve this. Note: I am using AIX 7.1... (5 Replies)
Discussion started by: satyaatcgi
5 Replies

2. Shell Programming and Scripting

Convert string to date and add 1 hours

i have some set of date data inside csv files and need to convert the timezone, 08302016113611861 08302016113623442 08302016113541570 08302016113557732 08302016113548439 08302016112853115 08302016113620684 08302016113432827 08302016113630321 date format is : %m%d%Y%H%M%Smilisec ... (2 Replies)
Discussion started by: before4
2 Replies

3. Shell Programming and Scripting

Extract count of string in all files and display on date wise

Hi All, hope you all are doing well! I kindly ask you for shell scripting help, here is the description: I have huge number of files shown below on date wise, which contains different strings(numbers you can say) including 505001 and 602001. ... (14 Replies)
Discussion started by: VasuKukkapalli
14 Replies

4. Shell Programming and Scripting

Match string in two files and add data to one file

Gents, file1 S 65733.00 19793.00 1 0 318592.8 2792489.5 29.1063000008 S 65733.00 19801.00 1 0 323120.8 2789153.6 13.3063000044 S 66009.00 19713.00 1 0 318672.7 2792538.2 30.6063000120 S 65801.00 19799.00 1 ... (2 Replies)
Discussion started by: jiam912
2 Replies

5. Shell Programming and Scripting

Add Date string at end of line

HOWTO Append Date String (MMYYYY) to End of Line file A.txt B.txt Output: A_052014.txt B_052014.txt (9 Replies)
Discussion started by: satish1222
9 Replies

6. Emergency UNIX and Linux Support

Convert string to date and add 1

Hi All, I want to convert string in format YYYYMMDD(20120607) to date in unix and add 1 day to it and convert back to string in format YYYYMMDD. Please help. (4 Replies)
Discussion started by: cns1710
4 Replies

7. Shell Programming and Scripting

Find and copy files based on todays date and search for a particular string

Hi All, I am new to shell srcipting. Problem : I need to write a script which copy the log files from /prod/logs directory based on todays date like (Jul 17) and place it to /home/hzjnr0 directory and then search the copied logfiles for the string "@ending successfully on Thu Jul 17". If... (2 Replies)
Discussion started by: mail.chiranjit
2 Replies

8. Shell Programming and Scripting

ksh shell script to add date (YYYYMMDDHHMISS) to all .txt files in a folder

Everyday 15 files are written to a folder \app\where\thefiles\are\destined\CURRFOLDER Task1: I need to add date in YYYYMMDDHHMISS format to each of them. Example: File: ACCOUNT.txt Should be updated as: ACCOUNT_20101005175059.txt Task 2: After I update the files, they need to be ... (2 Replies)
Discussion started by: Duminix
2 Replies

9. Shell Programming and Scripting

add a particular string to particular files

Hiii I wanted to add a particular string using grep to all the files of a specific size say add a string empty to files of zero size would appreciate if u could use "find" me not comfortable with sed or awk. Thanks (2 Replies)
Discussion started by: gr8pals
2 Replies

10. Shell Programming and Scripting

Backup files per Date String

Guys, I've got a quick logic question. I'm pretty savvy when it comes to shell scripting, however, I'm at a loss when it comes to writing a simple shell script to backup only certain files out of the month. I have a directory, for example, /data/backups/websites/domain. In each "domain"... (5 Replies)
Discussion started by: drewrockshard
5 Replies
Login or Register to Ask a Question