How to add lines to end of file on Linux - linux

I want to add the following 2 lines:
VNCSERVERS="1:root"
VNCSERVERARGS[1]="-geometry 1600x1200"
to the end of the file vncservers found at the directory /etc/sysconfig/.
How can I do this?

The easiest way is to redirect the output of the echo by >>:
echo 'VNCSERVERS="1:root"' >> /etc/sysconfig/configfile
echo 'VNCSERVERARGS[1]="-geometry 1600x1200"' >> /etc/sysconfig/configfile

Related

how to create a file with line of text in Linux

Create a file bash/alx with these two lines inside: #!/bin/bash and echo "ALX" in linux
I have tried this:
touch bash/alx
touch '#!/bin/bash'
touch is a command that creates or updates the timestamp on a file.
echo foo outputs foo to stdout.
If you use output redirection, echo foo > bar puts foo into the file bar.
Combining these will give you your solution.
touch /path/t/myFileName
echo "text for file" > /path/to/myFileName
This page has some good information.
To create a file in Linux using touch,the syntax is:
touch /path/to/your/file
touch alx.txt # E.g
You can echo something in a file using (by default echo output to stdout):
echo content > file
# or
echo content >> file
The former override the file content if it already exist or create it while the latter will append your content to the file if it exists or create it too.
Thus you can use echo directly without needing to use touch to create the file before.
And as you can pass options to Linux commands, echo has -e that instructs it to interpret special chars like \n in your string. So you can send multiple lines to a file with echo -e "line1\nline2\n..." > somewhere.txt.

How to write end of file with new line in linux?

I want to write, a script output into logfile at the end with new line..
with this command, it replace the file.
echo "hai" > /tmp/syslog.txt
but I need add "hai" with existing file content at the end with new line..
thanks in advance..
just simple like this:
echo "hai" >> /tmp/syslog.txt
:)
Using option \n – New line with backspace interpreter -e treats new line from where it is used.
echo -e "\nHai" >> /tmp/syslog.txt

How to echo command output to end of existing line in Linux?

I am trying to automate the building of a config file and most of what I need can do done via a straight echo 'text here' >> myfile.conf
What I am having trouble with is appending the output of a command to the last line in the file.
My last working command is echo 'masternodeprivatekey=' >> myfile.conf
My next command is ./wallet-cli masternode genkey &>> myfile.conf
I need to have the masternode key generated to be included on the same line as masternodeprivatekey= so that the line will end up as this -
masternodeprivatekey=abcd13def456ghi789
Can this be done?
I have seen ppl suggest adding /c to the end of the echo command or -n to the end of the line however these don't work, they just add those characters to the end and still post the output of ./wallet-cli masternode genkey &>> myfile.conf on the next line.
Is it possible to do what I want? I'm pretty sure I could echo the output to two different temp files and then combine them, but is there an easier way?
You can use echo with option -n , it do not output the trailing newline
echo -n 'masternodeprivatekey=' >> myfile.conf
In sed, you can adress the last line with $
echo 'masternodeprivatekey=' >> myfile.conf
sed -i -r "$ s/(.*)/\1$(($RANDOM%100000))/" myfile.con
Check, whether your sed has -i and -r parameters. This worked with GNU-sed.
Since I don't know ./wallet-cli, I used something else in $(...) instead, to perform a command which generates output. So for you, it might be
sed -i -r "$ s/(.*)/\1$(./wallet-cli masternode genkey)" myfile.con
Since you seem to know before, that you will add something after the equal sign, why don't you do:
echo "masternodeprivatekey=$(./wallet-cli masternode genkey)" >> myfile.con
As per Mark Plotnick's suggestion, the use of printf instead of echo works perfectly.

Append new line in all files of the folder

I have 300 files in a folder. I have to append one new line at the end of all files in a folder.
How can i achieve it using grep.
I tried the following command but its not working
sed 's/$/\n/' /Path/filename.txt
Just say echo "" >> file. This will append a new line at the end of file.
To do it in all the files in the folder:
for file in *
do
echo "" >> "$file"
done
From the comments, in your case you have to say:
for file in /path/*.txt
do
echo "" >> "$file"
done

What is a command to create a file on Linux?

For testing purposes I have to create a file with 1000 lines in it with one command.
What is a command to create a file on Linux?
touch is usually used to create empty files, but if you want to create a non-empty file, just redirect the output of some command to that file, like in the first line of this example:
$ echo hello world > greeting.txt
$ cat greeting.txt
hello world
A way to create a file with 1000 lines would be:
$ seq 1000 > file
for x in `seq 1 1000`; do echo "sometext" $x >>file.txt; done
you use the above for loop use print something $c times in text.txt.$c is number of runnning time (1,2,4...10000)
for (( c=1; c<=1000; c++ ));do echo "something $c times">>test.txt ;done

Resources