Find the answer to your Linux question:
Results 1 to 6 of 6
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Removing File By Size


    Hi, I would like a command that will remove a file that is below, say 100kb. Thank you. I think I have to use the find command but im new to that. Thanks.

    Here is what I have so far:
    Code:
    find . -size 50k -exec rm "{}" \;
    This will delete any file that is 50kb exactly. I need to delete any file that is 50kb or smaller. Should I create a loop that walks through every kb (1.00 - 50.00) and deletes them? This seems like it would take forever since I would have to loop through two decimal places as well.

  2. #2
    Linux Engineer
    Join Date
    Jan 2005
    Location
    Chicago (USA)
    Posts
    1,028
    This sounds like a homework question...

    I'll give you some hints: ls -l and cut.

  3. #3
    Actually its not a homework question.

    Heres what I came up with:

    Code:
    #!/bin/bash
    
    current_file=0
    file_max=40
    
    cd test
    
    let "file_max += 1" #Adds one more because it checks for 0kb files as well.
    
    while [ "$current_file" != "$file_max" ]
    do
      echo "$current_file " 
    
      find . -size $current_file -exec rm "{}" \;
    
      let "current_file += 1"
    
    
    done
    
    echo
    
    exit 0

  4. $spacer_open
    $spacer_close
  5. #4
    BUMP

    My above code works for files that are exactly 1.0 2.0 3.0 4.0 5.0kb, but not for 1.1 1.2. 1.3, etc. How can I fix that?

    Ugh all that looping was useless. find . -size -45 -exec rm "{}" \; works just as well (Notice the - before 45.) But I still can figure out how to get files that have decimal places.

  6. #5
    Linux Newbie
    Join Date
    Jul 2005
    Location
    Turn Around
    Posts
    202
    1.) Is this homework?
    2.) Bash will only work with integers.
    3.) Your script might want to use < <= >= or > for your purpose instead of != in your while loop.

  7. #6
    1) No, I am not trying to have you guys do my homework. I am in High School, and most High Schools I know dont offer a Linux class.

    2) Good to know.

    3) Yes well I found that "find . -size -45 -exec rm "{}" \;" works just as well without all the useless looping. The thing is, it dosent work exactly how I want it. If you look in the man pages of find, it states that the -size option dosent search for indirect blocks. Meaning, it will only find file sizes that are exact. (E.G. It will find 5.00kb but not 5.01kb.)

    How can I remedy this?

Posting Permissions

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