Mac – Change html files to txt on entire folder

command linemacterminal

Is there a mac terminal command to change all .html files to .txt including the files in sub folders too?

Best Answer

I guess actually you just want to rename them. Run the following from inside the folder. If you do not trust the command, add echo before mv to see what it would do.

find . -type f -name "*.html" -exec bash -c "mv {} \`dirname {}\`/\`basename -s.html {}\`.txt" \;

This does the following:

  1. Looks up every file (-type f) in the current folder (.) whose name is ends with ".html" (-name "*.html")
  2. It than finds out the path of the file (dirname), adds a slash (/) and the name of the original file without the ".html" suffix (basename -s.html) and adds a ".txt" suffix.
  3. Then it renames the original file (mv).

Actually, the ` ` notation for using a programs output as part of a command is deprecated. $() should be used instead, but in the case of find the backticks are easier to use.