我想触摸目录中的每个文件:
files=$(ls -a "node_modules/suman-types/dts")
echo "files $files";
for file in "$files"; do
echo "touching file $file";
touch "node_modules/suman-types/dts/$file";
done
但运行后,我得到:
inject.d.ts
injection.d.ts
integrant-value-container.d.ts
it.d.ts
reporters.d.ts
runner.d.ts
suman-utils.d.ts
suman.d.ts
table-data.d.ts
test-suite-maker.d.ts
test-suite.d.ts: File name too long
“文件名太长”消息是什么意思?
更新#1
我将我的脚本更改为:
files=$(find "node_modules/suman-types/dts" -name "*.d.ts")
for file in "$files"; do
echo "touching file $file";
touch "$file";
done
touch "node_modules/suman-types"
但后来我明白了:
$ ./types-touch.sh
touching file node_modules/suman-types/dts/after-each.d.ts
node_modules/suman-types/dts/after.d.ts
node_modules/suman-types/dts/before-each.d.ts
node_modules/suman-types/dts/before.d.ts
node_modules/suman-types/dts/describe.d.ts
node_modules/suman-types/dts/global.d.ts
node_modules/suman-types/dts/index-init.d.ts
node_modules/suman-types/dts/inject.d.ts
node_modules/suman-types/dts/injection.d.ts
node_modules/suman-types/dts/integrant-value-container.d.ts
node_modules/suman-types/dts/it.d.ts
node_modules/suman-types/dts/reporters.d.ts
node_modules/suman-types/dts/runner.d.ts
node_modules/suman-types/dts/suman-utils.d.ts
node_modules/suman-types/dts/suman.d.ts
node_modules/suman-types/dts/table-data.d.ts
node_modules/suman-types/dts/test-suite-maker.d.ts
node_modules/suman-types/dts/test-suite.d.ts
touch: node_modules/suman-types/dts/after-each.d.ts
node_modules/suman-types/dts/after.d.ts
node_modules/suman-types/dts/before-each.d.ts
node_modules/suman-types/dts/before.d.ts
node_modules/suman-types/dts/describe.d.ts
node_modules/suman-types/dts/global.d.ts
node_modules/suman-types/dts/index-init.d.ts
node_modules/suman-types/dts/inject.d.ts
node_modules/suman-types/dts/injection.d.ts
node_modules/suman-types/dts/integrant-value-container.d.ts
node_modules/suman-types/dts/it.d.ts
node_modules/suman-types/dts/reporters.d.ts
node_modules/suman-types/dts/runner.d.ts
node_modules/suman-types/dts/suman-utils.d.ts
node_modules/suman-types/dts/suman.d.ts
node_modules/suman-types/dts/table-data.d.ts
node_modules/suman-types/dts/test-suite-maker.d.ts
node_modules/suman-types/dts/test-suite.d.ts: No such file or directory
答案1
您的问题源于将所有ls
输出捕获到名为 的单个(字符串)变量中files
。该变量看起来像:
filename1\nfilename2\nfilename3\n...
亲自查看:
echo "$files" | od -c
您真正要做的是在一个非常长的字符串上循环一次,该字符串对应于一个不存在的文件。你得到的错误信息有点丰富——它告诉你这个长文件名字符串不存在。
要访问目录中的每个文件,只需使用 shell 通配符并运行touch
(通配符只会获取该目录中的文件):
touch node_modules/suman-types/dts/*
或者一一触摸它们:
for file in node_modules/suman-types/dts/*; do touch "$file"; done
或用于在目录中递归查找所有文件,并对其find
运行:touch
find node_modules/suman-types/dts -type f -exec touch -- {} \;
或者在支持它的 shell 中(Bash/ksh/zsh,有一些变体),使用递归 glob 运算符**
:
shopt -s globstar # in Bash
for file in node_modules/suman-types/dts/**/*; do
touch "$file"
done
答案2
尝试这样做文件名扩展:
for file in node_modules/suman-types/dts/*; do
echo "Touching file: ${file##*/}"
touch "${file}"
done