Home > Net >  how to create a file with line of text in Linux
how to create a file with line of text in Linux

Time:03-24

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'

CodePudding user response:

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.

CodePudding user response:

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.

  • Related