How do we use perl to list out all the files in a directory and subdirectories? Answer: Here is a routine that'll recurse a given directory (perl script.pl /path/to/search/). Code: #!/usr/bin/perl -w use strict; sub recurse($) {
## append a trailing / if it's not there
## print the directory being searched
## loop through the files contained in the directory
## if the file is a directory
## print the file ... tabbed
for readability
## initial call ... $ARGV[0] is the first
command line argument
Note: What does my($path) = @_; do? @_ is a special variable that holds the arguments of a subroutine. This is why you don't see this in perl: sub newSubroutine($arg1,$arg2); Instead you can do the following: Code: #!/usr/bin/perl -w use strict; sub newSubroutine {
newSubroutine('some','random','number','of','arguments'); So it's very similar to $_ only it's used with lists.
Have a Linux Problem
Linux Books
Linux Home: Linux System Administration Hints and Tips (c) www.gotothings.com All material on this site is Copyright.
|