I am trying to add two lines of text from one file to the beginning of a bunch of other files by using a for loop in the bash command line.
I tried this in the command line:
$for i in *.txt; do head -2 ../../041_R_def_c.txt > X; cat i >> X; \mv X i; done
but it gives me the error:
cat: i: No such file or directory
I also tried to just move one line to the beginning of the files:
$for i in *.txt; do echo "#c wavelength d %4.0f angstrom"|cat - i > /tmp/out && mv /tmp/out i; done
cat: i: No such file or directory
cat: i: No such file or directory
cat: i: No such file or directory
I thought that i would carry the name of the .txt file, but I don't think that is what i is. How can I get i to carry the name of the file/play the role of the file?
In your loop,
i
is a variable, you need to access the contents of the variable with$
, so it should becat $i >> X
. Otherwise, it thinks you want tocat
the file namedi
, which doesn't exist.$i
gives you the name stored inside the variablei
.You will run into the same issue with
mv
... You will want\mv X $i