Results 1 to 8 of 8
Hi;
i have a question like this. i want to find old file for 3 days from this time. if this files not *.gz, i'll use gzip command
i added ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 09-16-2011 #1Just Joined!
- Join Date
- Sep 2011
- Posts
- 4
want to find old file
Hi;
i have a question like this. i want to find old file for 3 days from this time. if this files not *.gz, i'll use gzip command
i added my script code, it's my first.
can you replace it
my code find files but every run time gzip this file.
for ex; a.txt and b.txt files old file, run code
output --> a.txt.gz b.txt.gz
and run again
output --> a.txt.gz.gz b.txt.gz.gz
#!/bin/sh
#echo Backup Started $(date)
if [ ! -e /home/LOGS/ZIP ];then
mkdir /home/LOGS/ZIP
fi;
gzip $(find /home/LOGS -type f -mtime -3)
mv /home/LOGS/*.gz /home/LOGS/ZIP/
#rm -f $(find /home/LOGS/ZIP -type f -mtime -30)
#echo Backup Complated $(date)
- 09-16-2011 #2Trusted Penguin
- Join Date
- May 2011
- Posts
- 3,685
I assume you do not want a recursive find here, you only want files in /home/LOGS/, but find is recursive by default (i.e., it will find files in your /home/LOGS/ZIP dir, too). try this:
if you wanted to avoid any zip files that happened to be in /home/LOGS, you could make it this:Code:gzip $(find /home/LOGS -maxdepth 1 -type f -mtime -3)
Code:gzip $(find /home/LOGS -maxdepth 1 -type f -mtime -3 -not -name '*.gz')
- 09-16-2011 #3Just Joined!
- Join Date
- Sep 2011
- Posts
- 4
i'll find files in /home/LOGS/ that's true.
i can't understand -maxdepth also the script have an error.
error is explain usage find command
- 09-16-2011 #4Trusted Penguin
- Join Date
- May 2011
- Posts
- 3,685
Are you using busybox (or some other bastard version of) find? Can you post the command and your error?
Could you could move the /home/LOGS/ZIP dir to say, /home/LOGS-ZIPPED/ to avoid this?
- 09-16-2011 #5Just Joined!
- Join Date
- Sep 2011
- Posts
- 4
sorry i don't know version. i use a unit and there are some differences about command.
i changed gzip command like your code.
an error;
i don't know which path used for -maxdepth.Usage: find [PATH...] [EXPRESSION]
Search for files in a directory hierarchy. The default PATH is
the current directory; default EXPRESSION is '-print'
EXPRESSION may consist of:
-follow Dereference symbolic links.
-name PATTERN File name (leading directories removed) matches PATTERN.
-print Print (default and assumed).
-type X Filetype matches X (where X is one of: f,d,l,b,c,...)
-perm PERMS Permissions match any of (+NNN); all of (-NNN);
or exactly (NNN)
-mtime TIME Modified time is greater than (+N); less than (-N);
or exactly (N) days
-newer FILE Modified time is more recent than FILE's
thanks...
- 09-16-2011 #6Trusted Penguin
- Join Date
- May 2011
- Posts
- 3,685
run this:
then this:Code:ls -l `which find`
Just checked - my busybox version of find can handle the 'maxdepth' arg. are you sure you are using it correctly? You should be passing it a '1' (number one). Can you post the exact find command you are trying?Code:find --version
- 09-16-2011 #7Just Joined!
- Join Date
- Sep 2011
- Posts
- 4
i think, you are right. my version is problem.
i can't run this command but i looked version.
this unit version = Linux version 2.4.18-rmk3
if library software updated, some command would run.
thank you very much for helping...
- 09-16-2011 #8Trusted Penguin
- Join Date
- May 2011
- Posts
- 3,685
here's an alternate solution, using date, ls, and stat (for getting modtime). maybe your stat isn't available or current, either, though!
It also uses bash built-in arithmetic functions (to calculate file age), so that may bork you, too (if you're using a busybox shell w/o such functions, etc.).Code:#!/bin/sh now=`date +%s` maxDays=3 maxSec=$(( $maxDays * 86400 )) list(){ for file in `ls /home/LOGS/*`; do [ -f $file ] || continue echo $file|grep -q \.gz && continue modtime=`stat -c %Y $file` age=$(( $now - $modtime )) # echo -n "File $file is $age seconds old..." if [ $age -gt $maxSec ]; then echo $file # echo more than 3 days old # else # echo still young fi done } oldfiles=`list` if [ -n "$oldfiles" ]; then echo Zipping up old files... for old in $oldfiles; do gzip $old mv -v $old.gz /home/LOGS/ZIP/ done else echo No old files found fi


Reply With Quote
