0

I have 8k+ text files that I would like to rename with the first line of text of the respective file. Most lines are up to three words but some are more complex, with commas and other punctuation, e.g. "My Milk(also known as Annie Ps 'Tell The Milk' ".

I have used this:

for i in *.txt; do mv "$i" "$(head -1 "$i")".txt; done

Only issue I am having is files getting overwritten. I guess I will have to append a sequence to mitigate this.

user57
  • 1
  • 2
  • You can write a python script that iterates through the whole directory, open each file (in read mode), save the first line in a variable, close the file, rename the file and repeat. – faressalem Apr 25 '20 at 13:37
  • Check this for counting the files: https://stackoverflow.com/q/2632205/11024053 – faressalem Apr 25 '20 at 13:44
  • Thanks but I am more of a copy paster, that page just confused me more. – user57 Apr 26 '20 at 11:22

0 Answers0