Single line create file with content

apasajja picture apasajja · Apr 12, 2012 · Viewed 12.2k times · Source

The OS is Ubuntu. I want to create file.txt in /home/z/Desktop where the content of the file is some text here.

The first and usual way is run nano /home/z/Desktop/file.txt and type some text here. after that, press ctrl+x, pressy followed by Enter.

The second way is run cat > /home/z/Desktop/file.txt, type some text here and press Enter followed by ctrl+c

I hope I can run single line of command to make it faster. I thought xdotool will work with the cat (the second way), but no, it not works

Answer

gak picture gak · Apr 12, 2012

You can use "echo" in bash. e.g.:

echo "some text here" > file.txt

If you don't want a new line character at the end of the file, use the -n argument:

echo -n "some text here" > file.txt