Visit Our UNIX and Linux User Community

Find and copy .zip file based on today's date

Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Find and copy .zip file based on today's date
# 1  
Old 07-02-2019
Find and copy .zip file based on today's date

Hi Team,
I'm new to unix and i have a requirement to copy or move files from one directory to another based on current date mentioned in the .zip file name. Note that i need to copy only the recent zip file. please help me with the code
i tried the code as:
#! /usr/bin/sh
find /var/tmp/NewFile/TestM/ -name '*.txt' -mtime 0 | while read FILE
        echo "Found file $FILE"

But, this gives only if the file is found or not but i need to copy as well if zip file is found.
Data :
-rw-rw-rw-   1 user  user     1697 Jul 02 09:57 file_02072019.txt
-rw-rw-rw-   1 user  user      103 Jul 02 09:57

Moderator's Comments:
Mod Comment edit by bakunin: please use CODE-tags for code, data and terminal output. Thank you.

Last edited by bakunin; 07-02-2019 at 07:22 AM..
# 2  
Old 07-02-2019
Originally Posted by midhun3108
#! /usr/bin/sh
find /var/tmp/NewFile/TestM/ -name '*.txt' -mtime 0 | while read FILE
        echo "Found file $FILE"

OK, this is a good start. The find utility provides a list of filenames fitting the clauses you specifiy. Here is the principle of how it works. I suggest you try the following commands on the commandline to get acquainted:

find /var/tmp/NewFile/TestM/

will list everything in the directory: files, directories, whatever. As you are interested only in files you can specify:

find /var/tmp/NewFile/TestM/ -type f

The -type f limits the output to only files, skipping directories and other things. But you are not interested in all files, only the ones with a certain extension: "zip", if i got you right. Therefore:

find /var/tmp/NewFile/TestM/ -type f -name "*zip"

This will reduce the outcome further to only files (-type f) named "zip" (-name "*zip") at the end. You can use other clauses (as you have done) to further limit the result set.

Right now you pipe this list into a while-loop and process it there. In the version you showed us only an echo-statement is inside the loop and therefore the filename is simply displayed, nothing else. You could now put other commands into the loop to process the files in the way you want, i.e.:

#! /usr/bin/sh
find /var/tmp/NewFile/TestM/ -name '*zip' -mtime 0 | while read FILE
        echo "Found file $FILE"
        cp "$FILE" /some/where/else

but there is a much better and more elegant approach: find can itself process the files found using the -exec clause: the filename found is represented by "{}" and you need to terminate the "commandline" by "\;". Therefore:

find /var/tmp/NewFile/TestM/ -name '*zip' -mtime 0 -exec cp {} /some/where/else \;

I suggest you consult the man pages about find about the exact terms and limitations of this procedure.

I hope this helps.

This User Gave Thanks to bakunin For This Post:
# 3  
Old 07-02-2019

There's a few ways to approach this, but using the find command as you're starting to do will certainly do the trick. However, as things stand, your find command isn't actually going to do anything other than print out the message Found file <FILENAME> for all files with the extension txt underneath the directory /var/tmp/NewFile/Test.

Taking things in turn:

-name '*.txt'
This will match all files whose name ends with the extension .txt

-mtime 0
Not one I've seen often phrased in this particular way, but this will have the effect of matching all files modified between now (the time when the command is run) and one day ago. Personally I usually phrase this as -mtime -1 (meaning all files modified less than one day ago), but this is fine, if that's what you're wanting to find.

Now, you then go on to pipe the output into a while loop for some reason, which I'm not too clear on - you don't really have to do this, as the find command is capable of operating directly on what it finds via the -exec flag. This will cause any given command to be executed on whatever the find command ha found.

So, for example: let's say you wanted to find all files whose extension was .txt and which were modified less than one day ago, and then you wanted to move them to another directory (let's call it /tmp/recent-files for the sake of our example). This could be achieved with a single find command like this:

find /var/tmp/NewFile/Test -mtime 0 -name '*.txt' -exec mv -fv \{\} /tmp/recent-files/ \;

So let's look at this last bit in more detail, as this is probably new to you.

Firstly, -exec is the flag that tells find that we want to run a command on all the things we've found that match our patterns up to this point.

Next, we have mv -fv \{\} /tmp/recent-files/ \; - this is the actual command we want to run, used with some find-specific syntax. Breaking it down further: mv -fv is the first part of our move command, and is just as normal (the flags you use here can be whatever you want, but I went with -fv to ensure files get moved no matter what, and that it will print the name of each file it moves).

Now, here comes the interesting bit. The meaning of \{\} is "the filename of whatever matched file we are currently considering". So the two curly brackets are a stand-in for the filename of whatever thing find has found which matched your conditions.

