How to create a file in Linux using Terminal or SSH

How to create a file in Linux using Terminal, Command line interface or SSH.

There are a few ways to create a new empty file or redirect the output of a command to a new file. To create an empty file, any of these commands can accomplish that without having to install any extra packages or modules.


Touch creates an empty file in the path you specify. Easy

touch file
touch /var/


Simply created an empty file with the name you write.


or in a directory



You can also add text with echo but this creates a new empty file.

echo -n >file
echo >/var/


Same idea as with echo but using prinf if you prefer it like this.

printf ''>file

Create file from text output

Create non-empty file

If you need to save a file but with contents it’s still pretty easy.


echo 'file contents' > file


printf 'file contents\ntest\n' > file

ls > file

ls -lh /var/log/ > logfiles

cat >

cat file1 > file2


Create the file and edit it. Insert key – When finished save it :wq to save and exit vim.

vim file1 


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.