我有大約100個目錄都在同一個父目錄中,遵守命名約定[sitename] .com。我想將它們重命名爲[sitename] .subdomain.com。需要一個快速bash腳本
這裏是我的嘗試:
for FILE in `ls | sed 's/.com//' | xargs`;mv $FILE.com $FILE.subdomain.com;
但悲慘的失敗了。有任何想法嗎?
我有大約100個目錄都在同一個父目錄中,遵守命名約定[sitename] .com。我想將它們重命名爲[sitename] .subdomain.com。需要一個快速bash腳本
這裏是我的嘗試:
for FILE in `ls | sed 's/.com//' | xargs`;mv $FILE.com $FILE.subdomain.com;
但悲慘的失敗了。有任何想法嗎?
使用bash:
for i in *
do
mv $i ${i%%.com}.subdomain.com
done
的$ {。我%% COM}構建回報我沒有'.com'後綴的價值。
find . -name '*.com' -type d -maxdepth 1 \
| while read site; do
mv "${site}" "${site%.com}.subdomain.com"
done
什麼:
ls |
grep -Fv '.subdomain.com' |
while read FILE; do
f=`basename "$FILE" .com`
mv $f.com $f.subdomain.com
done
使用rename(1)。
rename .com .subdomain.com *.com
如果你有一個perl rename,而不是正常的,這個工程:
rename s/\\.com$/.subdomain.com/ *.com
參見:http://blog.ivandemarino.me/2010/09/30/Rename-Subdirectories-in-a-Tree-the-Bash-way
#!/bin/bash
# Simple Bash script to recursively rename Subdirectories in a Tree.
# Author: Ivan De Marino <[email protected]>
#
# Usage:
# rename_subdirs.sh <starting directory> <new dir name> <old dir name>
usage() {
echo "Simple Bash script to recursively rename Subdirectories in a Tree."
echo "Author: Ivan De Marino <[email protected]>"
echo
echo "Usage:"
echo " rename_subdirs.sh <starting directory> <old dir name> <new dir name>"
exit 1
}
[ "$#" -eq 3 ] || usage
recursive()
{
cd "$1"
for dir in *
do
if [ -d "$dir" ]; then
echo "Directory found: '$dir'"
(recursive "$dir" "$2" "$3")
if [ "$dir" == "$2" ]; then
echo "Renaming '$2' in '$3'"
mv "$2" "$3"
fi;
fi;
done
}
recursive "$1" "$2" "$3"
試試這個:
for FILE in `ls -d *.com`; do
FNAME=`echo $FILE | sed 's/\.com//'`;
`mv $FILE $FNAME.subdomain.com`;
done
我覺得有一個在.com.`的`結束了加時賽。 – 2011-01-24 17:49:11
是的,修正了這個問題。 – Tonttu 2011-01-24 17:51:48
我通常會用mmv來解決這個問題。感謝讓我意識到另一個工具。 – 2011-01-24 18:09:52