| 1 | #!/bin/bash |
| 2 | # symlinks.sh: Lists symbolic links in a directory. |
| 3 | |
| 4 | |
| 5 | directory=${1-`pwd`} |
| 6 | # Defaults to current working directory, |
| 7 | #+ if not otherwise specified. |
| 8 | # Equivalent to code block below. |
| 9 | # ---------------------------------------------------------- |
| 10 | # ARGS=1 # Expect one command-line argument. |
| 11 | # |
| 12 | # if [ $# -ne "$ARGS" ] # If not 1 arg... |
| 13 | # then |
| 14 | # directory=`pwd` # current working directory |
| 15 | # else |
| 16 | # directory=$1 |
| 17 | # fi |
| 18 | # ---------------------------------------------------------- |
| 19 | |
| 20 | echo "symbolic links in directory \"$directory\"" |
| 21 | |
| 22 | for file in "$( find $directory -type l )" # -type l = symbolic links |
| 23 | do |
| 24 | echo "$file" |
| 25 | done | sort # Otherwise file list is unsorted. |
| 26 | # Strictly speaking, a loop isn't really necessary here, |
| 27 | #+ since the output of the "find" command is expanded into a single word. |
| 28 | # However, it's easy to understand and illustrative this way. |
| 29 | |
| 30 | # As Dominik 'Aeneas' Schnitzer points out, |
| 31 | #+ failing to quote $( find $directory -type l ) |
| 32 | #+ will choke on filenames with embedded whitespace. |
| 33 | # containing whitespace. |
| 34 | |
| 35 | exit 0 |
| 36 | |
| 37 | |
| 38 | # -------------------------------------------------------- |
| 39 | # Jean Helou proposes the following alternative: |
| 40 | |
| 41 | echo "symbolic links in directory \"$directory\"" |
| 42 | # Backup of the current IFS. One can never be too cautious. |
| 43 | OLDIFS=$IFS |
| 44 | IFS=: |
| 45 | |
| 46 | for file in $(find $directory -type l -printf "%p$IFS") |
| 47 | do # ^^^^^^^^^^^^^^^^ |
| 48 | echo "$file" |
| 49 | done|sort |
| 50 | |
| 51 | # And, James "Mike" Conley suggests modifying Helou's code thusly: |
| 52 | |
| 53 | OLDIFS=$IFS |
| 54 | IFS='' # Null IFS means no word breaks |
| 55 | for file in $( find $directory -type l ) |
| 56 | do |
| 57 | echo $file |
| 58 | done | sort |
| 59 | |
| 60 | # This works in the "pathological" case of a directory name having |
| 61 | #+ an embedded colon. |
| 62 | # "This also fixes the pathological case of the directory name having |
| 63 | #+ a colon (or space in earlier example) as well." |
| 64 |