+ Reply to Thread
Page 1 of 3 123 LastLast
Results 1 to 10 of 27

Thread: Fun Things To Do With Find

  1. #1
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Fun Things To Do With Find

    Linux has a lot to offer with the find command, which can be used to recursively iterate through subdirectories and files.

    You can execute your own command for each matching file or directory with the -exec argument, which will execute whatever you put between -exec and a terminating semicolon. You'll probably have to escape the semicolon to avoid the shell from interpreting it wrong. If you need to pass the current file that find is working on to your command, use the token '{}' as a placeholder. For example:

    Setting Permissions on Directory Trees
    Code:
    find ~/WWW -type f -exec chmod 755 '{}' \;
    This above command will set the permissions for every file in my WWW directory and its subdirectories to 755 (rwxr-xr-x). Note that because I specify the type as only files (hence the f passed to -type), all directories will be skipped. Changing the f to d will call chmod on all directories but not files. See the man page for other types you can pass to this parameter.

    Now let's say I want to restrict that only to files ending in .htm or .html. Well, I can pass a regular expression or glob, like so:

    Code:
    find ~/WWW -name '*.htm*' -type f -exec chmod 755 '{}' \;
    find ~/WWW -regex '.*\.htm.*' -type f -exec chmod 755 '{}' \;
    Note, however, that the -regex option is performed on the entire file path, whereas -name is only performed on the file name, and only accepts the standard * and ? wildcards.

    Getting a List of Modified Files
    You can specify several different time stamp options to get a list of files modified within the past minute or longer.

    Code:
    #this will print a list of all the files modified
    #in the past week.
    find ./ -mtime 7 -print
    
    #this will print a list of all the files modified
    #in the last hour.
    find ./ -mmin 60 -print
    As you've probably guessed, -mmin takes an argument of minutes, and -mtime takes an argument of days. -print is used to indicate that you want to see the name of each and every file that matches the criteria.

    Backing Up New Files
    You can use this to update a backup archive, like so:

    Code:
    find ./ -mtime 7 -print | tar -uf backup.tar -T -
    Unfortunately if you want to compress it you'll have to decompress it to a .tar (not .tar.gz or whatever), update it, and then recompress it. You can easily write a shell script and stick it in your .bashrc or .cshrc file:

    Code:
    backup() {
        gunzip -c backup.tar.gz > tmpbackup.tar ;
        find ./ -mtime 7 -print | tar -uf tmpbackup.tar -T - ;
        rm -f backup.tar.gz ;
        gzip --best -c tmpbackup.tar > backup.tar.gz ;
        rm -f tmpbackup.tar ;
    }
    Getting Rid of Porn
    This calls shred on every single file in the specified directory and subdirectories, deletes the files, and finally deletes the directory itself. shred, for those of you who don't know, repeatedly overwrites files with garbage data so that recovering the original file is incredibly difficult. (However, this is not guaranteed to work on all types of file systems; see caveat on the shred man page I linked to below.)

    Code:
    obliterate() {
    	find $1 -type f -exec shred -uz '{}' \; -print ;
    	rm -rf $1 ;
    }
    Linux man pages:
    find
    shred
    gzip/gunzip
    tar
    sudo rm -rf /

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Fun Things To Do With Find

    Great tutorial! I use find on the CodeCall server to delete old backups. Works great!
    +rep

  4. #3
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Fun Things To Do With Find

    Oh yeah, I forgot about those annoying backup files that emacs leaves everywhere.

    Code:
    rm -rfv *~
    That should take care of them. DO NOT put a space between the asterisk and the tilde, or you will find yourself with many fewer files than before.
    sudo rm -rf /

  5. #4
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: Fun Things To Do With Find

    I need to reinstall linux as another partition soon
    /me points to my todo list

    Great Tut! +Rep!

    /me writes down how to get rid of porn

  6. #5
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Fun Things To Do With Find

    Thanks for the rep. I'd suggest Ubuntu, though I've never really tried any other distro.
    sudo rm -rf /

  7. #6
    Jordan Guest

    Re: Fun Things To Do With Find

    These really are great tips, do you plan on writing more similar to this?

  8. #7
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Fun Things To Do With Find

    I'm working on another one right now for creating GUIs for shell scripts.
    sudo rm -rf /

  9. #8
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Fun Things To Do With Find

    find is a great tool. Actually, it's a great example of where Linux command-line owns Windows GUI.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  10. #9
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Fun Things To Do With Find

    pcregrep and just plain grep both do as well.
    sudo rm -rf /

  11. #10
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Fun Things To Do With Find

    I actually put linux commands on most Windows boxes I have. grep made my life a LOT easier when I was analyzing a FoxPro program a while back.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

+ Reply to Thread
Page 1 of 3 123 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. .net things I want to do in C++
    By Axel in forum C and C++
    Replies: 5
    Last Post: 08-01-2011, 06:40 PM
  2. Notepad Find,Find Next ,replace,goto Code
    By shyamenk in forum C# Programming
    Replies: 7
    Last Post: 04-06-2011, 08:25 PM
  3. First things
    By Jaan in forum Business and Legal
    Replies: 3
    Last Post: 02-16-2009, 11:03 AM
  4. Find harmony in two relative things
    By Maurice_Z in forum Programming Theory
    Replies: 3
    Last Post: 03-02-2008, 06:03 AM
  5. Help with those 2 things !!!!
    By kresh7 in forum Visual Basic Programming
    Replies: 3
    Last Post: 02-20-2008, 03:45 PM

Tags for this Thread

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