Low Orbit Flux Logo 2 D

Linux How To Create A File

Creating a file is easy on Linux. There are many different ways you can do this and we are going to cover some of them here.

The most common way to create a file on Linux is to use the touch command like this. It creates an empty file. If the file already exists it will update the time on the file.


touch test1.txt

You can touch multiple files at once like this.


touch a.txt b.txt c.txt

You can also echo text to a file like this. This will create the file if it doesn’t exist. If the file exists it will be overwritten.


echo "some text for my file" > test1.txt

You can append to an existing file like this. It will create the file if it doesn’t exist yet.


echo "some text for my file" >> test1.txt

If you echo nothing it will create an empty file ( or wipe out an existing file ).


echo > test1.txt

You can also just use the redirect operator directly like this. It will create or overwrite the file specified.


> test1.txt

You can write multiple lines to a file like this. It will read in what ever you type, line at a time until you type [CTRL]-D.


cat > test1.txt
Line of text.
Another line of text.
CTRL-D

You can use the following to read in text line at a time until it encounters EOF. It will then create/write/overwrite the file.


cat << EOF > test1.txt
Line of text.
Another line of text.
EOF