Is there a mac terminal command to change all .html files to .txt including the files in sub folders too?
Asked
Active
Viewed 394 times
1
-
see http://apple.stackexchange.com/questions/64497/how-to-rename-multiple-files-at-once – Tetsujin Oct 11 '15 at 06:30
-
I got it but how do we do this to an entire folder! Not logical to enter 1000's of file names. – Adrien Oct 11 '15 at 06:32
-
you cd to the folder first. tbh, you could have done it by now, using something like Quick File Renamer Lite or a dozen freeware alternatives – Tetsujin Oct 11 '15 at 06:36
-
This doesn't change the extension. To do what this software does you don't need anything and Mac itself can do it. You can select all files and then right click and rename all files. – Adrien Oct 11 '15 at 06:44
-
1I'd forgotten that's now included in Yosemite. I've had it for years as a 3rd party add-in service. So why not use that? Does it have to be done in terminal? Otherwise it's just Replace Text > .html > .txt Finder prefs > advanced if you want to switch off the change extension warning. – Tetsujin Oct 11 '15 at 06:57
-
4Do you just want to change the extension or do you want to remove all HTML formatting as well? – nohillside Oct 11 '15 at 07:15
3 Answers
1
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:
- Looks up every file (
-type f) in the current folder (.) whose name is ends with ".html" (-name "*.html") - 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. - 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 offindthe backticks are easier to use.
bot47
- 7,742
-1
for-loop
for file in *.html; do
mv "${file}" "${file%.html}".txt
done
This 'for loop' is just one way to do it.
CousinCocaine
- 10,098
-1
rename
You can use rename which you can install using brew.
Install rename using brew: brew install rename
Rename files using rename:
rename -s .html .txt *
CousinCocaine
- 10,098