1
$ tree .

.
├── tmp1
│   └── acsfd.md
├── tmp2
│   └── adb.md
└── tmp3
    └── aa23aa.md
...
└── tmpn
    └── random.md

Want to rename all md files to index.md,

I tried:

find . -name "*.md" -exec mv {} index.md \;

But this will delete all md files and create one index.md on current folder.

4

In this case, you need -execdir instead of -exec to run mv in the directory containing the matched file:

$ tree
.
├── tmp1
│   └── rand32726.md
├── tmp2
│   └── rand16097.md
├── tmp3
│   └── rand10683.md
└── tmpn
    └── rand23531.md

4 directories, 4 files
$ find . -iname '*.md' -execdir mv {} index.md \;
$ tree
.
├── tmp1
│   └── index.md
├── tmp2
│   └── index.md
├── tmp3
│   └── index.md
└── tmpn
    └── index.md

4 directories, 4 files
  • yes, thank you. – FaiChou Mar 28 at 6:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.