How do I change all plain text files to .txt files in a directory? Is there such a command?

I have tried rename command but didn't succeed.


Assuming your directory is filled only with simple text files the most direct way to give all files and extension of .txt is to open a Terminal window in the directory and run the following command:

for f in *; do mv "$f" "$f.txt"; done

You have to love the command line :)


You can try using the

file -i NameofFileGoesHere

command.

File man page

See if you discover a pattern Sample Output:

file.mp4: application/octet-stream; charset=binary                                                       
filenumplayer1: inode/x-empty; charset=binary                                                          
foobar.ogg: application/octet-stream; charset=binary 

For some files it returns:

headstart.save: text/x-shellscript; charset=us-ascii

You are looking for files marked "ascii" But they could be Really any kind of file. Especially if it is a file that was not created by you. It would not be a good idea to rename it.

But if all files are indeed personal files. Based on the above info I would try the following script

#!/bin/bash
for f in * 
do
#Looking for the string us-ascii hence this command
whatis=$(file --mime "$f" | awk -F "=" '{print $2}')
if [[ $whatis == "us-ascii" ]]
then
mv "$f" "$f.txt"
fi
done

This is only to answer the question!