7

i have looked everywhere and im feeling hopeless

my file is this

 mkdir -p ~/Desktop/new_file && echo "hello\\world > ~/Desktop/new_file.txt

I've also tried

 hello\n world

 hello ; world

I'm trying to accomplish a return signal to my hello world output for a multi line sentence all from the terminal

7

The man page for echo shows the following:

DESCRIPTION
       Echo the STRING(s) to standard output.

       -n     do not output the trailing newline

       -e     enable interpretation of backslash escapes

Which means that if you want multi-line output, you would simply start with echo -e and would add in a \n for new line.

mkdir -p ~/Desktop/new_file && echo -e "hello\nworld" >> ~/Desktop/new_file/new_file.txt

Hope this helps!

5
  • its a text file inside a directory with hello world inside the text file
    – user610658
    Mar 26 '17 at 4:03
  • @hellomoto Ah, OK. In your example, you are not writing into the new folder.
    – Terrance
    Mar 26 '17 at 4:04
  • @hellomoto Updated my answer for you.
    – Terrance
    Mar 26 '17 at 4:05
  • thank you Terrance, so its better to use >> instead of >
    – user610658
    Mar 26 '17 at 4:07
  • @hellomoto >> and > will both create the file. However, > will overwrite the file if anything is in it and you want to create a new one. >> will append to the file if it already exists. If it does not, it will create one. Usually, I like to use >> if I am putting multiple lines in a file as to not accidentally overwrite all the lines that were just barely added. If you did a loop file that kept reading lines, the only line you would see with > would be the very last time it looped through. So, you can make the choice.
    – Terrance
    Mar 26 '17 at 22:24
3

First, although your title mentions touch, the command you have actually used is mkdir so you have created a directory called new_file. You will not be able to write text to new_file as-is.

In fact there's no need to create the target file in a separate step: redirecting a command's standard output to a named file will create it automatically if it does not already exist. You can remove the (empty) new_file directory using rmdir ~/Desktop/new_file


For the reasons outlined here Why is printf better than echo? you might want to consider instead using

printf 'Hello\nworld\n' > ~/Desktop/new_file

or use a here document

cat > ~/Desktop/new_file
Hello
world

which allows you to enter multiline text directly, terminating the input with Ctrl+D when you're done.

In either case, you can replace > by >> if you want to append to, rather than overwrite the existing contents of, the file.

1

If I understand your question correctly you want to use:

echo "hello" > ~/Desktop/new_file.txt && echo "world" >> ~/Desktop/new_file.txt

Then to check the results use cat ~/Desktop/new_file.txt which shows:

hello
world

There's a shorter way of doing this but I'm kind of new to Linux myself.

Your Answer

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