如何使用 bash 脚本从文件中递归删除特定扩展名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3837438/
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
How do I remove a specific extension from files recursively using a bash script
提问by nick
I'm trying to find a bash script that will recursively look for files with a .bx extension, and remove this extension. The filenames are in no particular format (some are hidden files with "." prefix, some have spaces in the name, etc.), and not all files have this extension.
我正在尝试找到一个 bash 脚本,该脚本将递归查找具有 .bx 扩展名的文件,并删除此扩展名。文件名没有特定的格式(有些是带有“.”前缀的隐藏文件,有些名称中有空格等),并且并非所有文件都具有此扩展名。
I'm not sure how to find each file with the .bx extension (in and below my cwd) and remove it. Thanks for the help!
我不确定如何找到每个扩展名为 .bx 的文件(在我的 cwd 中和下方)并将其删除。谢谢您的帮助!
回答by tylerl
find . -name '*.bx' -type f | while read NAME ; do mv "${NAME}" "${NAME%.bx}" ; done
回答by Ken
find -name "*.bx" -print0 | xargs -0 rename 's/\.bx//'
回答by ghostdog74
Bash 4+
重击 4+
shopt -s globstar
shopt -s nullglob
shopt -s dotglob
for file in **/*.bx
do
mv "$file" "${file%.bx}"
done
回答by jyz
for blah in *.bx ; do mv ${blah} ${blah%%.bx}
for blah in *.bx ; do mv ${blah} ${blah%%.bx}
回答by gsbabil
Here is another version which does the following:
这是另一个版本,它执行以下操作:
- Finds out files based on
$old_extvariable (right now set to.bx) in and belowcwd, stores them in$files - Replaces those files' extension to nothing (or something new depending on
$new_extvariable, currently set to.xyz)
- 根据中和下方的
$old_ext变量(现在设置为.bx)找出文件cwd,将它们存储在$files - 将这些文件的扩展名替换为空(或根据
$new_ext变量的新内容,当前设置为.xyz)
The script uses dirnameand basenameto find out file-path and file-name respectively.
该脚本使用dirname和basename分别找出文件路径和文件名。
#!/bin/bash
old_ext=".bx"
new_ext=".xyz"
files=$(find ./ -name "*${old_ext}")
for file in $files
do
file_name=$(basename $file $old_ext)
file_path=$(dirname $file)
new_file=${file_path}/${file_name}${new_ext}
#echo "$file --> $new_file"
mv "$file" "$new_file"
done
回答by ajaaskel
Extra: How to remove any extension from filenames
额外:如何从文件名中删除任何扩展名
find -maxdepth 1 -type f | sed 's/.\///g'| grep -E [.] | while read file; do mv $file ${file%.*}; done
will cut starting from last dot, i.e. pet.cat.dog ---> pet.cat
将从最后一个点开始切割,即 pet.cat.dog ---> pet.cat
find -maxdepth 1 -type f | sed 's/.\///g'| grep -E [.] | while read file; do mv $file ${file%%.*}; done
will cut starting from first dot, i.e. pet.cat.dog ---> pet
将从第一个点开始切割,即 pet.cat.dog ---> pet
"-maxdepth 1" limits operation to current directory, "-type f" is used to select files only. Sed & grep combination is used to pick only filenames with dot. Number of percent signs in "mv" command will define actual cut point.
“-maxdepth 1”限制对当前目录的操作,“-type f”仅用于选择文件。sed 和 grep 组合用于仅选择带点的文件名。“mv”命令中的百分号数量将定义实际切割点。
回答by Raghuram
Assuming you are in the folder from where you want to do this
假设您位于要执行此操作的文件夹中
find . -name "*.bx" -print0 | xargs -0 rename .bx ""

