Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
backup_server has directory AAA. I need to check from application server
that is there any new files created today in the AAA dirctory. if yes, all files were created today or partial files?.
I am not expert in scripting. Please suggest me the way.
You can use two tests in you find statment to find files created in last 24 hours and files more than 5 minutes old which might filter out files still being written to. (Depending on the file size)
For currently open files, look at using the lsof command. To track files being created, look at the famd daemon.
You will need to provide more information on what you mean by partial files, such as what kinds of files you are talking about (can integrity be tested ). How they were created, what log files if any will indicate a failure ( e.g. ftp logs )
To find all the files in AAA directory that were created today:
Code:
find "<path to AAA directory>" -type f -daystart -not -ctime +0
To find all the files in AAA directory that were not created today:
Code:
find "<path to AAA directory>" -type f -daystart -ctime +0
To count the results from a directory search, just add '| wc -l':
Code:
find ... | wc -l
Putting everything inside a script:
Code:
#!/bin/bash
AAA="<path to AAA directory>"
declare -i TODAYCOUNT NOTTODAYCOUNT
TODAYCOUNT=$(find "$AAA" -type f -daystart -not -ctime +0 | wc -l)
if [[ TODAYCOUNT -eq 0 ]]; then
echo "No file was created today."
else
NOTTODAYCOUNT=$(find "$AAA" -type f -daystart -ctime +0 | wc -l)
if [[ NOTTODAYCOUNT -eq 0 ]]; then
echo "There are new files in $AAA and all of them were created today ($TODAYCOUNT)."
else
echo "Some of the files in $AAA were created today ($TODAYCOUNT)."
fi
fi
or
Code:
#!/bin/bash
AAA="<path to AAA directory>"
declare -a TODAY
declare -i TODAYCOUNT=0 NOTTODAYCOUNT I
while read; do
TODAY[TODAYCOUNT++]=$REPLY
done < <(find "$AAA" -type f -daystart -not -ctime +0 | wc -l)
if [[ TODAYCOUNT -eq 0 ]]; then
echo "No file was created today."
else
NOTTODAYCOUNT=$(find "$AAA" -type f -daystart -ctime +0 | wc -l)
if [[ NOTTODAYCOUNT -eq 0 ]]; then
echo "There are new files in $AAA and all of them were created today ($TODAYCOUNT)."
else
echo "Some of the files in $AAA were created today ($TODAYCOUNT)."
fi
echo --------------------
for (( I = 0; I < TODAYCOUNT; ++I )); do # for bash 2.05+
echo "${TODAY[I]}"
done
echo --------------------
fi
I was already done with post I just made and was about to make the post when I read your new post in preview so please don't mind the scripts. I think they're not easy to apply if you're using ftp.
It says invalid command. I believe because its accessing remote server.
Can someone modify the code to make it work. I also tried 'find' same way but it says
usage: ls remote-directory local-file
Thanks,
dip
Yes...YOU can modify the code to make it work. This is your job...why should we do it for you, when you just want to take the lazy way out and give up? We don't know your environment, or what you really need this to do. There are thousands of scripting tutorials available through Google...you can easily find one.
Also, if you're going to use FTP, try either using an Expect script, or using SCP/SFTP with a key swap, so it won't prompt you for passwords.
dipeshvshah, Are you after actually knowing if the files were updated or are you more specifically just after updating all new and all updated files? if so I would recommend the rsync utility as this will do most of this on your behalf tho I do not know if it can be used to create a report as I myself never use rsync but I would presume it would be possible (as I back-up whole 30~50GB VM images as opposed to single files).
You could use the find command locally to produce a list of filenames that meet your criteria. You don't use find inside an ftp script. In your ftp script you generated, you are including shell script commands. An ftp script can only include ftp commands.
You could run find on the remote server via ssh to return a list of files on the remote server matching certain criteria.
One technique to consider is to use find's -printf command to generate the parts of the ftp script where you put the files.
find /<dir>/ -type f -cmin +5 -ctime -1 -printf "put %p\n" >>sendfiles.ftp
echo "bye" >>sendfiles.ftp
Assemble the ftp script and then run it like
ftp <sendfiles.ftp >logfile
Be sure to read the info file for find. You can return the names of files and ignore directories, for example.
----
Using rsync or tar incremental dumps may be more suitable for what you want to do. You need ssh access to do this.
Are these options listed in the remote servers `ls' command options? If not you are still injecting shell commands inside an ftp script.
If not, I think it would be better (if you have access) to ssh into the remote server so you can run the find command. As already mentioned using sftp as well would be more secure.
---
You can match the date "Aug 17" in this case, and count the number of occurrences using awk.
or use sed or grep as before & wc to count the number of matches.
TODAY=$( date '+%d %b' )
newfiles=$( sed -n "/$TODAY/p" list_of_files.txt | wc -l )
or
newfiles=$( grep -c "$TODAY" list_of_files.txt)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.