给定以下目录:
[~/test]find .
.
./something_else.txt
./univision.rbw
./something_else.rb
./testtest
./testtest/weewee.rb
./testtest/weewee.txt
./univision.rb
运行以下 sed 命令会产生很多mv
错误:
[~/test]for i in $( find . ); do mv $i `echo $i | sed s/test/this_is_not_a_test/g`; done;
mv: ‘.’ and ‘./.’ are the same file
mv: ‘./something_else.txt’ and ‘./something_else.txt’ are the same file
mv: ‘./univision.rbw’ and ‘./univision.rbw’ are the same file
mv: ‘./something_else.rb’ and ‘./something_else.rb’ are the same file
mv: cannot stat ‘./testtest/weewee.rb’: No such file or directory
mv: cannot stat ‘./testtest/weewee.txt’: No such file or directory
mv: ‘./univision.rb’ and ‘./univision.rb’ are the same file
但它也能“发挥作用”
[~/test]find .
.
./something_else.txt
./univision.rbw
./something_else.rb
./this_is_not_a_testthis_is_not_a_test
./this_is_not_a_testthis_is_not_a_test/weewee.rb
./this_is_not_a_testthis_is_not_a_test/weewee.txt
./univision.rb
我在这里遗漏了什么?
答案1
如果文件名不包含test
,则表示您将其移动到自身上方(如mv . ./.
)。它会发出错误,但不会更改任何内容。test
名称中包含 的文件会正确重命名。
答案2
您首先将目录重命名./testtest
为./this_is_not_a_testthis_is_not_a_test
,然后进一步尝试重命名例如./testtest/weewee.rb
将失败,因为该文件不再存在。您应该-depth
首先使用 find 的选项来处理目录中的条目,然后将目录本身作为最后一步。