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.

3
  • 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. Commented Apr 25, 2020 at 13:37
  • Check this for counting the files: stackoverflow.com/q/2632205/11024053 Commented Apr 25, 2020 at 13:44
  • Thanks but I am more of a copy paster, that page just confused me more. Commented Apr 26, 2020 at 11:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.