简体   繁体   中英

Having trouble renaming files in subdirectories (With conditions) [shell scripting]

I am not succeeding at writing a proper code!

My directory is like this

/Users/dave/yellow/shade001/light
/Users/dave/yellow/shade001/dark1
/Users/dave/yellow/shade001/dark2
...
/Users/dave/yellow/shade999/light
/Users/dave/yellow/shade999/dark1
/Users/dave/yellow/shade999/dark2

There are about 999 shade folders inside yellow directory, each contains 2 or 3 files ( light always present, dark1 always present, and dark2 sometime present). I would like to rename light , dark1 and delete dark2 such that:

light.txt renamed to lgt.txt

dark1.txt renamed to dk1.txt

dark2 (whenever found) should be deleted

Here is what i did:

for /r %x in (light.txt) do ren "%x" lgt.txt for /r %x in (dark1.txt) do ren "%x" dk1.txt find . -type f -name 'dark2' -delete done

In Bash or similar shell:

find shade* -name light.txt | while read NAME; do mv $NAME `dirname $NAME`/lgt.txt; done

Analogically with dark1 and dark2 (rm instead of mv in the latter case).

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