diff options
Diffstat (limited to '')
| -rwxr-xr-x | sel | 37 |
1 files changed, 28 insertions, 9 deletions
| @@ -1,6 +1,9 @@ | |||
| 1 | #!/bin/sh | 1 | #!/bin/sh |
| 2 | 2 | ||
| 3 | # Allow bulk operations on a list of selected files. | 3 | # Allow bulk operations on a list of selected files. |
| 4 | |||
| 5 | # Requires: dmenu (or similar), trash (optional), open-file (for open only) | ||
| 6 | |||
| 4 | # Usage: sel [-m menu] [command] | 7 | # Usage: sel [-m menu] [command] |
| 5 | # If no command is specified, shows the list of selected files | 8 | # If no command is specified, shows the list of selected files |
| 6 | # Commands: | 9 | # Commands: |
| @@ -13,8 +16,6 @@ | |||
| 13 | # open: open files using open-file | 16 | # open: open files using open-file |
| 14 | # rm: remove selected files and clear selection | 17 | # rm: remove selected files and clear selection |
| 15 | 18 | ||
| 16 | # Requires: dmenu (or similar), trash (optional), open-file (for open only) | ||
| 17 | |||
| 18 | # TODO: The usage of paste(1) is a bit of a hack, and for example it does | 19 | # TODO: The usage of paste(1) is a bit of a hack, and for example it does |
| 19 | # not work if filenames contain tab characters. Fix this. | 20 | # not work if filenames contain tab characters. Fix this. |
| 20 | 21 | ||
| @@ -26,14 +27,32 @@ menu="" # default uses dmenu-filepicker default | |||
| 26 | editor=${EDITOR:-vi} | 27 | editor=${EDITOR:-vi} |
| 27 | rm="trash rm" # replace with rm -r if you don't use trash | 28 | rm="trash rm" # replace with rm -r if you don't use trash |
| 28 | 29 | ||
| 30 | usage() { | ||
| 31 | echo "Usage: sel [-m MENU] [COMMAND]" | ||
| 32 | echo "If no COMMAND is specified, shows the list of selected files" | ||
| 33 | echo "Possible commands:" | ||
| 34 | echo " add [files...]: add files to selection" | ||
| 35 | echo " addall: add all files in the current folder to selection" | ||
| 36 | echo " clear: clear selection" | ||
| 37 | echo " cp: copy to current dir, possbily after editing filenames" | ||
| 38 | echo " edit: open selection in editor" | ||
| 39 | echo " mv: move to current dir, possbily after editing filenames" | ||
| 40 | echo " open: open files using open-file" | ||
| 41 | echo " rm: remove selected files and clear selection" | ||
| 42 | } | ||
| 43 | |||
| 29 | while getopts "m:" opt; do | 44 | while getopts "m:" opt; do |
| 30 | case "$opt" in | 45 | case "$opt" in |
| 31 | m) | 46 | m) |
| 32 | menu="$OPTARG" | 47 | menu="$OPTARG" |
| 33 | ;; | 48 | ;; |
| 49 | *) | ||
| 50 | usage | ||
| 51 | exit 1 | ||
| 52 | ;; | ||
| 34 | esac | 53 | esac |
| 35 | done | 54 | done |
| 36 | shift `expr $OPTIND - 1` | 55 | shift $((OPTIND - 1)) |
| 37 | 56 | ||
| 38 | add() { | 57 | add() { |
| 39 | shift 1 | 58 | shift 1 |
| @@ -57,12 +76,12 @@ cphere() { | |||
| 57 | file2=$(mktemp) | 76 | file2=$(mktemp) |
| 58 | sed 's/^.*\///' "$file" > "$file2" | 77 | sed 's/^.*\///' "$file" > "$file2" |
| 59 | $editor "$file2" | 78 | $editor "$file2" |
| 60 | if [ "$(wc -l $file | awk '{print $1}')" != \ | 79 | if [ "$(wc -l "$file" | awk '{print $1}')" != \ |
| 61 | "$(wc -l $file2 | awk '{print $1}')" ]; then | 80 | "$(wc -l "$file2" | awk '{print $1}')" ]; then |
| 62 | echo "Error reading new file names" | 81 | echo "Error reading new file names" |
| 63 | return 1 | 82 | return 1 |
| 64 | else | 83 | else |
| 65 | paste "$file" "$file2" | while read f; do | 84 | paste "$file" "$file2" | while read -r f; do |
| 66 | fold=$(echo "$f" | sed 's/ .*//') | 85 | fold=$(echo "$f" | sed 's/ .*//') |
| 67 | fnew=$(echo "$f" | sed 's/.* //') | 86 | fnew=$(echo "$f" | sed 's/.* //') |
| 68 | cp -R "$fold" ./"$fnew" || return 1 | 87 | cp -R "$fold" ./"$fnew" || return 1 |
| @@ -71,13 +90,13 @@ cphere() { | |||
| 71 | } | 90 | } |
| 72 | 91 | ||
| 73 | open() { | 92 | open() { |
| 74 | while read f; do | 93 | while read -r f; do |
| 75 | open-file "$f" | 94 | open-file "$f" |
| 76 | done < "$file" | 95 | done < "$file" |
| 77 | } | 96 | } |
| 78 | 97 | ||
| 79 | remove() { | 98 | remove() { |
| 80 | while read f; do | 99 | while read -r f; do |
| 81 | $rm "$f" | 100 | $rm "$f" |
| 82 | done < "$file" | 101 | done < "$file" |
| 83 | } | 102 | } |
| @@ -90,7 +109,7 @@ case "$1" in | |||
| 90 | add "$@" | 109 | add "$@" |
| 91 | ;; | 110 | ;; |
| 92 | addall) | 111 | addall) |
| 93 | add * | 112 | add ./* |
| 94 | ;; | 113 | ;; |
| 95 | clear) | 114 | clear) |
| 96 | clear | 115 | clear |
