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
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/rex.red/file
Same idea as with echo but using prinf if you prefer it like this.
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 file1 > file2
Create the file and edit it. Insert key – When finished save it :wq to save and exit vim.