Copying multiple files and appending time stamp before file extension


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copying multiple files and appending time stamp before file extension
# 1  
Old 08-24-2016
Copying multiple files and appending time stamp before file extension

Hi,

I have multiple files that read:

Code:
Asa.txt
Bad.txt
Gnu.txt

And I want to rename them using awk to

Asa_ddmmyytt.txt and so on
...
If there is a single command or more efficient executable please share!

Thanks!

Last edited by Don Cragun; 08-24-2016 at 06:33 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 08-24-2016
How are you generating the datestamp?

I don't know awk, but this snippet should work in bash. Please test first.
Code:
mydate=$(date +%d%m%y%H%M)
for file in *.txt
do
   mv "${file}" "${file%.txt}_${mydate}.txt"
done

I'm simply generating the datestamp using the current date/time. If the extension can be anything you probably want to use this line instead:
Code:
mv "${file}" "${file%.*}_${mydate}.${file##*.}"

Andrew
# 3  
Old 08-25-2016
Quote:
Originally Posted by apmcd47
How are you generating the datestamp?

I don't know awk, but this snippet should work in bash. Please test first.
Code:
mydate=$(date +%d%m%y%H%M)
for file in *.txt
do
   mv "${file}" "${file%.txt}_${mydate}.txt"
done

I'm simply generating the datestamp using the current date/time. If the extension can be anything you probably want to use this line instead:
Code:
mv "${file}" "${file%.*}_${mydate}.${file##*.}"

Andrew
Yes I use the server date! This worked thanks! Would love to have someone show me an awk command as well!
# 4  
Old 08-25-2016
Tbh, I see no practical reason to use awk for this. It would just open an additional process and make it more complicated than this pure shell solution provided.

What do you expect awk to do in this part? Just the name substitution? It can modify a string, but it can not rename files on it own and the date would be handed over from date into awk too.
# 5  
Old 08-25-2016
A practical hint for the future: it pays off to be more detailed and precise when specifying a problem, and nothing should be taken for granted.
While many people interpret ddmmyy as a date format string, tt - at least for me - doesn't ring a bell, and apmcd47's proposal ignores it as well. And, if it were sth like a date format - what date to deploy? Yes, you - in a second post - said "server date", but does that make sense to use something arbitrary, unrelated to anything, the result depending on when the script was run?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Appending time stamp for multiple file

Hi All, I am trying to append time stamp to all file with wild character. If you look above I want take all file with wild card *001* and append current time stamp to it. I did below code. But not sure if there is any easy way that can be done in a single step a=date +%s for... (3 Replies)
Discussion started by: arunkumar_mca
3 Replies

2. Shell Programming and Scripting

How to preserve time stamp while copying a directory from Server B to Server A?

Experts, Please help me out here. How to preserve time stamp while copying a directory from Server B to Server A (3 Replies)
Discussion started by: ahmed.vaghar
3 Replies

3. Shell Programming and Scripting

How to add the dat time stamp just before the last extension of a file?

hi, A file name is in the form inside a variable FILE_NAME="s1.txt.xls" i want to append date and time stamp inbetween s1.txt and .xls. so after appending date time stamp the file name would be MOD_FILE_NAME="s1.txt.201307251422.xls" currently i am using the below code... (4 Replies)
Discussion started by: Little
4 Replies

4. Shell Programming and Scripting

Split a file into multiple files with an extension

Hi I have a file with 100 million rows. I want to split them into 1000 subfiles and name them from 1.xls to 1000.xls.. Can I do it in awk? Thanks, (8 Replies)
Discussion started by: Diya123
8 Replies

5. UNIX for Dummies Questions & Answers

copying files of certain extension?

Hi, I am using scp to copy a certain directory over the network. This folder contain some files that I am not interested in. My question is; is it possible to copy files of certain extension only, keeping the same directory hierarchy as it is (that is sub-folders)? Thanks (8 Replies)
Discussion started by: faizlo
8 Replies

6. UNIX for Dummies Questions & Answers

move files between file systems with privileges, time stamp

Hi I have to move files between file systems but files in new file system must have the same attributes as in old one (privileges, time stamp etc). Which tool is best : - ufsdump / ufsrestore - tar - cpio - pax - dd - mv Or maybe there is sth else, you suggest to use. Thx for help (5 Replies)
Discussion started by: presul
5 Replies

7. UNIX for Dummies Questions & Answers

Removing prefix from multiple files and renaming file extension

Hello i have the files in this format pdb1i0t.ent pdb1lv7.ent pdb1pp6.ent pdb1tj2.ent pdb1xg2.ent pdb2b4b.ent pdb2ewe.ent Now i have to remove the prefix pdb from all the files and also i need to change the extension of .ent to .txt The new file should look like this ... (3 Replies)
Discussion started by: empyrean
3 Replies

8. Linux

copying all files except those with certain extension

Hi, I have a root directory which has a big number of other subdirectories and contains a big number of files. I want to copy all these files and directories to another folder except files with certain extension, say .txt, files - how may I do this? Thanks, faizlo (8 Replies)
Discussion started by: faizlo
8 Replies

9. UNIX for Dummies Questions & Answers

Copy all the files with time stamp and remove header,trailer from file

All, I am new to unix and i have the following requirement. I have file(s) landing into input directory with timestamp, first i want to copy all these files into seperate directory then i want to rename these files without timestamp and also remove header,trailer from that file.. Could... (35 Replies)
Discussion started by: ksrams
35 Replies

10. UNIX for Dummies Questions & Answers

Need to delete the files based on the time stamp of the file

Hi Everyone, I want to delete some files in a path based on the time stamp of the file that is i want to delete the file once in a month. Can any one help me on this? Thanks in advance (2 Replies)
Discussion started by: samudha
2 Replies
Login or Register to Ask a Question