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.

Asked By: fedos 3d


Here are two ways to do this.

First, the safe way will create a copy of the files with the txt extension and will preserve the original files:

IFS=$'n'; for i in $(file * | grep text | grep -v ".txt|exec" | sed 's/:.*$//g'); do cp -- "$i" "$i".txt; done

Alternatively, the following method will not preserve the original files:

IFS=$'n'; for i in $(file * | grep text | grep -v ".txt|exec" | sed 's/:.*$//g'); do mv -- "$i" "$i".txt; done

Both of these methods use the file command to determine which files are ASCII text files. Then, grep filters the results to only include the files that are "text". Next, grep -v ".txt|exec" excludes the files that already contain the ".txt" file extension and shell script executables and sed isolates the file names to give us a clean list of the files.

The first method uses the cp command to create a copy of each file with the ".txt" extension.

The second method uses the mv command to move each filename to a new name with the ".txt" extension.

I also used an input field separator at the beginning of the loop to include files with blank spaces.

Additionally, it won’t work properly if a filename contains a newline or colons.

Answered By: mchid

This will move all text-files in current directory to .txt if they not already have that extension.
You can change or remove -maxdepth 1 to make it recursive.

find . -maxdepth 1 -type f 
  -exec grep -Iq . {} ; 
  -exec sh -c '
      test "$1" != "${1%.txt}.txt" 
      && mv -i -- "$1" "${1%.txt}.txt"
  ' find-sh {} ;

grep -Iq . {} will let find skip binary files (via).
You don’t really need that test part, but then, mv will throw an error message.

Alternative if you want it recursive:

grep -rIlZ . . 
| xargs -0 -I{} sh -c '
      test "$1" != "${1%.txt}.txt" 
      && mv -i -- "$1" "${1%.txt}.txt"
  ' xargs-sh {}
Answered By: pLumo

You can try using the

file -i NameofFileGoesHere


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: 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

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

This is only to answer the question!

Answered By: wirtqdrotfnv

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 🙂

Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.