How to copy file using awk based on time creation to some directory

0

I trying to copy a file to directory using awk filter based on time creation file

#!/bin/bash
dir=/home/resaputra23/pyexample/python2/*
des=/home/resaputra23/pyexample/backup/
file1= stat $dir | awk {'print $2'} | grep 2014

for i in $file1
do
cp $i $des || echo "unable to copy $i"
done

I get output

2014-12-02
2014-12-01
2014-12-28

But no copy file in $des

user3492614

Posted 2015-04-22T16:36:33.507

Reputation: 1

Some notes: to have a fast check of what is inside $file1 you can put before your for an echo $file1 ; exit 0, and remove when all will work. If you want to include the output of that pipe of commands in the variable file1 you should write file1=$(cmd1|cmd2...). To stat I'm afraid that it's possible to have the same doubt about the use of ls parsing opportunity. imagine the error you can incur if a filename tomorrow will have one space... You can try to protect somehow with "$1" but is safer to use find.

– Hastur – 2015-04-22T18:14:22.537

Answers

0

try this:

!/bin/bash
dir=/home/resaputra23/pyexample/python2/
des=/home/resaputra23/pyexample/backup/
#insert your prefered since date    
sincedate="2015-01-01"
find $dir -newermt "$sincedate" > files.txt
while read files; do
    cp $files $des
done < files.txt
rm -f files.txt

Francisco Tapia

Posted 2015-04-22T16:36:33.507

Reputation: 2 383

Can you explain the use of files.txt and rm in the last line? – Jason Aller – 2015-04-22T17:34:20.867

in fifth line made a file to read the list of files to run the loop, and after the task is complete it will be removed. is a way, greetings. – Francisco Tapia – 2015-04-22T17:36:43.790

2Hi Francisco welcome on SuperUser. I like your answer, but there are some dangers parsing the ls output: it is simple and works fast but it hides problem mainly related to spaces and special characters that are allowed inside a filename... maybe better to use find. BTW I would like to suggest you t=$(tempfile) to create a temporary file with a name unique (there is a nice example of bash scripting inside man tempfile too). – Hastur – 2015-04-22T18:29:05.253

Ty Hastur and your are absolutly right, i got your point and ill mod my answer, ive noticed about ls output give me more information that i wanted find is the best option. – Francisco Tapia – 2015-04-22T18:32:57.077

2@JasonAller He is redirecting (>files.txt) the output of a command to a temporary file. After he takes it as input for his cycle while read...<files.txt. Then he deletes it with rm -f file.txt. In practical uses and in the most part of the cases, under Linux that file will remain in cache and it will never be written the on HDD because deleted fast. If the output is in a file you can use it later more times with no need to run that command again. I agree it's not needed and it could be done inline while ifs= read $( find ...), moreover it's still not safe against uncommon file names. – Hastur – 2015-04-26T10:51:27.780

1@FranciscoTapia You're welcome. I'm sorry to have to tell you that you are still not protected against uncommon file names, the ones that usually love to appear later and in the most critical moment :-) Imagine you have a pdf file that somebody will save taking the new name from the title inside the document, and imagine that tile on two or more lines. You can have a saved file with a newline character inside. Such names will be probably saved in more lines on your temporary file. – Hastur – 2015-04-26T11:02:29.070

@Hastur heya, can u show me the right way. – Francisco Tapia – 2015-04-26T15:43:49.190

1

I do not think it exists the right way, but I prefer to think there are many way to do a thing. But I can say for sure that I experienced many time a wrong one ;) BTW here I answered in a way that seems to me to keep count of space and \n newlines inside.

– Hastur – 2015-04-28T14:31:01.390

0

I suggest to use find. The following command will select and move all the files.

find "$Dir" -newermt "1 Jen 2014" ! -newermt "1 Jen 2015" -exec cp {} $Dest \;

Some words more: in general there are many reason about Why it is better to avoid to parse the output of ls, mainly because of space or special characters eventually present in the file name.

Excerpt from man find, for the use of the option -newerXY

-newerXY reference
Compares the timestamp of the current file with reference. The reference argument is normally the name of a file (and one of its timestamps is used for the comparison) but it may also be a string describing an absolute time. X and Y are placeholders for other letters, and these letters select which time belonging to how reference is used for the comparison.

       a   The access time of the file reference  
       B   The birth time of the file reference  
       c   The inode status change time of reference  
       m   The modification time of the file reference  
       t   reference is interpreted directly as a time  

Hastur

Posted 2015-04-22T16:36:33.507

Reputation: 15 043