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:
But, this gives only if the file is found or not but i need to copy as well if zip file is found.
Data :
Moderator's Comments:
edit by bakunin: please use CODE-tags for code, data and terminal output. Thank you.
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:
will list everything in the directory: files, directories, whatever. As you are interested only in files you can specify:
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:
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.:
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:
I suggest you consult the man pages about find about the exact terms and limitations of this procedure.
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:
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.
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)