I am trying to write a one-liner to append a row count to the filename of each file in a directory before the extension. I thought I could do this with wc -1 but I am struggling to get it in a variable to append to the filename.

for file in *.txt ;do rowcount="wc -l < $file" mv $file ${file%.txt}_$rowcount.txt; done
  • Almost there, try for file in *.txt ;do rowcount=$(wc -l < "$file") ; mv "$file" "${file%.txt}_$rowcount.txt"; done – icarus Feb 1 at 7:11

Your Answer

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

Browse other questions tagged or ask your own question.