簡體   English   中英

linux 命令刪除或替換空格和特殊字符

[英]linux command to remove or replace whitespaces and special characters

我可以讓rename ' ' _ *工作以刪除第一個空格,但是當我嘗試類似rename 's/ /_/g' *時沒有任何反應。 理想情況下,我想要一個可以重命名文件並刪除所有空格和特殊字符以僅保留字母和數字的命令。

這可能是某行,僅提取字母和數字

echo "file with spaces here.txt" | sed -e 's/[^0-9a-zA-Z ]/ /g' | tr ' ' '_'

對於目錄中的所有文件,例如

for f in *;
do
   newName=$( echo "$f" | sed -e 's/[^0-9a-zA-Z ]/ /g' | tr ' ' '_' );
   mv "$f" "$newName";
done

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM