在 Linux 中查找多个文件并重命名它们
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16541582/
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
Find multiple files and rename them in Linux
提问by rashok
I am having files like a_dbg.txt, b_dbg.txt ...
in a Suse 10
system. I want to write a bash shell script which should rename these files by removing "_dbg" from them.
我a_dbg.txt, b_dbg.txt ...
在Suse 10
系统中拥有文件。我想编写一个 bash shell 脚本,它应该通过从中删除“_dbg”来重命名这些文件。
Google suggested me to use rename
command. So I executed the command rename _dbg.txt .txt *dbg*
on the CURRENT_FOLDER
谷歌建议我使用rename
命令。所以我rename _dbg.txt .txt *dbg*
在CURRENT_FOLDER
My actual CURRENT_FOLDER
contains the below files.
我的实际CURRENT_FOLDER
包含以下文件。
CURRENT_FOLDER/a_dbg.txt
CURRENT_FOLDER/b_dbg.txt
CURRENT_FOLDER/XX/c_dbg.txt
CURRENT_FOLDER/YY/d_dbg.txt
After executing the rename
command,
执行rename
命令后,
CURRENT_FOLDER/a.txt
CURRENT_FOLDER/b.txt
CURRENT_FOLDER/XX/c_dbg.txt
CURRENT_FOLDER/YY/d_dbg.txt
Its not doing recursively, how to make this command to rename files in all subdirectories. Like XX
and YY
I will be having so many subdirectories which name is unpredictable. And also my CURRENT_FOLDER
will be having some other files also.
它不是递归执行,如何使此命令重命名所有子目录中的文件。Like XX
and YY
I will have so many subdirectories that name is unpredictable.喜欢,我将拥有许多名称不可预测的子目录。而且我CURRENT_FOLDER
也会有一些其他文件。
采纳答案by kamituel
You can use find
to find all matching files recursively:
您可以使用find
递归查找所有匹配的文件:
$ find . -iname "*dbg*" -exec rename _dbg.txt .txt '{}' \;
EDIT:what the '{}'
and \;
are?
编辑:什么'{}'
和\;
是谁?
The -exec
argument makes find execute rename
for every matching file found. '{}'
will be replaced with the path name of the file. The last token, \;
is there only to mark the end of the exec expression.
该-exec
参数使 findrename
对找到的每个匹配文件执行。'{}'
将替换为文件的路径名。最后一个标记,\;
仅用于标记 exec 表达式的结尾。
All that is described nicely in the man page for find:
所有这些都在 find 的手册页中得到了很好的描述:
-exec utility [argument ...] ;
True if the program named utility returns a zero value as its
exit status. Optional arguments may be passed to the utility.
The expression must be terminated by a semicolon (``;''). If you
invoke find from a shell you may need to quote the semicolon if
the shell would otherwise treat it as a control operator. If the
string ``{}'' appears anywhere in the utility name or the argu-
ments it is replaced by the pathname of the current file.
Utility will be executed from the directory from which find was
executed. Utility and arguments are not subject to the further
expansion of shell patterns and constructs.
回答by glenn Hymanman
with bash:
使用 bash:
shopt -s globstar nullglob
rename _dbg.txt .txt **/*dbg*
回答by VishnuVardhanA
small script i wrote to replace all files with .txt extension to .cpp extension under /tmp and sub directories recursively
我编写的小脚本将 /tmp 和子目录下的所有文件以 .txt 扩展名替换为 .cpp 扩展名
#!/bin/bash
for file in $(find /tmp -name '*.txt')
do
mv $file $(echo "$file" | sed -r 's|.txt|.cpp|g')
done
回答by Manh Tai
Scipt above can be written in one line:
上面的 Scipt 可以写成一行:
find /tmp -name "*.txt" -exec bash -c 'mv for f in $(find . -name "*dbg*"); do mv $f $(echo $f | sed 's/_dbg//'); done
$(echo "#on current folder
rnm -dp -1 -fo -ssf '_dbg' -rs '/_dbg//' *
" | sed -r \"s|.txt|.cpp|g\")' '{}' \;
回答by pulse
classic solution:
经典解决方案:
rnm -dp -1 -fo -ssf '_dbg' -rs '/_dbg//' /path/to/the/directory
回答by Jahid
If you just want to rename and don't mind using an external tool, then you can use rnm. The command would be:
如果您只想重命名并且不介意使用外部工具,那么您可以使用rnm。命令将是:
find -iname \*.* | rename -v "s/ /-/g"
-dp -1
will make it recursive to all subdirectories.
-dp -1
将使其递归到所有子目录。
-fo
implies file only mode.
-fo
暗示仅文件模式。
-ssf '_dbg'
searches for files with _dbg in the filename.
-ssf '_dbg'
搜索文件名中带有 _dbg 的文件。
-rs '/_dbg//'
replaces _dbg with empty string.
-rs '/_dbg//'
用空字符串替换 _dbg。
You can run the above command with the path of the CURRENT_FOLDER too:
您也可以使用 CURRENT_FOLDER 的路径运行上述命令:
rename --no-act 's/\.html$/\.php/' *.html */*.html
回答by Gantan
For renaming recursively I use the following commands:
为了递归重命名,我使用以下命令:
##代码##回答by u5602117
You can use this below.
您可以在下面使用它。
##代码##