Append timestamp create .trg file for all content of an unzipped archive


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append timestamp create .trg file for all content of an unzipped archive
# 1  
Old 02-26-2014
Linux Append timestamp create .trg file for all content of an unzipped archive

Hi,
I have a test.zip archive that contains
test.zip --> (file_1.txt, file_2.txt , file_3.txt)

I need to unzip the file like this,
Code:
file_1_timestamp.txt
file_1_timestamp.trg
file_2_timestamp.txt
file_2_timestamp.trg
file_3_timestamp.txt
file_3_timestamp.trg

Could you please let me know that, How to do this job through shell script (ksh).
Thanks in advance

Moderator's Comments:
Mod Comment Would be cool if next time you use code tags for your code and data…

Last edited by vbe; 02-26-2014 at 07:10 PM..
# 2  
Old 02-26-2014
You mean the current timestamp?

Something like this?

Code:
unzip test.zip
cd test
rename "s/\./_$(date +%H%M%S)\./" *

--ahamed
# 3  
Old 02-26-2014
Thanks Ahamed,
I have to do this as a postprocessing script after receiving the zip archive,
I can't rename all contents of the directory because of it's a shared directory
Code:
1- unzip file.zip 
2- for each file in zip archive "append filename+ current timestamp"
3- for each file created in last step create a copy/touch with trg file extention "filename+ current timestamp.trg"
4- results will be in a shared directory

what do you think?
# 4  
Old 02-26-2014
Try this...

This will rename all the files inside the test directory which will get created once you unzip the file with the current time stamp.
Then it will create a copy of each file with the extenstion .trg
Once done, you can move all the files to the shared folder or wherever you want.

Code:
unzip test.zip
cd test
rename "s/\./_$(date +%H%M%S)\./" *
ls | xargs  -l sh -c 'cp $0 ${0%.*}.trg'

--ahamed
# 5  
Old 02-27-2014
unfortunately on my box i found util versione of "rename", so i found this code;
Code:
for file in *.zip
  do
      mv $file ${file}`date +%Y%m%d'
  done

ls | xargs  -l sh -c 'cp $0 ${0%.*}.trg'

but if my process launchs two instance of script after of reception of two seperate .zip archive, under my work dir i will have two set of file, how can i determine which file belongs to first script invocation and which group belongs to second instance of script, any idea, thanks anyway Ahamad
# 6  
Old 02-27-2014
Quote:
Originally Posted by Shandel
but if my process launchs two instance of script after of reception of two seperate .zip archive, under my work dir i will have two set of file, how can i determine which file belongs to first script invocation and which group belongs to second instance of script, any idea, thanks anyway Ahamad
'mv' with a destination inside the same folder should be atomic. Only one will end up renaming the file, the other will harmlessly say 'no such file or directory'.
# 7  
Old 02-27-2014
I am confused on what you want now.

The following code when run from the directory where the zip files exists will

For all the zip files
1. unzip the file
2. cd to the unzipped folder
3. rename the file with timestamp
4. create a copy of the renamed file with the extension .trg

Code:
for file in *.zip; do
  unzip $file
  cd ${file%.*}
  for fl in *; do
    ext=${fl##*.}
    new_fl=${fl/.*/}_$(date +%Y%m%d)$ext
    mv $fl $new_fl
    cp $new_fl ${new_fl/.*/.trg}
  done
  cd ../
done

Untested, should work.

--ahamed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove exisiting file content from a file and have to append new file content?

hi all, i had the below script x=`cat input.txt |wc -1` awk 'NR>1 && NR<'$x' ' input.txt > output.txt by using above script i am able to remove the head and tail part from the input file and able to append the output to the output.txt but if i run it for second time the output is... (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

2. Shell Programming and Scripting

Renaming unzipped files with name of archive

I am attempting to rename files within a zipped archive with the beginning of the name of the zip file. For example unzip AAA_000.zip and rename file1.csv, file2.txt to AAA_file1.csv, AAA_file2.txt. I am able to do this for a zip file with one file inside, but not for multiple files. This is... (2 Replies)
Discussion started by: lcp
2 Replies

3. UNIX for Dummies Questions & Answers

How to append portion of a file content to another file when a certain pattern is matching?

Hi ladies and gentleman.. I have two text file with me. I need to replace one of the file content to another file if one both files have a matching pattern. Example: text1.txt: ABCD 1234567,HELLO_WORLDA,HELLO_WORLDB DCBA 3456789,HELLO_WORLDE,HELLO_WORLDF text2.txt: XXXX,ABCD... (25 Replies)
Discussion started by: bananamen
25 Replies

4. Shell Programming and Scripting

Create archive and nil the content of log file using script

Plese help I need a urgent requirement. Ex: test.log requirement : using shell script I need to archive the log file and nil and the content of (test.log) file to 0 kb and then in the archive folder log files are name to test.tar test1.tar test2.tar EX: /home/abc/ test.log ... (1 Reply)
Discussion started by: johney1981
1 Replies

5. Shell Programming and Scripting

Create a file from ls -l command and append additional info to results

I need to be able to take the results from ls -l command and modify the output as follows: I will run ls -l *.mak My results will be aa.mak bb.mak cc.mak I then need to take those results and create a file that has the following info: dsjj/ubin/aa dsjj/ubin/bb dsjj/ubin/cc ... (3 Replies)
Discussion started by: jclanc8
3 Replies

6. Shell Programming and Scripting

if file is NOT empty, then append content to file

hi people, i have texts down.txt and down-new.txt and i want to check; - if down-new.txt is NOT empty, then write date and its content to /home/gc_sw/down.txt for example; down.txt:AAAA SSSS down-new.txt:123 456 and after checking down-new.txt is NOT empty, down.txt should... (10 Replies)
Discussion started by: gc_sw
10 Replies

7. UNIX for Dummies Questions & Answers

Append file with grep output but add timestamp?

I've setup a cron job that greps a file every five minutes and then writes (appends) the grep output/result to another file: grep "monkey" zoo.log | tail -1 >> cron-zoo-log Is there any way I can add the date and time (timestamp) to the cron-zoo-log file for each time a new line was added? ... (12 Replies)
Discussion started by: Sepia
12 Replies

8. Shell Programming and Scripting

how to create file.txt and add current date in file content

Hey guy, how to make bash script to create foo.txt file and add current date into file content and that file always append. example: today the script run and add today date into content foo.txt and tomorrow the script will run and add tomorrow date in content foo.txt without remove today... (3 Replies)
Discussion started by: chenboly
3 Replies

9. UNIX for Dummies Questions & Answers

create a archive of old file

i need a script which will create a archive of the files older than 10 days........ (2 Replies)
Discussion started by: jayaramanit
2 Replies

10. UNIX for Dummies Questions & Answers

Create Year directory, date subdirectory and archive the file

Hi, After checking all the UNIX threads, I am able to come up with a solution so far. I am working on a shell script where it moves the files to a certain directory. The conditions to check are 1) Check if the file exists in the current directory. 2) Check if the destination directory... (2 Replies)
Discussion started by: madhunk
2 Replies
Login or Register to Ask a Question