renaming file with same name as directory name

2019-02-25 02:53发布

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.

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

I want to rename each of this file result.pdf with same name as directory 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?

2条回答
2楼-- · 2019-02-25 03:21

How about this one-liner -

for i in *; do mv "$i/result.pdf" "$i/$i.pdf" ; done
查看更多
爷、活的狠高调
3楼-- · 2019-02-25 03:28

Try to quote the names:

for i in *; do
  mv "$i/result.pdf" "$i/$i.pdf";
done
查看更多
登录 后发表回答