English 中文(简体)
Bash 改名夹的书写
原标题:Bash script to rename folders recursively

我想重新命名这些文件夹中的我所有文件夹和文件夹,这样,文件夹中的所有内容都用空间取而代之。 你们能否帮助我这样做?

问题回答

使用<代码>-execdir的回答较为简单,但仅使用<代码>find的版本,即支持<代码>-execdir,例如政府数据库,自 rel=“noreferer”以来。 POSIX只有-exec

如果你只想使用ash子,那就令人惊讶地trick了,因为你重新命名了<编码>限定的<<<>>>的目录。 即使你通过使用<条码>-深度选择<条码>,也可确保你只将这条道路的最后部分改成<条码>-exec。 The following is a trivial differences of ,one of the Bash FAQ andview OK for me:

find . -depth -name "*_*" -exec bash -c  dir=${1%/*} base=${1##*/}; mv "$1" "$dir/${base//_/ }"  _ {} ;

That FAQ answer has more discussion about problem of recursively naming folders which may be of interest.


<<>Update>/em>: 由于一名仲裁员相当复杂,为了解释起见,可能有必要将其拆解。 基本上,<代码>定后 指挥:

find . -depth -name "*_*" -exec bash -c [SOME-STUFF] _ {} ;

In other words, find all the directories which contain an underscore, and for each such directory, starting with the deepest, run the bash script [SOME-STUFF], with parameter 0 as _ (to indicate that we don t care about it) and parameter 1 as the name of the file that find found. (find will substitute the filename for {} after -exec. The ; just terminates the command that -exec runs.)

随后,<代码>[SOME-STUFF]部分由以下部分组成:

dir=${1%/*}

... which, using parameter expansion, will remove the shortest match for /* from the end of $1 (the filename) and set dir to the result. Similarly, this part:

base=${1##*/}

......从<代码>1开始起,删除了<代码>*>/和<代码>底线/代码>的长度。 http://code>base>只是道路上的最后一部分。

Then the renaming is actually done by the mv command, which is:

mv "$1" "$dir/${base//_/ }"

这再次使用参数扩展,这次使用<代码>{参数/距离/指示> syntax。 姓名($1)改名为$dir。 之后是<代码> 底线,但每一条强调在<代码> 底线上以一个空间取代。

Yes, simply cd to /dir and :

find -depth -type d -execdir rename  s/_/ /g  {} ;

视不同情况,你需要改名(有时是名前)。

如果有

file $(type -p rename)

产出包括ELF, 看起来坏;

edit -depth and -execdir added

如果你有血汗,你可以:

for i in **; do [[ -d "$i" ]] || continue; mv "$i" "${i/_/ }"; done




相关问题
What does it mean "to write a web service"?

I just asked a question about whether it was possible to write a web-page-checking code and run it from free web server, and one supporter answered and said that it was possible only if I run "a web ...

How can I use exit codes to run shell scripts sequentially?

Since cruise control is full of bugs that have wasted my entire week, I have decided the existing shell scripts I have are simpler and thus better. Here is what I have so far svn update /var/www/...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Why does Scala create a ~/tmp directory when I run a script?

When I execute a Scala script from the command line, a directory named "tmp" is created in my home directory. It is always empty, so I simply deleted it without any apparent problem. Of course, when I ...

Ivy, ant and start scripts

I have a project that uses ant to build and ivy for dependencies. I would like to generate the start scripts for my project, with the classpath, based on the dependencies configured in Ivy, ...

热门标签