i want to save current day file daily
for this is am using below command.
cp -p $(ls -lrt | grep "Apr 15" | awk '{print $9}'
in order to script this part, i am saving date output in a file using below command
date | awk '{print $2,$3}' >>t1
thru below command i want to list the file of specific date but below command is listing all the content.
ls -lrt | grep -w "$t1"
o/p
drwxr-xr-x 2 root system 256 Mar 21 2012 lost+found
-rw-r----- 1 root system 25 Aug 05 2013 cfgvg.out
srwxrwxrwx 1 root system 0 Aug 05 2013 dpi_socket
-rw-r----- 1 root system 102 Apr 15 01:17 loc_file
-rw-r----- 1 root system 0 Apr 15 01:17 fget_config_lock
-rw-r----- 1 drsadm drsadm 7 Apr 15 16:59 t1
What you are doing is cumbersome - so, consider using the find command.
To directly answer what I can:
This gives the most recent file name only , assuming you have already executed a cd command to be in the right directory --
t1=$(ls -rt filename* | tail -1)
#
t1=$(ls -rt | tail -1)
I do not understand your code very well - so this is the only contribution I can make at this point.
ON second thought - please tell us in words what you want - not how you think it should be coded. That would help a lot, I think.
RudiC
April 15, 2015, 12:06pm
4
Trying to understand and optimize your approach - do consider jim mcnamaras comments, though - I think this might do what you want:
ls -l | awk -vDT=$(date +"%b %d") '$0 ~ DT {print $9}'
Please note that
no grep is needed when you're using awk anyhow
no awk for date is needed, use its format option
ls does not need the -rt options i your case
The error in post#1 is, you're writing the results to a file but try to expand a variable of the same name which is empty.
first of all i am very sorry if i was not able to explain my question
here i am again explaining it.
My motive is to save all the file which are generating daily.
as i do not want to do this manually i am trying to write a script for this.
Now the command which i am using here, to list current day file is mentioned as below. asuming current date is 10th May 2015
cp -p $(ls -lrt | grep "May 10" | awk '{print $9}' ---1
using above command i can list all the files of 10th May.
now to script this here i am saving date in file using below command
date | awk '{print $2,$3}' >t1 ---2
now here i am checking if it is working or not for this here i am using below command
ls -lrt | grep -w "$t1" ---3
but what i see here is that it is listing all the files.
-rwxrwxrwx 1 adm adm 654 Jun 15 2010 collect
-rw-r----- 1 adm adm 1 May 10 20:15 a
-rw-r----- 1 adm adm 1 May 10 20:15 b
-rw-r----- 1 adm adm 7 May 10 20:15 t1
what i expect from the command to do is to list only 10th may file.
here what i am expecting from command No.3 is, to work similar to the below command
ls -lrt | grep "May 10"
I hope this time i am able to explain my concern.
regards
scriptor
---------- Post updated at 08:50 PM ---------- Previous update was at 08:36 PM ----------
Hi Rudic
i tried your command but i am getting below error
awk: 0602-533 Cannot find or open file $0 ~ DT {print $9}.
The source line number is 1.
The assignment is for awk - not a shell assignment. Therefore needs quotes to prevent word splitting.
ls -l | awk -vDT="$(date +"%b %d")" '$0 ~ DT {print $9}'
Or use this precise one
ls -l | awk -vD=$(date +%b) -vT=$(date +%d) '$6==D && $7==T {print $9}'
ls -l | awk -vDT="$(date +"%b %d")" '$0 ~ DT {print $9}'
can you please explain me the use of
-vDT
and
~ DT
option in above command
i tried to figure out but didn't get exact one over net .
RudiC
July 16, 2015, 4:20am
8
man awk :
so, -vDT defines the variable DT to be used inside the awk script.
And, ~ DT is not an option, but the matching operator; $0 ~ DT checks for a match.