I have many files inside a folder with .txt extension. Now they are named randomly by an app but I want to rename each text file to some words written in those files itself after words Name: but before word Age:.


A file named 13765_46748.txt contains:

This is just a test file containing info of each persons.
Name: Pearson Bratts Age: 55 Profession:Technician

Now I want the above file to be renamed to Pearson Bratts.txt using Tasker or any other script app with working script.


The complete shell script follows. You need a Terminal emulator, but I'll leave that to you.

if [ "$1" == "$(cat /dev/null)" ]; then
 echo "Usage: $0 <file>"
 return 1

content="$(cat '$1')"
content="${content#*Name: }"
content="${content// Age*/}"

mv "$1" "$content.txt"

This is to be saved in a something.sh, which, when executed, will require an additional parameter. For example, we'll assume you'll call it renamer.sh, and thus the usage will be:

renamer.sh some/file.txt

. It'll rename the file you give it, accordingly to its content. To give it the correct file, is your responsibility, and it works for only a file per execution. It won't work if the file has spaces inside its name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.