bash 检查文件名中的点并删除它们(在 Linux 上)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16426958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check for dots in the file name and remove them (on Linux)
提问by user2359595
I found a bash script for my Synology NAS that is looking for *.mp4 files in a folder, strips the video out and saves them as *.m4a. (The purpose is an automatic 'YouTube to podcast converter').
我找到了一个用于 Synology NAS 的 bash 脚本,该脚本正在文件夹中查找 *.mp4 文件,将视频剥离并将它们另存为 *.m4a。(目的是一个自动的“YouTube 到播客转换器”)。
for f in *.mp4; do mv -- "$f" "$(date +%Y-%m-%d -r "${f}") $f"; done
for f in *.mp4; do ffmpeg -i "$f" -vn -acodec copy "${f%.mp4}.m4a"; done
Sometimes the *.mp4 file names contain periods, e.g. 'This video...mp4', resulting in the podcast player not recognizing such files.
有时*.mp4 文件名包含句点,例如“此视频...mp4”,导致播客播放器无法识别此类文件。
Is there a line I can add to the script to remove period(s) in front of the extension or to just remove them all?
是否有一行可以添加到脚本中以删除扩展名前面的句点或将它们全部删除?
采纳答案by Adrian Frühwirth
Remove the extension from ${f}, then delete or substitute all dots, then add the extension back when you rename the files essentially dealing with all but the last dot:
从 中删除扩展名${f},然后删除或替换所有点,然后在重命名文件时重新添加扩展名,基本上处理除最后一个点之外的所有内容:
#!/bin/bash
for f in *.mp4; do
f=${f%.mp4}
mv -- "${f}.mp4" "$(date +%Y-%m-%d -r "${f}.mp4") ${f//./_}.mp4"
done
for f in *.mp4; do
ffmpeg -i "${f}" -vn -acodec copy "${f%.mp4}.m4a"
done
Obviously this only works for a single extension (.mp4in this case).
显然这仅适用于单个扩展(.mp4在这种情况下)。
回答by Stefano Sanfilippo
Use the renamecommand to replace multiple consecutive dots with just one:
使用以下rename命令将多个连续的点替换为一个:
rename 's/\.+/./g' *.mp4
If you have perlinstalled and you cannot find rename, try also prename.
如果您已perl安装但找不到rename,也请尝试prename。
回答by Amardeep AC9MF
Try the following, which uses tr to remove any periods in the filename:
尝试以下操作,它使用 tr 删除文件名中的所有句点:
for f in *.mp4; do mv -- "$f" "$(date +%Y-%m-%d -r "${f}") $f"; done
for f in *.mp4; do ffmpeg -i "$f" -vn -acodec copy "`echo "$f" | tr -d "\."`.m4a"; done
回答by Somnath Rakshit
Just came across this answer here.
You can use the renamepackage by installing it using the following command.
刚刚在这里遇到了这个答案。
您可以rename通过使用以下命令安装该软件包来使用它。
sudo apt install rename
Then go to the directory containing the files and use the following command:
然后转到包含文件的目录并使用以下命令:
rename 's/\.(?!mp4$)/ /g' *.mp4

