What is a file? A file is a computer resource that records data discretely in a storage device. There are many ways that you can create a file in Linux. You can use the file manager or the terminal to create a file in Linux.
There are many reasons why you would want to create a file in Linux. You may want to create an empty file as a placeholder for something that you plan to enter into it later on.
You create files from the command line or using GUI of the file manager.
Linux terminal commands to create a file in Linux
1. vi command
vi is a text editor, that can create and edit files. vi is one of text editors that come pre-installed with most of the Linux distros. Other text editors that you can use instead of vi to create a file in Linux are: nano, emacs etc.
To create a file using vi, simply run vi followed by the filename.
$ vi filename
2. Touch command
Touch command is one of the easiest ways to create a file in Linux. You can type ‘touch test’ to create an empty file in whichever directory you are currently in. The file modification date can also be changed of a file that already exists by using Touch command.
You can also create multiple files at once by typing
$ touch test1 test2 test3
3. Echo command
Echo command is usually used to print strings on the terminal. But we can also redirect the input to another file or create a new file altogether using Echo command.
To create a new file using echo command, run the echo command followed by the text you want to print and use the redirection operator ‘>’.
$ echo “text” > fielname
4. Cat command
Cat command is mainly used to read files but it can also be used to create a file in Linux.
To create a new file run the cat command followed by redirection operator ‘>’ and the name of the file.
$ cat > filename
5. Redirection operator
Redirection operator allows you to capture the output from one command/file and push it as input to another command/file.
To create a new file enter this into your terminal
$ > filename
This is the shortest command to create a file in Linux.
6. Printf command
Printf command is mainly used to print the given string or any other format on the terminal window. It works the same on terminal as in other programming languages. Printf can have format specifiers, escape sequences or ordinary characters.
To create a new file run printf command followed by the data you want in it, then use the redirection operator followed by the filename.
$ printf “this is a test\n” > filename
7. dd command
dd command can be used to make big files for testing purposes. It is primarily used to copy and convert files.
To create a 1GB file using dd command write the following in the terminal
$ dd if=/dev/zero of=bigfile bs=1 count=0 seek=1G
8. Fallocate command
Fallocate is generally used to allocate real disk space for files.
To create a 2GB file using fallocate command write the following in the terminal
$ fallocate -l 2G filename
You can use all of these commands to create a file in Linux or you can just right click in your file manager and create a file that way.