Jump to content

Fun Things To Do With Find

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
26 replies to this topic

#1
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,721 posts
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
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:

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.

#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:

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:

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.)

obliterate() {
	find $1 -type f -exec shred -uz '{}' \; -print ;
	rm -rf $1 ;
}

Linux man pages:
find
shred
gzip/gunzip
tar
sudo rm -rf /

#2
Guest_Jordan_*

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

#3
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,721 posts
Oh yeah, I forgot about those annoying backup files that emacs leaves everywhere.

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 /

#4
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
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

#5
dargueta

dargueta

    Writes binary right handed and hex left handed

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

#6
Guest_Jordan_*

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

#7
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,721 posts
I'm working on another one right now for creating GUIs for shell scripts.
sudo rm -rf /

#8
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
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

#9
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,721 posts
pcregrep and just plain grep both do as well.
sudo rm -rf /

#10
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
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

#11
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,721 posts
It helps me a lot at work too, when I have to find a line of source code in hundreds of files.
sudo rm -rf /

#12
Guest_Jordan_*

Guest_Jordan_*
  • Guests

dargueta said:

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


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.

Also:


find ./ -name "*~" | xargs rm


OR


rm `find ./ -name "*~"`