Could anyone very kindly help me a simple way to perform the - perhaps - very trivial task of writing the name of a file as first line of that file which is in txt format?
And would be possible to do this recursively for some thousands files in the XY directory?
And, again, add to the simple filename also the folder name (say <XY/nomefile.txt>)
# place the script in the dir where filenames need to be added to the .txt file
# use mv to change to original filename if needed: mv $filename.new $filename
for filename in $(ls *.txt)
do
sed "1s/^/${filename} \n/" ${filename} > $filename.new
echo Done ${filename}
done
As you also need the directory name... Modified michaelrozar17's reply...
# place the script in the dir where filenames need to be added to the .txt file
# You can remove the comments if that's what you want...
for filename in $(ls -1 *.txt)
do
pwd=${PWD}/${filename}
awk 'BEGIN{print "'$pwd'"}1' $filename > $filename.new
#rm -f $filename
#mv $filename.new $filename
done