In this article, we will learn to create a file in the Linux/Unix system using the terminal. In the Linux/Unix system, there are the following ways available to creating files.
- Using the touch command
- Using the cat command
- Using redirection operator
- Using the echo command
- Using the heredoc
- Using the dd command
1. Create a file in the Linux/Unix system using the touch command.
The touch command is used to create file/files without any content and update the access date or modification date of a file or directory in the Linux system. This is the simplest way to create a file in Linux/Unix using the terminal.
The general syntax of the touch command is as follows:
$ touch [option] ... FILE...
A brief description of options available in the touch command.
|-a||Change the access time of a file|
|-c, –no-create||Check file is available or not, if not available then prevent creating a file|
|-m||Change the modification time of a file|
|-t STAMP||Use specified time instead of the current time|
|–help||Display help and exit|
|–version||Display the version information and exit.|
Create a file using the touch command in Linux/Unix system.
In this example, using the touch command we can create a file in the Linux system. Before executing the touch command, we will check that how many files available in our current directory using the below command.
$ ls -l
After using the below command a new file created newfile.txt in the current directory.
$ touch newfile.txt
To ensure that the file is created or not we will again execute the ls command to list the directory contents.
2. Create a File in the Linux/Unix system using the cat command.
The cat (concatenate) command is used to create, view, concatenate files in the Linux operating system. The touch command is also used to create a file in a Linux system without content whereas the cat creates files with some content. The cat command reads the content of a file and prompts it.
The general syntax of the cat command is as follows:
$ cat [option]... FILE...
A brief description of options available in the cat command.
|-A, –show-all||Show all content of a file|
|-b, –number-nonblank||Display number of non-empty lines overrides -n|
|-n, –number||Display number of all output lines|
|-T, –show-tabs||Display this help and exit|
|–help||Display this help and exit|
|–version||Display version information and exit|
Create a file with some content using the cat command in Linux/Unix system.
To create a file with some content, we use the cat command and file name after that write some content and press CTRL + C when writing is complete as shown below.
$ cat > file.txt
Display contents of the files using the cat command in the Linux system.
The cat command is also used to view the contents of the file. After using the cat command along with the file name contents of the file will be prompt as shown below.
3. Create a file in the Linux/Unix system using a redirection operator.
In the Linux/Unix system a redirection operator is also used to create a file.
$ > file.txt
4. Create a file in the Linux/Unix system using the echo command.
The echo command is also used to create a new file in the Linux system.
Create a new file without contents in the Linux system using the echo command.
To create a file without contents, we use the echo command with a redirection operator followed by the file name as shown below.
$ echo > file.txt
Create a new file with some contents in the Linux system using the echo command.
To create a file with some contents, we use the echo command followed by the text, a redirection operator, and the file name as shown below.
5. Create a file in the Linux/Unix system using heredoc.
heredoc stands for here document. The heredoc delimiter is a type of redirection. It allows passing multiple lines of input to a command.
The general syntax of heredoc. Important
Command << Heredoc_delimiter multiple lines contents... heredoc_delimiter
Create a file with multiple lines of contents using a heredoc delimiter in the Linux system.
To create a file using heredoc, we use the cat command with heredoc delimiter in the Linux system as shown below.
$ cat << heredoc_delimiter < file_name
6. Create a file in the Linux/Unix system using the dd command.
The dd command is mainly used to converts and copy files. To check more details about the dd command. We can also create a large file using the dd command.
Create a large file in the Linux system using the dd command.
To create a large file, we use the dd command as shown below.
$ dd if = /dev/zero of = file.test bs =1 count =0 seek = 2G