使用 Sed 尋找並替換,而不是常規文件

使用 Sed 尋找並替換,而不是常規文件

我正在開發一個便攜式 anaconda 包,我嘗試使用 find 和 sed 查找所有文件並替換當前目錄及其子目錄中文件內的路徑。

然而,當我執行該命令時,sed 拋出錯誤:

sed: couldn't edit anaconda3: not a regular file

我正在使用的命令是:

find ./ -type f -exec sed -i -e "s+/opt/conda_tools+$TOOLKIT_DIR+g" * {} \;

我使用 + 作為分隔符,因為 / 是路徑的一部分。我正在執行腳本的目錄的內容是:

drwxr-xr-x 24 test_user linuxusers 4096 十一月 21 16:07 anaconda3

單獨執行 find 命令會如預期列出文件,但由於某種原因,目錄名稱仍然被選取。我還嘗試了該命令的以下變體,但沒有成功:

find ./ -type f -exec sed -i '' "s+/opt/conda_tools+$INSTALL_DIR+g" * {} \;

我以前曾成功使用過 find 和 sed,但我有點困惑下一步該去哪裡。有什麼明顯的錯誤是我在這裡遺漏的,或不那麼明顯的嗎?

答案1

刪除其中的“*”

find ./ -type f -exec sed -ie "s+/opt/conda_tools+$TOOLKIT_DIR+g" * {} \;

所以

find ./ -type f -exec sed -ie "s+/opt/conda_tools+$TOOLKIT_DIR+g" {} \;

相關內容