Rev 4 | Go to most recent revision | Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 3 | ls | 1 | #!/usr/bin/env bash |
| 2 | # progname : filefind.bash |
||
| 3 | # purpose : display a directory tree |
||
| 4 | # similar to find |
||
| 5 | |||
| 6 | # ============================================================================= |
||
| 7 | # Functions |
||
| 8 | # ============================================================================= |
||
| 9 | |||
| 10 | Usage() |
||
| 11 | { |
||
| 12 | mesg="$1" |
||
| 13 | |||
| 14 | echo "Usage : $progname directory" |
||
| 15 | echo $mesg |
||
| 16 | } |
||
| 17 | |||
| 18 | # |
||
| 19 | # we need local vars to keep the value when called recursively |
||
| 20 | # |
||
| 21 | traverse() |
||
| 22 | { |
||
| 23 | local directory="$1" |
||
| 24 | |||
| 25 | local curdir curdir2 |
||
| 26 | |||
| 27 | curdir=$(pwd) |
||
| 28 | cd -- "$directory" |
||
| 29 | curdir2=$(pwd) |
||
| 30 | files=$(ls) |
||
| 31 | for file in $files |
||
| 32 | do |
||
| 33 | pfile=$curdir2/$file # file including path |
||
| 34 | if [ -d "$file" -a ! -L "$file" ] |
||
| 35 | then |
||
| 36 | echo $pfile |
||
| 37 | traverse "$file" |
||
| 38 | else |
||
| 39 | if [ -f $file -o -L $file ] |
||
| 40 | then |
||
| 41 | echo "$pfile" |
||
| 42 | else |
||
| 43 | echo "WARNING: Could not process file $pfile" |
||
| 44 | fi |
||
| 45 | fi |
||
| 46 | done |
||
| 47 | cd -- "$curdir" |
||
| 48 | } |
||
| 49 | |||
| 50 | start_traverse() |
||
| 51 | { |
||
| 52 | directory=$1 |
||
| 53 | |||
| 54 | echo $directory |
||
| 55 | traverse $directory |
||
| 56 | } |
||
| 57 | |||
| 58 | # ============================================================================= |
||
| 59 | # MAIN |
||
| 60 | # ============================================================================= |
||
| 61 | |||
| 62 | progname=$(basename $0) |
||
| 63 | if [ $# -eq 1 ] |
||
| 64 | then |
||
| 65 | directory=$1 |
||
| 66 | if [ -d $directory ] |
||
| 67 | then |
||
| 68 | start_traverse $directory |
||
| 69 | else |
||
| 70 | Usage "ERROR: No directory $directory found" |
||
| 71 | fi |
||
| 72 | else |
||
| 73 | Usage "ERRROR: 1 argument expected" |
||
| 74 | fi |