Is it possible to edit an existing text file using only one line of command and Nano. For instance if I have a text file called "new.txt" and I want to write "HELLO WORLD" in it. Can I do like this:

nano new.txt "something that will make it to write in that file "HELLO WORLD"

That is, can I write in a file using Nano without explicitly opening it in Nano. Yet only add something after the command nano (like -w for write) and the file name in the terminal.

  • Do you want to append a line to the file? If yes, then you don't need nano at all and I'm happy to provide an answer. Or do you want to create a completely new file? Please edit your question to include that information. – gertvdijk Jan 16 '14 at 15:21
  • @gertvdijk: Yes I would add at into an existing file that is append it. – Nick Jan 16 '14 at 15:33

Just use >> in your shell. This is a way of output redirection that will append to a file. In order to create the output, use the simple echo. E.g.

echo "Hello world." >> /path/to/file.txt

Alternatively, to create a new file (or to overwrite an existing one), use a single >, e.g.:

echo "Hello world." > /path/to/new/file.txt

One can use any command that create output to redirect it to a file like this. echo is just an example that outputs the argument(s) to stdout.


Try this,

cd /path/to/the/directory/where/filename.txt/is/located
echo "Hello World" >> filename.txt

>> - append lines to an existing file.If no such file was present then it automatically creates a new one.
> - overwrites an existing file or create a new one and writes standard output.

  • to append a line you don't need nano like @gertvdijk said. – Avinash Raj Jan 16 '14 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.