我正在尝试将目录中的所有文件分别移动到新目录 phonex 并同时重命名它们 phonex.txt。 所以例如
1.txt, 2.txt, jim.txt
成为:
phone1.txt in directory phone1
phone2.txt in directory phone2
phone3.txt in directory phone3.
我是 awk 的新手,但我已经设法创建了目录,但我无法正确重命名。 我试过:
ls|xargs -n1|awk ' {i++;system("mkdir phone"i);system("mv "$0" phone”i ”.txt -t phone"i)}'
哪些错误有很多:
mv: cannot stat `phone”i': No such file or directory
和:
ls|xargs -n1|awk ' {i++;system("mkdir phone"i);system("mv "$0" phone”i ”/phone"i”.txt”)}'
错误:
awk: 1: unexpected character 0xe2
xargs:/bin/echo: 由信号 13 终止 谁能帮我完成它? TIA!
最佳答案
在这种情况下,完全没有必要将 ls
通过管道传输到 xargs
到 awk
中。您尝试做的事情可以使用一个简单的循环来完成:
for f in *.txt; do
i=$((i+1))
dir="phone$i"
mkdir "$dir" && mv "$f" "$dir/$dir.txt"
done
根据你的 shell,$i
的增量可以用不同的方式完成(比如 bash 中的 ((++i))
),但这种方式是POSIX 兼容,因此应该适用于任何现代 shell。
顺便说一句,你原来的错误的原因是你使用了 curl 引号 ”
,shell 无法理解。您应该始终只在 shell 中使用单 '
和双 "
。
关于linux - awk 系统语法抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26423878/