On May 31, 2005 04:24 am, Gerald Humphreys wrote:
> Good day everyone.
>
> Please can someone show me an example of looking if there are any files
> and/or sub directorys in a directory, using a script. #!/bin/sh
>
> Regards
> Gerald Humphreys
for traversing directories your friend is the find program. Its syntax sucks
though! Here are some examples.
# how to find directories. find's first argument is the directory to traverse
# -type is a command which can restrict the search for various types of files.
find path -type d
# how to find ordinary files
find path -type f
# count lines of code in a C program. -name is a command that specifies a
pattern for the files to search for. the special pattern {} which is
substituted for each file that matches find;s search. Here I am using cat to
get each line into the input side of the pipe.
find path -type f -name '*.[ch]' -exec cat {} \; | wc -l
# substitute for grep -R. Here I am executing grep for each ordinary file in
path. /dev/null is used so that grep will report the file that matches. the
'\;' argument is the flag that signals the end of the arguments for the -exec
command.
find path -type f -exec grep -n pattern {} /dev/null \;
Hope that helps.
_______________________________________________
clug-talk mailing list
[email protected]
http://clug.ca/mailman/listinfo/clug-talk_clug.ca
Mailing List Guidelines (http://clug.ca/ml_guidelines.php)
**Please remove these lines when replying