bash 重命名与目录名称同名的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8886239/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:19:20  来源:igfitidea点击:

renaming file with same name as directory name

bashshellscripting

提问by Registered User

I have a directory structure as follows

我有一个目录结构如下

dir----|
       |
       |--dir1\ some\ thing--result.pdf
       |
       |--dir2\ some\ thing--result.pdf
       |
       |--dir3\ some\ thing--result.pdf
       |

The file name in each subdirectory is result.pdf.

每个子目录中的文件名是result.pdf。

Where as directories dir1 dir2 dir3 have blank spaces in their name. What I am not able to understand (I am writing a bash script) how do I take this directory name in variable and rename result.pdf

其中目录 dir1 dir2 dir3 的名称中有空格。我无法理解的内容(我正在编写 bash 脚本)如何在变量中使用此目录名称并重命名 result.pdf

I want to rename each of this file result.pdf with same name as directory name.pdf

我想将每个文件 result.pdf 重命名为与目录 name.pdf 相同的名称

   #!/bin/bash
    for i in *;do
    cd $i
    mv result.pd $i.pdf
    cd ..
    done

blank spaces in directory names are creating problems.How to over come this?

目录名称中的空格会产生问题。如何解决这个问题?

回答by ypnos

Try to quote the names:

尝试引用名称:

for i in *; do
  mv "$i/result.pdf" "$i/$i.pdf";
done

回答by jaypal singh

How about this one-liner -

这个单线怎么样——

for i in *; do mv "$i/result.pdf" "$i/$i.pdf" ; done