Next is the second part of the mv command, which is the destination you want to move things to - in our case, that's /tmp/recent-files/.

Lastly, we end with a single semi-colon symbol, \;. This marks the end of our -exec to the find command.

So the end result of this particular find command is to match all files underneath /var/tmp/NewFile/test which were modified between now and 1 days ago and which end with the extension .txt, and to move all such matching files one at a time into the directory /tmp/recent-files/.

Hope this helps ! You should be able to play about with the syntax of the above to achieve whatever result you're wanting, but if you have any other questions let me know and I'll see if I can lend a further hand.
This User Gave Thanks to drysdalk For This Post:
# 4  
Old 07-02-2019
Thanks a lot bakunin and drysdalk for providing detailed explanation. It made my understanding clear. I think ill use exec command as suggested instead of while loop for this operation.

Previous Thread | Next Thread
Test Your Knowledge in Computers #15
Difficulty: Easy
UDP is a connection-oriented protocol.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How can we Zip multiple files created on the same date into one single zip file.?

Hi all i am very new to shell scripting and need some help from you to learn 1)i have some log files that gets generated on daily basis example: i have abc_2017_01_30_1.log ,2017_01_30_2.log like wise so i want to zip this 4 logs which are created on same date into one zip folder. 2)Post zipping... (1 Reply)
Discussion started by: b.saipriyanka
1 Replies

2. UNIX for Beginners Questions & Answers

How can we Zip multiple files created on the same date into one single zip file.?

Hi all i am very new to shell scripting and need some help from you to learn 1)i have some log files that gets generated on daily basis example: i have abc_2017_01_30_1.log ,2017_01_30_2.log like wise so i want to zip this 4 logs which are created on same date into one zip folder. 2)Post zipping... (2 Replies)
Discussion started by: b.saipriyanka
2 Replies

3. Shell Programming and Scripting

Find and awk with today's date

Hi All, Solaris 10 o/s With your help I developed the following script. find /oracle/diag/rdbms/*/*/trace -type f -name '*d00*.trc' -mtime 0 -exec egrep –c 'NS Primary Error' '{}' '+' which returns the counts I needed nelow: /oracle/diag/rdbms/musidp/musidp/trace/abcdef_d001_21751.trc:15... (9 Replies)
Discussion started by: bdby
9 Replies

4. Shell Programming and Scripting

File Copy based on file receive date

I have bunch of files in my source folder like below and every day based on date I am receiving file ex: Nov 28,Nov 29,Nov 30 ... -rw-rw-r--+ 1 root root 20 Nov 27 06:00 aaaa27.txt -rw-rw-r--+ 1 root root 20 Nov 28 06:00 aaaa28.txt -rw-rw-r--+ 1 root root 20 Nov 29 06:00 aaaa29.txt I... (4 Replies)
Discussion started by: krish2014
4 Replies

5. Shell Programming and Scripting

Find the latest file based on the date in the filename

Hi, We've a list of files that gets created on a weekly basis and it has got a date and time embedded to it. Below are the examples. I want to find out how to get the latest files get the date and time stamp out of it. Files are PQR123.PLL.M989898.201308012254.gpg... (1 Reply)
Discussion started by: rudoraj
1 Replies

6. Shell Programming and Scripting

Shell Scripting: Copy Files with Today's date

I was wondering the best way about finding files that were created today and copy them to a directory (grep ?). There can be multiple files for todays date or none. I am looking to copy all of the .lis files for todays date. I may need to modify the filename to include todays date but for the... (4 Replies)
Discussion started by: smkremer
4 Replies

7. Shell Programming and Scripting

Find file that matches today's date in filename and move to /tmp in bash

I'm having problems with my bash script. I would like to find a file matching today's date in the filename, i.e. my_file_20120902.txt and then move it to a different directory, i.e. /tmp. Thanks. (1 Reply)
Discussion started by: jamesi
1 Replies

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

9. Shell Programming and Scripting

Identify log files based on time stamp,zip and then copy..HELP

Hi All, PFB is a requirement. I am new to shell scripting. So plz help. It would be highly appreciated. 1. choose all the log files based on a particular date (files location is '/test/domain')--i.e,we should choose all the files that are modified on 29th November, neither 28th nor 30th 2.... (3 Replies)
Discussion started by: skdas_niladri
3 Replies

10. Shell Programming and Scripting

Script to copy log files with today's date

I am a newbie to scripting. I need a korn shell script to copy log files of current day to archive folder and rename with current days date stamp. I would really appreciate your help. File structure is as follows. Everyday files get overwritten, so I need copy to a archive directory and... (3 Replies)
Discussion started by: mdncan
3 Replies

Featured Tech Videos