简体   繁体   中英

Renaming files with the same names as directory - bash script

I want to rename my files so that they are name with the same name as the folder.

I have a main folder that has around 1000 folders. each of these folders have another file within it. in that very last folder, I have files with different extentions. and I want to rename the files that have pdb extention.

here's the strcuture of my folders :

pv----|

       |--m10\ pk\ result0.pdb result1.pdb result2.pdb
       |--m20\ pk\ result0.pdb result1.pdb result2.pdb
       |--m30\ pk\ result0.pdb result1.pdb result2.pdb

I want something like this :

pv----|

       |--m10\ pk\ m10_result0.pdb m10_result1.pdb m10_result2.pdb
       |--m20\ pk\ m20_result0.pdb m20_result1.pdb m20_result2.pdb
       |--m30\ pk\ m30_result0.pdb m30_result1.pdb m30_result2.pdb

that's the code I made but It's not working ..

for d in MD_PR2 / * / * / 

do

     (cd "$d" && for file in *.pdb ; do mv "$file" "${file/result/$d_result}" ; done)
done

my code is deleting "result" of each file's name and I don't know. it becomes 0.pdb , 1.pdb ..etc

thank you very much

Before:

user@pc:~$ tree
.
├── m10
│   └── pk
│       ├── result0.pdb
│       ├── result1.pdb
│       └── result2.pdb
├── m20
│   └── pk
│       ├── result0.pdb
│       ├── result1.pdb
│       └── result2.pdb
└── m30
    └── pk
        ├── result0.pdb
        ├── result1.pdb
        └── result2.pdb

Your code is not working because $d_result is being interpreted as a variable name, not as a concatenation of $d and _result . I suggest using ${d}_result .

However I would suggest another approach, one that doesn't need to cd into each directory.

Code:

shopt -s globstar
for file in **; do 
  if [[ "$file" =~ ".pdb" ]] ; then 
    mv "$file" `echo $file | sed -e 's/\(.*\)\/\(.*\)\/\(.*.pdb\)/\1\/\2\/\1_\2_\3/'`; 
  fi; 
done;

After:

user@pc:~$ tree
.
├── m10
│   └── pk
│       ├── m10_pk_result0.pdb
│       ├── m10_pk_result1.pdb
│       └── m10_pk_result2.pdb
├── m20
│   └── pk
│       ├── m20_pk_result0.pdb
│       ├── m20_pk_result1.pdb
│       └── m20_pk_result2.pdb
└── m30
    └── pk
        ├── m30_pk_result0.pdb
        ├── m30_pk_result1.pdb
        └── m30_pk_result2.pdb

Code explanation:

  • shopt -s globstar : Allow for ** to be expanded into "all files and directories recursively"
  • Variable "file" contains filenames including directories
  • Check "file" against "$file" =~ ".pdb" to ignore working with directories
  • Generate newfilename with sed :
    • Search and replace: s/search/replace/
    • Find something like dir1/dir2/smthg.pdb: (.*)/(.*)/(.*.pdb)
    • Replace with dir1/dir2/dir1_dir2_smthg.pdb: \\1/\\2/\\1_\\2_\\3 (replace with \\1_\\2_\\3 if you also want to move renamed files into parent dir)
    • (I removed some backslashes for readability)
  • mv file to newfilename

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM