Reputation: 21
I'm doing an assignment on terminal commands in Ubuntu. The problem I'm currently stuck on asks me to create a text file in a directory I'm not currently in, and add text to it, all using one command. I was trying to run it as:
touch /home/user/Desktop/index.html
echo "text" > index.html
...
but keep getting errors.
Upvotes: 1
Views: 9359
Reputation: 101
Also you can use printf
:
printf 'Hello\nworld' > /home/user/Desktop/index.html
Upvotes: 2
Reputation:
cat > /the/directory/your_file
hello world!
foo bar
baz
^D
comments:
cat /the/directory/your_file
, (NOTE: no redirection operator '>' here!), you will see the contents of the file you just created.Upvotes: 1
Reputation: 706
You also need to specify the path when writing "text" into the file:
touch /home/user/Desktop/index.html ; echo "text" > /home/user/Desktop/index.html
Also, there's no need to touch the file first. The >
operator will automatically create the file if it doesn't exist, so you can just type:
echo "text" > /home/user/Desktop/index.html
Upvotes: 3