我需要管理我的服务器的maildir消息。
我需要知道如何有效地重命名单个消息文件已经[R] ead,反之亦然,最好是使用标准bash。
1408429273.V825I1cce0a1M366263.server0:2,S
1408500713.V825I1ccddaaM341812.server0:2,S
1408502356.V825I1ccddf3M195155.server0:2,S
1408502501.V825I1cce048M253486.server0:2,RS
如果一个文件已被标记为[R] ead,我不想再次绊倒它,或者更糟糕的是,重命名它。
答案 0 :(得分:2)
尝试:
#!/bin/bash
shopt -s nullglob
for F in /path/to/files/*:*,*; do
CODE=${F##*,}
[[ $CODE != *R* ]] && echo mv "$F" "${F%,*},R${CODE}"
done
在确认其正常工作后删除echo
。
更严格:
#!/bin/bash
shopt -s nullglob
shopt -s extglob
for F in /path/to/files/*:+([0-9]),*([A-Z]); do
CODE=${F##*,}
[[ $CODE != *R* ]] && echo mv "$F" "${F%,*},R${CODE}"
done
要重命名为不读:
#!/bin/bash
shopt -s nullglob
shopt -s extglob
for F in /path/to/files/*:+([0-9]),*([A-Z]); do
CODE=${F##*,}
[[ $CODE == *R* ]] && echo mv "$F" "${F%,*},${CODE//R}"
done
脚本:
#!/bin/bash
shopt -s extglob
CHANGE_MODE=''
FILES=()
function show_usage_and_exit {
echo "Usage: $0 option [--] file [file2 ...]" >&2
echo "Options:" >&2
echo " -r, --read Add read flag to filenames." >&2
echo " -u, --unread Remove read flag from filenames." >&2
exit 1
}
while [[ $# -gt 0 ]]; do
case "$1" in
-r|--read)
CHANGE_MODE='R'
shift
;;
-u|--unread)
CHANGE_MODE='U'
shift
;;
-h|--help)
show_usage_and_exit
;;
--)
shift
FILES+=("$@")
break
;;
-*)
echo "Invalid option: $1" >&2
show_usage_and_exit
;;
*)
FILES+=("$1")
;;
esac
shift
done
if [[ -z $CHANGE_MODE ]]; then
echo "No mode specified."
show_usage_and_exit
elif [[ ${#FILES[@]} -eq 0 ]]; then
echo "No file was specified."
show_usage_and_exit
else
for F in "${FILES[@]}"; do
if [[ ! -f $F ]]; then
echo "File does not exist or is not a file: $F" >&2
exit 1
elif [[ $F != *:+([0-9]),*([A-Z]) ]]; then
echo "File format is not recognized: $F" >&2
exit 1
fi
done
fi
if [[ $CHANGE_MODE == R ]]; then
for F in "${FILES[@]}"; do
CODE=${F##*,}
[[ $CODE != *R* ]] && echo mv "$F" "${F%,*},R${CODE}"
done
else
for F in "${FILES[@]}"; do
CODE=${F##*,}
[[ $CODE == *R* ]] && echo mv "$F" "${F%,*},${CODE//R}"
done
fi