
在我的專案中,我有很多測試文件,它們的名稱為*.ts
.
它們都在__tests__
資料夾中,但也可以是__tests__/test1.ts
,__tests__/dir2/test2.ts
等等。
我想將這些文件重命名為*.test.ts
.
我怎樣才能做到呢?
例子:
project/src/__tests__/app.ts
->project/src/__tests__/app.test.ts
project/src/dashboard/__test__/start/login.ts
->project/src/dashboard/__test__/start/login.test.ts
我成功地找到了這些文件:
find . -type f -path '*__tests__*.ts'
但不知道如何重命名它們。
答案1
您需要-exec
呼叫 shell 來執行重命名的選項。
find . -type f -path '*__tests__*.ts' -exec sh -c '
for f; do
mv -- "$f" "${f%ts}test.ts"
done
' findsh {} +
-exec sh -c
呼叫 shell 並運行:for f; do mv -- "$f" "${f%ts}test.ts"; done
循環找到的文件,將它們重新命名為ts
已刪除的目標,並test.ts
在其位置上放置 a 。
- 在 中
findsh {} +
,findsh
只是一個佔位符,是將檔案提供給指令(shell){} +
的構造。-exec