5 Days of find: The Basics of find
bashshell.net
bashshell.net
find -name syslog.conf
otherwise find will not look for that filename in all subdirectories.So the shown
find /etc/*.conf
is only doing the equivalent of ls /etc/*.conf
unless there is a directory the name of which ends in ".conf", in which case all its contents will be listed recursively. The required find command is: find /etc -name \*.conf
And what "find / syslog.conf" is doing is to list all files and directories in /, going down subdirectories recursively, and list syslog.conf in the current directory. What should really be used is: find / -name syslog.conf
which returns something like: /etc/syslog.confFor example, if you're in a directory that contains a file named "foo.bar", and you try to search for files ending in .bar, then doing a:
find . -name *.bar
may return just "./foo.bar" as the shell expanded *.bar to foo.bar, and passed that to find.To fix, wrap arguments you pass to find in quotes if they contain wildcard characters.
(edit: fixed formatting as HN uses asteriks to italicize)
find . -name \*.bar
Also passes the unexpanded argument "*.bar" to find.du -a | grep -y "$1" | grep -y "$2" | awk '{print $2}'
I am planning to rewrite to handle more than two search terms, but it works for me good enough for now.
Your du command is:
du -a | grep -y "$1" | grep -y "$2" | awk '{print $2}'
The find command I would use for the same effect is: find -iregex ".*$1.*" -a -iregex ".*$2.*"
How does the du command do a better job than the find command?Maybe I just wasn't using find right, but several introductory linux books have also claimed it is hard to use. Before I found the du -a | grep idea, I was using one I read somewhere else of grepping a directory listing (made by ls -lR ~/ > dir) rather than use find.
find [path...] [expression]
the current directory is used. I don't know if it's just that way for GNU find, or if it's standard Posix find behaviour.Regarding the wildcards in search statements, I usually just need to match a case-sensitive substring, so I use something like:
find -name '*sub*'
I don't know of any weird wildcard reactions, but that may be because I don't usually use complex searches or matches.One last thing, if you don't mind all the "./" prefixes, "find ." (or just "find") is just like "du -a" without the block counts.