Find the answer to your Linux question:
Results 1 to 9 of 9
Hello, I'm writing a script to remove files from a directory more than 30 days old, matching a certain pattern (*name*) without removing from subvols. I am far enough that ...
  1. #1
    Just Joined!
    Join Date
    Jul 2007
    Posts
    3

    Removing old fies from a directory

    Hello, I'm writing a script to remove files from a directory more than 30 days old, matching a certain pattern (*name*) without removing from subvols.

    I am far enough that it finds the old files by name (using find) and writes them to a file. When I loop through the file and read it, it executes every line instead of performing a move of it.

    Any ideas?

    Thanks
    Geoff


    Here is the read and copy section of my code. Note: once this works it will be changed to a move

    # Read logfile, get file basename (no dir) and move to output file loc
    while read -u3 oldfile
    do
    oldbase=basename $oldfile
    cp $oldfile $outdir/$oldbase
    done 3<oldpaths.log

  2. #2
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    You can move files older than 30 days with one command:

    Code:
    find / -name whatever -ctime +30 -exec mv {} /newdir/ \;
    So there's really no need to do file i/o, unless you need a log.

    And if by old, you mean modification time change ctime to mtime. Or atime for access.

  3. #3
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    That might not be what you're looking for but I am a little unclear as to what you mean by "subvols."

  4. #4
    Just Joined!
    Join Date
    Jul 2007
    Posts
    3
    I don't want the find to be recursive. I only want it to move files in the directory I specify.

    Thanks,
    Geoff

  5. #5
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    Code:
    find / -name whatever -maxdepth 1 -ctime +30 -exec mv {} /newdir/ \;
    I think that should do it.

  6. #6
    Just Joined!
    Join Date
    Jul 2007
    Posts
    3
    The version of find I have doesn't support the -maxdepth arg. it also doesn't have -path.

  7. #7
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    Then troll through the man pages. I am using GNU find.

  8. #8
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    Then try -prune:

    Code:
    find * -prune -type f -name <whatever> -ctime +30 -exec mv {} /newdir/ \;

  9. #9
    Just Joined!
    Join Date
    Jul 2007
    Posts
    41
    I think your script would work if you changed

    Code:
      oldbase=basename $oldfile
    to

    Code:
      oldbase=$(basename $oldfile)
    The first way will create an environment in which oldbase=basename then run the file at the path stored in $oldfile, like the env command.

    The second way will execute basename on oldfile and store the result in oldbase.

Posting Permissions

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