Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11
Hey all, I want to make a script the will be used in a back up program, i want it to use a if statement to see weather a file ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie
    Join Date
    Dec 2005
    Posts
    104

    Question Script to move files depending on size


    Hey all,
    I want to make a script the will be used in a back up program, i want it to use a if statement to see weather a file is larger than 2gb and move it to a folder so it doesent take up Tape space for the back up...So when the script runs it will go through a folder and move anything that is over 2gb to /backup/exclude/ and all the other files that are smaller than 2gb will be left in the original folder /backup/include

    I have no idea where to start, other than the ls -lhsS command
    Any help?
    Thanks HEAPS
    Nathan

  2. #2
    Linux Newbie
    Join Date
    May 2005
    Location
    Chennai,TamilNadu, India
    Posts
    141
    I hipe you know bash scripting basics like the syntax of the if command and how to do basic comparison.

    Then i can give u a start of how to go through with it

    ls -l | awk '{print int($5)}'`

    Try executing this command in shell within a directory. You will get the size of each file printed.

    You can learn more about the 'awk' command from the net

  3. #3
    Linux Newbie
    Join Date
    Dec 2005
    Posts
    104
    Hey, thanks. Yeah i do know about syntax.
    But how do i set up the loop? A do loop? Untill the end of the folder?

    if ls -l | awk '{print int($5)}'` >2.0gb then
    cp ??????? ???????
    fi


    thanks....
    Would it be like VB reading a text file via line?
    Thanks..
    Nathan

  4. #4
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Code:
    for line in `ls -1`
    do
        size=`awk '{print int($5)}' $line`
        if [$size > 2097152]
        then
            cp $file 2_gig_folder/
        fi
    done
    Something like that perhaps? Note that I don't know Bash very well, so the above may well have errors.

    Anyway, this uses a foreach loop to take each line from `ls -l` and run that loop on it.


    EDIT:

    I always forget about the find command... useful bugger, that.

  5. #5
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    I'd do it like this:
    Code:
    find /backup/include -size +2000000k -exec mv -i {} /backup/exclude \;
    That's a one-liner, so you can easily just run it directly from an interactive shell instead of creating a script.

    If you really want to do it with a script (or just know how it would be done), there are, naturally, a great number of ways to do it, but I'd do it like this:
    Code:
    #!/bin/sh
    
    find /backup/include -printf '%s %p\n' | while read size name; do
        if [ "$size" -gt 2000000000 ]; then
            mv -i "$name" /backup/exclude
        fi
    done
    Last edited by Dolda2000; 12-21-2005 at 12:20 AM.

  6. #6
    Linux Newbie
    Join Date
    Dec 2005
    Posts
    104

    Diddnt work :S

    I want to do it in a script because the backup program will run, then exclude any files larger than the quota, move them to another pleace to exclude them, then tar all the other files to a tape, i have the rest but not the moving part, the one liner works, but not the .sh one...I c/p'd it but no luck, i have talked to me boss to see if he can help, but he is clueless like me...
    Help?
    Thanks

  7. #7
    Linux Newbie
    Join Date
    May 2005
    Location
    Chennai,TamilNadu, India
    Posts
    141
    I also tried the script which dolda has given but i got the output as this for a set of files. I tried for a lesser size just to see . The files were getting copied to the location mentioned but the errors were coming. so i added debug statements to it


    find /usr/macubex -printf '%s %p\n' | while read size name; do
    echo "Size - "$size
    if [ $size -gt 20 ]; then
    echo "moving" "$name"
    mv -i $name /backup/exclude
    fi
    done

    Otuput
    Size - 4096
    moving /usr/macubex
    Size - 1036
    moving /usr/macubex/Test_Binary.o
    mv: cannot stat `/usr/macubex/Test_Binary.o': No such file or directory
    Size - 6636
    moving /usr/macubex/diskconf
    mv: cannot stat `/usr/macubex/diskconf': No such file or directory
    Size - 4666
    moving /usr/macubex/Test_Binary.so
    mv: cannot stat `/usr/macubex/Test_Binary.so': No such file or directory
    Size - 204
    moving /usr/macubex/Test_Binary.c
    mv: cannot stat `/usr/macubex/Test_Binary.c': No such file or directory


    From this i noticed that the "/usr/macubex" is been copied at the first itself as a directory into the folder /backup/exclude

    That is why when it tried for the files after that it says cannot stat the respective file.
    I am also not too familiar with the find command

  8. #8
    Linux Newbie
    Join Date
    May 2005
    Location
    Chennai,TamilNadu, India
    Posts
    141
    Code:
    TEST="/backup/include/file.txt"
    SOURCEFOLDER="/backup/include"
    DESTINATIONFOLDER="/backup/exclude"
    cd $SOURCEFOLDER
    echo `ls -l > $TEST`
    while read cmd_line
    do
    size=`echo $cmd_line | awk '{print int($5)}'`
    filename=`echo $cmd_line | awk '{print $9}'`
    if [ $size -gt 20 ]
    then
    echo $filename - $size
    cp -i $SOURCEFOLDER"/$filename" $DESTINATIONFOLDER
    fi
    done < $TEST


    This is kind of a roundabout method to do it using a for loop. but it will work
    I think i have to look into the "find command and see if i am able to do it that way more easily

  9. #9
    Linux Newbie
    Join Date
    May 2005
    Location
    Chennai,TamilNadu, India
    Posts
    141
    Instead of "cp" use "mv". Ok

  10. #10
    Just Joined!
    Join Date
    Jun 2005
    Location
    Canada, Halifax
    Posts
    86
    Far be it for me to advocate for dolda however his script works great. Not to put words in his mouth, but his script is meant to demonstrate the correct procedure but doesn't provide any error checking (it is up to you to fill in the detals). sharonenoch, you've changed your test file size to so small a size that it is on par with that of the directory. Either provide a copy-if-not-directory test, or use a larger file size and you'll see it (the script as shown) work as intended.

    PS: don't forget the quotes around the name variable in the move statement or "funny" file names are going to give you woes.

Page 1 of 2 1 2 LastLast

Posting Permissions

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