Hello experts,
I used following approach to get listing of all files of remote server.
Now I have remote server file information on same server.
#Defining variables and assigning values
USR='admin'
PASSWD='abc'
HT='xyz.aaa.com '
FILE='S*.pdf
DIRNAME='/doc'
LsFiles()
{
ftp -n -i $HT <<end_ftp
user $USR $PASSWD
cd $DIRNAME
ls "-lrt *.pdf"
bye
end_ftp
}
LsFiles > list_of_files.txt
I am getting listing in the output.txt
I want to count today's created files and old files.
I want to compare the numbers i.e. all files were created today
or there are also some old files in the list.
Could you pl. give me a clue?
Thanks in advance.
Thanks
Try this:
ls --full-time | awk ' BEGIN{ "date \"+%F\"" | getline today } \
{ if($6 == today) ++nFileCreatedToday; else ++nOldFile } \
END { nFileCreatedToday=0; nOldFile=0; print "Number of files created today:", nFileCreatedToday, "\nNumber of old files: ", nOldFile } '
Note: you need to issue "ls --full-time" instead of "ls -lrt" to get the file listing.
Thanks Kevin for quick reply.
ls --full-time | awk ' BEGIN{ "date \"+%F\"" | getline today } \ { if($6 == today) ++nFileCreatedToday; else ++nOldFile } \ END { print "Number of files created today:", nFileCreatedToday, "\nNumber of old Files: " nOldFile } ''
The files are located in remote server.
Do you think above statement will work in FTP connection?
OK, ssh to the remote server and get the file listing.
ssh user@server "ls --full-time /your/dir" | \
awk ' BEGIN{ "date \"+%F\"" | getline today } \
{ if($6 == today) ++nFileCreatedToday; else ++nOldFile } \
END { nFileCreatedToday=0; nOldFile=0; print "Number of files created today:", nFileCreatedToday, "\nNumber of old files: ", nOldFile } '
Kevin Thanks again for quick reply.
I modified the script was per your suggetion.
The directory has 5000 files but still it says
Number of files created today: 0
Number of old files: 0
Ensure
ssh user@server "ls --full-time /modify/this/to/your/directory/"
returns something.
USR='admin'
PASSWD='abc'
HT='xyz.aaa.com'
FILE='S*.pdf
DIRNAME='/doc'
ssh $USR@$HT "ls --full-time /doc" | \
awk ' BEGIN{ "date \"+%F\"" | getline today } \
{ if($6 == today) ++nFileCreatedToday; else ++nOldFile } \
END { nFileCreatedToday=0; nOldFile=0; print "Number of files created today:", nFileCreatedToday, "\nNumber of old files: ", nOldFile } '
Returns
Number of files created today: 0
Number of old files: 0
Test the following command and ensure it returns the file listing.
I tested this on RHEL, it worked as expected.
ssh user@server "ls --full-time /home/"
Thanks Kevin for reply!!
I tried:
ssh admin@xyz.aaa.com "ls --full-time /doc/"
That works fine but when
ssh admin@xyz.aaa.com "ls --full-time /doc/" | \
awk ' BEGIN{ "date \"+%F\"" | getline today } \
{ if($6 == today) ++nFileCreatedToday; else ++nOldFile } \
END { nFileCreatedToday=0; nOldFile=0; print "Number of files created today:", nFileCreatedToday, "\nNumber of old files: ", nOldFile } '
Returns
Number of files created today:0
Number of old files: 0
What platform is your remote server using? "ls --full-time" might produce something different on some UNIX systems, am not sure.
As on RHEL, I got
xxx.xxx.xxx.xxx [kevin ~]$ ls --full-time
total 5992
-rw-r--r-- 1 kevin kevin 37 2010-06-04 09:06:37.000000000 +0800 abc2.txt
-rw-r--r-- 1 kevin kevin 38 2010-06-04 09:06:18.000000000 +0800 abc.txt
....
You can see that 6th column of the listing is the create date of the file, so you need to compare this column to the "today" variable in the AWK script I offered. It might not be the 6th column in your case, but I am sure you are able to modify the script to make it work.
dipeshvshah:
USR='admin'
PASSWD='abc'
HT='xyz.aaa.com'
FILE='S*.pdf
DIRNAME='/doc'
ssh $USR@$HT "ls --full-time /doc" | \
awk ' BEGIN{ "date \"+%F\"" | getline today } \
{ if($6 == today) ++nFileCreatedToday; else ++nOldFile } \
END { nFileCreatedToday=0; nOldFile=0; print "Number of files created today:", nFileCreatedToday, "\nNumber of old files: ", nOldFile } '
Returns
Number of files created today: 0
Number of old files: 0
Of course always get 0, because above red part. It reset the value to 0 in END sessions. They should be put in BEGIN session.
From your output file list_of_files.txt, use this :
awk '
BEGIN{ "date \"+%b %d\"" | getline today ;nFileCreatedToday=0; nOldFile=0;}
{ if("$6 FS %7" == today) ++nFileCreatedToday; else ++nOldFile}
END { print "Number of files created today:", nFileCreatedToday, "\nNumber of old files: ", nOldFile }
' list_of_files.txt
Number of files created today: 0
Number of old files: 7
rdcwayx, Thank you.
It was my fault, I didn't test the script after adding "nFileCreatedToday=0; nOldFile=0;" to the script.