1

Little help in solving a workflow with shell script using the find command.

  1. Finding all index.html files in every folder.

We can use this with find command for that.

find ./ -type f -name 'index.html' 
  1. Renaming the file index.html with the folder names.
  2. After renaming the files, I wanted to move the files one level up.

I'm stuck at renaming and moving the files one level up.

As I have more than 1 lakh files, Xargs will be handy for this.

Here is code I have so far

find ./ -type f -name 'index.html' | xargs -P 4 
├── dc
│   ├── adams-morgan
│   │   ├── car-donation
│   │   │   └── index.html
│   │   ├── feed.rss
│   │   ├── index.html
│   │   ├── junk-car
│   │   │   └── index.html
│   │   └── sitemap.xml
│   ├── american-university
│   │   ├── car-donation
│   │   │   └── index.html
│   │   ├── feed.rss
│   │   ├── index.html
│   │   ├── junk-car
│   │   │   └── index.html
│   │   └── sitemap.xml

Any help in in renaming the index.html files and moving the files one level up?

Thanks in advance Suresh

1 Answer 1

0

Instead of xargs you can use find -exec. Inside, you can run a small sh-Script:

find . -mindepth 2 -type f -name 'index.html' -exec sh -c '
    d="$(dirname "$1")";
    mv "$1" "$d/../$(basename "$d").html";
    rmdir "$d";
' find-sh {} \;
4
  • This works very well. In the root folder there is home page of index.html that is deleting too. As an alternative option, I did took the index.html as backup to keep it after code runs. Thanks Plumo for the fastest help :) May 7, 2020 at 12:05
  • Add -mindepth 2 so the file in the root is not touched
    – pLumo
    May 7, 2020 at 12:17
  • Hey Plumo, It works :) I just tested it on a new instance and it's spot on with your code. Thanks you so much mate. May 7, 2020 at 14:02
  • Hi Pulmo, Can we able to remove .html extension for the final files? I did tried to remove .html in the final rename.. it isn't working. Should we try another code line for that functionality? thanks in advance :) May 10, 2020 at 16:50

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.