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

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

touch file
touch /var/rex.red/file1

>file

Simply created an empty file with the name you write.

>file

or in a directory

>/var/rex.red/test4

echo

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

echo -n >file
echo >/var/rex.red/file

printf

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

echo 'file contents' > file

printf

printf 'file contents\ntest\n' > file

ls > file

ls -lh /var/log/ > logfiles

cat >

cat file1 > file2

vim

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

vim file1 


Comments

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.