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:
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
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.
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.
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:
You have to love the command line :)
You can try using the
command.
See if you discover a pattern Sample Output:
For some files it returns:
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
This is only to answer the question!
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:
Alternatively, the following method will not preserve the original files:
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 andsed
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.
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.grep -Iq . {}
will letfind
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: