This suggested command contains design and implementation errors. Consider what happens on April 1st. The grep match string becomes "Apr01" which will not match a directory list.
A less-than-perfect way of picking up files and directories created today is to match against the date and check that there is a colon character somewhere on the line.
The importance here is to get the "date" command right and to preserve spaces in the date throughout by proper use of double quote characters.
Beware of dates with a single digit day. The command "date +%d" which is wrongly advised in various earlier posts gives the day of the month with a leading zero but "ls -lar" give the day of the month with a leading space. Therefore we must use "date +%e" and preserve that space character.
My example is not a recommended method (mainly because it does not work with filenames containing colon characters and is awkward with filenames containing space characters). My earlier post in this thread is a recommended method.
If you are referring to post #1 , yes. This is mainly because the format of "ls -la" changes for older files (the time gets replaced with the year).
Quote:
I have a few log files which get generated on a daily basis. So, I need to pick only the ones which get generated for that particular day.
There are more techniques available depending on exactly what Operating System and tools you have.
For the moment we'll assume that you don't let an open log run through midnight. This can make scripting "on a daily basis" difficult.
Methyl the code you provided picks the files created on a particular day ie today and lists them on the screen, but my question is, after you run this script, lets say i have 2 files created on the same day, How do i copy these 2 files to a different location..if this is answered then i believe i can close this discussion for good..Please help..Thanks
Well, you cannot hide ' inside ', and you have to do a 'command stdout to string' conversion $(...) or `...`. Think of echo as 'command line string to stdout' converter, and this is the opposite.
Now, if you dislike nesting:
Both of these are ugly if no files, so sometimes, put the list in an env. var.:
If the number of files is really big, like from a long find, then waiting for a complete list is an unconscionable delay, so you want to pipeline parallel more:
but this lacks economy of scale on mv calls (nice for must-do-one-at-a-time commands), so use xargs to batch them up:
xargs is 'pile args from this many stdin lines on the end of the supplied command and execute'. The number 101 is magic, in that you can argue that you got over 99% of any economy of scale while minimizing latency to first mv. Specifying the number to xargs with -n not only limits the delay to first mv, it also says no execute for 0 lines.
The grep pattern needs to be `date '+^%b''%d '` so it both matches the date only at beginning of line (^) not in the file name, and matches the space separator (May1 and not May19 ).
Last edited by DGPickett; 05-19-2011 at 03:36 PM..
Hi All,
I have list of files like below with name abcxyz.timestamp. I need a unix command to pick the latest file from the list of below files. Here in this case the lates file is abcxyz.20190304103200. I have used this unix command "ls abcxyz*|tail -1" but i heard that it is not the appropriate... (2 Replies)
Hi ,
I did the initial search but could not find what I was expecting for.
15606Always_9999999997_20160418.xml
15606Always_9999999998_20160418.xml
15606Always_9999999999_20160418.xml
9819Always_99999999900_20160418.xml
9819Always_99999999911_20160418.xmlAbove is the list of files I... (4 Replies)
Hi Friends,
Newbie to shell scripting
Currently i have used the below to sort data based on filenames and datestamp
$ printf '%s\n' *.dat* | sort -t. -k3,4
filename_1.dat.20120430.Z
filename_2.dat.20120430.Z
filename_3.dat.20120430.Z
filename_1.dat.20120501.Z
filename_2.dat.20120501.Z... (12 Replies)
Hi Friends,
Newbie to shell scripting. Currently i have used the below to sort data based on filenames and datestamp
$ printf '%s\n' *.dat* | sort -t. -k3,4
filename_1.dat.20120430.Z
filename_2.dat.20120430.Z
filename_3.dat.20120430.Z
filename_1.dat.20120501.Z
filename_2.dat.20120501.Z... (1 Reply)
Hi
I have a requirement like below
I need to sort the files based on the timestamp in the file name and run them in sorted order and then archive all the files which are one day old to temp directory
My files looks like this
PGABOLTXML1D_201108121235.xml... (1 Reply)
Hi
In my script i am trying to access mainframe server using FTP,
in the server i have filee with the timestamp.I need to get the file with the latest timestamp among them . The server has the below files
/
ftp> cd /outbox
250 CWD command successful
ftp> ls
200 PORT command successful... (4 Replies)
Hi,
I am new to unix and shell scripting,can anybody help me in sctipting a requirement.
my requirement is to get the latest directory the name of the directory will be like CSB.monthdate_time stamp
like CSB.Sep29_11:16 and CSB.Oct01_16:21.
i need to pick the latest directory.
in the... (15 Replies)
Hi,
I have some list of directories in the form datemonthyear e.g. 02082009, 03082009 and 04082009 etc.
I need to pick the latest directory from the current working directory.
Outcome:
05082009
This is the output am expecting.
Thanks (6 Replies)
I have task in which I need to pickup a set of files from a directory
depending on the following criteria:
Every month 6 files are expected to arrive at /test.
The files come with date timestamp and the latest file set for the month needs to be used
Suppose this is the set of files that present... (5 Replies)