Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: bash script to delete files

  1. #1
    Mentor
    Join Date
    Jun 2001
    Posts
    1,672

    bash script to delete files

    I need a bash script that will delete all files that are less than 1MB in the /home/user folder. Can someone help me out?


  2. #2
    Guest

    Re: bash script to delete files

    Can I suggest either Ruby or Python? *this will be much easier.

    Pseudo-code:

    Code:
    Get all the files of /home/blah in a list/array
    Remove '.' and '..' from that list
    Scan every file in that list (for i in list)
     *If file size is below 1024 kb then remove file
    And since both Python and Ruby got functions to get a while dir into a list and to get a file's size and to remove a file, this could be like 15 lines of Python maximum

  3. #3
    Mentor
    Join Date
    Jun 2001
    Posts
    1,672

    Re: bash script to delete files

    I could definetly do this in Perl, but I figured it would be a lot simpler in bash, I just don't have any experience with it.

  4. #4
    Guest

    Re: bash script to delete files

    Give me 30 minutes cassette keeping dude.

  5. #5
    Mentor
    Join Date
    Jun 2001
    Posts
    1,672

    Re: bash script to delete files

    Give me 30 minutes cassette keeping dude.
    ???

  6. #6
    Guest

    Re: bash script to delete files

    Code:
    #!/usr/bin/env ruby
    
    begin
     d = Dir.new(ARGV[0])
    rescue
     puts "Invalid directory"
     exit 1
    end
    
    d.each do |f|
     next if (f == ".") or (f == "..")
    
     if File.size(ARGV[0] + "/" + f) < 1048576
      begin
       File.open(ARGV[0] + f, "w+")
      rescue
       File.close(f)
       next
      end
    
      File.delete(ARGV[0] + "/" + f)
      
     end
    end
    Python version coming later tonight

  7. #7
    Guest

    Re: bash script to delete files

    Python version:

    Code:
    #!/usr/bin/env python
    
    import os, sys
    
    try:
     files = os.listdir(sys.argv[1])
    except OSError:
     print sys.argv[1], ": no such directory"
     sys.exit(1)
    
    for f in files:
     size = (os.lstat(sys.argv[1] + "/" + f))[6] # The 7th element is the file size
    
     if size < 1048576:
      try:
       os.remove(sys.argv[1] + '/' + f)
      except OSError:
       continue

    These were done kindda fast, so report any bug you may find! Enjoy and I hope this will help you.

  8. #8
    Guest

    Re: bash script to delete files

    And I noticed it took 29 minutes to give you the 2 versions ;D Kindda good if you ask me

  9. #9

    Re: bash script to delete files

    you do not even really need a script, find and rm can do it just fine.

    Code:
    find /home/user -size -1024k -maxdepth 1 -type f -exec rm {} \;

  10. #10
    Guest

    Re: bash script to delete files

    you do not even really need a script, find and rm can do it just fine.

    Code:
    find /home/user -size -1024k -maxdepth 1 -type f -exec rm {} \;
    Pffff...You didn't learn that the file stats are in a tuple in Python, you didn't learn the name of the exception, etc. *Plus, mine is faster to run:

    ./clover.py /home/vince/

    But find and rm are clever And people who say that Windows "Search file" facility is powerful have NO idea what they are talking about.

Similar Threads

  1. Help with bash script
    By woodson2 in forum Linux - Software, Applications & Programming
    Replies: 5
    Last Post: 12-30-2010, 07:19 PM
  2. Winnt\temp files will not delete
    By ronwolff2001 in forum Windows - General Topics
    Replies: 1
    Last Post: 06-22-2006, 07:28 PM
  3. Bash script
    By linuxuser in forum Programming
    Replies: 1
    Last Post: 09-01-2005, 03:21 PM
  4. Strange problem: Cannot delete files in Linux w/ Win98
    By patpat1212 in forum Linux - Hardware, Networking & Security
    Replies: 1
    Last Post: 03-30-2005, 06:04 AM
  5. Help with bash script
    By tolstoy in forum Programming
    Replies: 5
    Last Post: 05-21-2002, 11:17 AM

Bookmarks

Posting Permissions

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