How to save current day files only?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to save current day files only?
# 1  
Old 04-15-2015
How to save current day files only?

i want to save current day file daily
for this is am using below command.
Code:
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
Code:
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.
Code:
 
ls -lrt | grep -w "$t1"

o/p
Code:
 
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

# 2  
Old 04-15-2015
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 --

Code:
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.
# 3  
Old 04-15-2015
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.
# 4  
Old 04-15-2015
Trying to understand and optimize your approach - do consider jim mcnamaras comments, though - I think this might do what you want:
Code:
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.
# 5  
Old 05-10-2015
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
Code:
 
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
Code:
 
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
Code:
 
ls -lrt | grep -w "$t1" ---3

but what i see here is that it is listing all the files.
Code:
 
-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
Code:
 
awk: 0602-533 Cannot find or open file $0 ~ DT {print $9}.
 The source line number is 1.

# 6  
Old 05-10-2015
The assignment is for awk - not a shell assignment. Therefore needs quotes to prevent word splitting.
Code:
ls -l | awk -vDT="$(date +"%b %d")" '$0 ~ DT {print $9}'

Or use this precise one
Code:
ls -l | awk -vD=$(date +%b) -vT=$(date +%d) '$6==D && $7==T {print $9}'


Last edited by MadeInGermany; 05-10-2015 at 04:50 PM..
This User Gave Thanks to MadeInGermany For This Post:
# 7  
Old 07-16-2015
Code:
ls -l | awk -vDT="$(date +"%b %d")" '$0 ~ DT {print $9}'

can you please explain me the use of
Code:
 -vDT

and
Code:
 ~ DT

option in above command
i tried to figure out but didn't get exact one over net .
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to get first & last day of a month from current date?

Hi, I need the first & last day of a month from any given date. For better understanding, if i need to back-fill data for date 07/20/2019 i.e July 20 2019, i need the first & last day has 07/01/2019 - 07/31/2019. FYI: I'm using GIT BASH terminal. sample code: export DT=$(date --date='6 days... (2 Replies)
Discussion started by: Rocky975583
2 Replies

2. Shell Programming and Scripting

Save all the file of current date in another dir?

Hi i want to copy all the files of current date in another directory. for example, below i want to save all the file of 26 march to copied in debug dir. $ ls -lrt | tail -5 -rwxrwxrwx 1 khare guest 73 Jan 6 12:35 chk -rw-r--r-- 1 khare guest 770 Mar 26 02:21 cc1... (2 Replies)
Discussion started by: scriptor
2 Replies

3. Shell Programming and Scripting

To get Non matching records for current day

My objective is to get the non matching records of previous day with current day. eg, file1 contains 1 a 2 b and file2 contains: 2 b 3 c then expected output is 3 c¨ another example file 1 contains: 1 a 2 b file 2 contains 1 c 2 b (8 Replies)
Discussion started by: newbie2014
8 Replies

4. UNIX for Dummies Questions & Answers

Move the files between Current day & a previous day

Hi All, I have a requirement where I need to first capture the current day & move all the files from a particular directory based on a previous day. i.e move all the files from one directory to another based on current day & a previous day. Here is what I am trying, but it gives me errors.... (2 Replies)
Discussion started by: dsfreddie
2 Replies

5. UNIX for Dummies Questions & Answers

current day remote files from FTP

Hi All, I have to work on a korn shell script to pick up only the current day files dropped on the remote server (using ftp). The file do not have daytimestamp on it. It has to be based on server time (AIX) The file naming convention is "test_file.txt" When I log in into the ftp account... (15 Replies)
Discussion started by: pavan_test
15 Replies

6. Shell Programming and Scripting

I need to back up a bunch of files on a directory and save that file as the current date....

this is what i have to find the files modified within the past 24 hours find . -mtime -1 -type f -print0 | xargs -0 tar rvf "$archive.tar" however i need to save/name this archive as the current date (MM-DD,YYYY.tar.gz) how do i doo this (1 Reply)
Discussion started by: bugenhagen_
1 Replies

7. Shell Programming and Scripting

Command to list current day files only

Hi All, can anyone pls share the command to list the files of current day only. i want to check if there are any files in a particular directory which are not of current date. (6 Replies)
Discussion started by: josephroyal
6 Replies

8. UNIX for Dummies Questions & Answers

I want to get day from the current system date

Hi, I want to check what day is today (like mon,Tue,wed) When i checked the syntax, i dont see there is a format specifier for getting the day. Let me know how to get the same. I am very new to unix and so I am asking some basic questions. cheers, gops (2 Replies)
Discussion started by: gopskrish
2 Replies

9. Shell Programming and Scripting

delete files one day old in current month only

i want to delete files that are one day old condition is files should be of current month only ie if iam running script on 1 march it should not delete files of 28 feb(29 if leap year :-)} any modifications to find $DIR -type f -atime +1 -exec rm -f{}\; (4 Replies)
Discussion started by: maverick
4 Replies

10. Shell Programming and Scripting

How to compare prev day file to current day file

Hi all: I am new to this board and UNIX programming so please forgive if I don't explain something correctly. I am trying to write a script to keep track of our links, we link one program written for Client A to Client B's directory. What we want to do is to keep track of our linked programs... (1 Reply)
Discussion started by: Smurtzy
1 Replies
Login or Register to Ask a Question