简体   繁体   中英

linux command to remove or replace whitespaces and special characters

I can get rename ' ' _ * to work to remove the first whitespace, but when I try something like rename 's/ /_/g' * nothing happens. Ideally I would like a command that can rename a file and remove all whitespaces and special characters to leave only letters and numbers.

something line this may be, extracts only letters and numbers

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

for all files in the directory something like

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM