Find the answer to your Linux question:
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.
  1. #1
    Just 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)

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    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:

    Code:
    gzip $(find /home/LOGS -maxdepth 1 -type f -mtime -3)
    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 -not -name '*.gz')

  3. #3
    Just 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

  4. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    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?

  5. #5
    Just 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;
    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
    i don't know which path used for -maxdepth.

    thanks...

  6. #6
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    run this:
    Code:
     ls -l `which find`
    then this:
    Code:
    find --version
    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?

  7. #7
    Just 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...

  8. #8
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    here's an alternate solution, using date, ls, and stat (for getting modtime). maybe your stat isn't available or current, either, though!

    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
    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.).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •