Essentially you need to loop through every text file in a directory, then add a small string to the start. It's not hard when you break it down into two steps.
To add the string to the beginning of a file, the format would be this:
echo "50 $( cat file.txt )" > file.txt
If you get a cannot overwrite existing file
error, you need to disable noclobber
set +o noclobber
Now you just need to drop that in a loop that loops through the files you want to alter.
for FILE in $( ls /mydata/ | grep txt$ ) ; do
echo "${FILE}"
done
If that outputs all the files you want to alter correctly, add in the command to alter the files (as below). If not, alter the grep statement until it matches what you need.
for FILE in $( ls /mydata/ | grep txt$ ) ; do
echo "50 $( cat ${FILE} )" > "${FILE}"
done
And if you want it done in one line:
for FILE in $( ls /mydata/ | grep txt$ ) ; do echo "50 $( cat ${FILE} )" > "${FILE}"; done
Edit
If you want the 50 on its own line, use printf along with \n
instead of echo, as below.
for FILE in $( ls /mydata/ | grep txt$ ) ; do printf "50\n$( cat ${FILE} )" > "${FILE}"; done