Discussion on:

12
Comments

Join the conversation!

Follow via:
RSS
Email Alert
0 Votes
+ -
find is one of my favorit commands
DanLM Updated - 11th Sep 2007
You can find files created after a specific date/time. Ie: Age of file since either creation or modification.

You can find files over a specific size. Looking for files that has filled a partition or blew out a user quota all of a sudden? This would be a perfect example of using the find.

You can find directories.(use this alot in the freebsd ports collection when I can't remember where the port is at).

You demonstrated executing specific commands with the find once a file has been found. combining the time with lookup (modification date) and the execution of other commands(rm), you can keep your /tmp files cleaned up.

Like I said, find is one of my favorite commands.

Dan
0 Votes
+ -
Find and Grep?
qqr 11th Sep 2007
Being a bit new to this I probably doing something wrong, this is also on Solaris rather than Linux, but I assume it should be similar.

I was trying to use find and grep the other day to look for a word in all files within a directory and sub directories. But had trouble getting it to work.

Command was grep -li management `find . -type f` > /tmp/latfile.lst

Result is ksh: /bin/grep: arg list too long

Any help would be appreciated, thanks.
0 Votes
+ -
find . -exec grep "www.athabasca" '{}' \; -print

This is an example from here:
http://www.athabascau.ca/html/depts/compserv/webunit/HOWTO/find.htm


In your case, I would try something like:

find . -type f -exec grep "management" '{}' \;

This should search all files in current directory, grep those files for the word management and print them.

Dan
0 Votes
+ -
Get just file names?
qqr 11th Sep 2007
Thanks Dan for taking the time to reply, that worked fine.

I didn't however explain very well what I was looking for.

Rather than returning the actual text with the word searched for, how would I just get the file name that contains the search criteria?

I thought the -name switch with find would give me the answer, but again I am struggling to get the syntax right.
0 Votes
+ -
find . -type f -exec grep -Hc "idiot" '{}' \; | grep -v ":0"

This was performed in a high level script directory where I developed a firewall script called idiotblocker. This is what it listed off.

./CVS/Repository:1
./CVS/Entries:2
./ChangeLog:5
./README:14
./check.txt:3
./idiotblocker.sh:34
./ManualUpdate.sh:5
./FTP-idiotblocker.pl:6
./FTP-idiotblocker.pl-bk20061030:2
./BruteForceCheck.sh:5
./PerlVersion/idiotblocker.pl:8
./PerlVersion/idiotconfig.pl:1
./PerlVersion/test.pl:2
./PerlVersion/idiotblocker_20070822Works.pl:8
./PerlVersion/idiotblockerbkup.pl:4
./ExternalBlacklist.sh:2
./idiotblocker_non.sh:34

What your seeing is the path with a number on the end showing the number of times the verb appears in the file.

What is occurring is.
1). Find is actually listing off all files, but it is showing a numeric at the end showing the number of occurrences of the text specified in the find grep option. Lots of 0 counts appeared.
2). This output is then directed to another grep which via the -v option prints every line from the previous command that does not have shocked

Will this work?

Dan
0 Votes
+ -
OMG
qqr 11th Sep 2007
Now my brain is spinning! The -H option is not valid on this OS. It will list the count fine.

I will have a bit of a play with it over the next day or so. Thanks again for your help.
0 Votes
+ -
find . -type f -exec grep -l management {} \;
Sorry, missed that when I was doing the man last night on the grep.

It's cleaner then what I got too.

Dan
0 Votes
+ -
Thanks Styopa and Dan
qqr 13th Sep 2007
Exactly what I needed.
I use the find command a LOT! The temptation of new Linux users is to use Search via a GUI file manager, which I've found to be painfully slow compared to the find command.

A few tips to add here:

1) Always surround the search text between single quotes (NOT double quotes!).

2) An often overlooked option is -ls as the last option, instead of -print (the default).

Finding files without knowing where they're located is many times useless, so use -ls to see the complete path, the permissions and more. The -ls option gives a long listing; the same as you'd see with the ls -l command.
I recently found good examples of unix find commands , easy to use and explained with clear example. something I have been looking for unix find in last few days. Some of example of find command is bit old but they are still effective.
Keyboard Shortcuts:
Prev
Next
Toggle
Join the conversation
Formatting +
BB Codes - Note: HTML is not supported in forums
  • [b] Bold [/b]
  • [i] Italic [/i]
  • [u] Underline [/u]
  • [s] Strikethrough [/s]
  • [q] "Quote" [/q]
  • [ol][*] 1. Ordered List [/ol]
  • [ul][*] · Unordered List [/ul]
  • [pre] Preformat [/pre]
  • [quote] "Blockquote" [/quote]

Join the TechRepublic Community and join the conversation! Signing-up is free and quick, Do it now, we want to hear your opinion.