Title says it all. I've managed to get just the lines with this:
lines=$(wc file.txt | awk {'print $1'});
But I could use an assist appending this to the filename. Bonus points for showing me how to loop this over all the .txt files in the current directory.
Save the above
awk
script in a file, saywcmv.awk
, the run it like:It will list the commands that need to be run to rename the files in the required way (except that it will ignore empty files). To actually execute them you can pipe the output to a shell for execution as follows.
Like it goes with all irreversible batch operations, be careful and execute commands only if they look okay.
This would work, but there are definitely more elegant ways.
Result would put the line numbers nicely before the
.txt
. Like:you can do like this as well:
example:
You can adjust maxdepth accordingly.
Another way with awk to manage easier a second loop by allowing more control on name (like avoiding one having already the count inside from previous cycle)
Due to good remark of @gniourf_gniourf:
Use rename command