How to Create a File in the Linux/Unix system using terminal?

LinuxOperating SystemOpen Source

In the Linux/Unix operating system there are two ways available to creating files.

  • Using the touch command
  • Using the cat command

Creating a File using 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 a terminal.

Syntax

The general syntax of the touch command is as follow −

$ touch [option] ... FILE...

Brief description of option available in the touch command.

Sr.No.Option & Description
1-a
Change the access time of file
2-c,--no-create
Check file is available or not if not then pervert to create a new file
3-d, --date=STRING
Set time of file instead of current time
4-f
Ignored
5-m
Change the modification time of file
6-r, --reference=FILE
Use another file time instead of current time
7-t STAMP
Use specified time instead of current time
8--help
Displays a help message and then exits.
9--version
It gives info about the version and then exits.

Here, using the touch command we can create a file in the Linux system. Before executing touch command, we will check how many files are available in our current directory using the below command.

$ ls -l

After using below command a new file created newfile.txt in the current directory.

$ touch newfile.txt

To ensure that file is created or not we will again execute ls command to list the directory contents.

To change the access time of a file in the Linux system using -a option with touch command as shown in below.

$ touch -a newfile.txt

Creating a File using 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 files in the Linux system without content whereas the cat creates files with some content. The cat command reads the content of a file and prompts it.

Syntax

The general syntax of the cat command is as follows −

$ cat [option]... FILE...

Brief description of option available in the cat command.

Sr.No.Option & Description
1-A, --show-all 
Show all content of a file
2-b, --number-nonblank
Display number of non-empty lines, overrides -n
3-n, --number
Display number of all output lines.
4-T, --show-tabs
Display tab separated Lines
5--help
Displays a help message and then exits.
6--version
It gives info about the version and then exits.

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 in below.

$ cat >file.txt
Hey, write some contents here...
^C

The cat command is also used to view the contents of the file. After using cat command along with file name contents of the file will be prompt as shown in below.

$ cat file.txt
Hey, here your contents...
raja
Published on 30-Jun-2021 11:06:28
Advertisements