How to Create File in Linux
Last Updated :
15 Dec, 2023
Today, we’re going to learn about something really important – how to create files in Linux. It’s like creating a fresh piece of digital paper to write or store things. We’ll explore different ways to do this using simple commands. Whether you’re just starting out or have been using Linux for a bit, understanding how to create files will make your computer adventures a lot easier. So, let’s jump in and discover how to make new files in Linux together!
There are mainly seven ways of creating files in Linux. All of them have their own purpose and benefits. They are as follows:
1. Creating file using the `cat` command in Linux
It is the most universal command/tool for creating files on Linux systems. We cannot edit a file using the cat command. Major operations that can be done using it are as follows:
To create files and write the data into them.
cat >file1
This command creates a new file file1 (in write mode) if it doesn’t exist in the present working directory. If any file with file name file1 exists in the current directory, it is overwritten.Â
Note: After writing the text into the file, press ctrl+d to save and exit from the writing mode.
cat >file1
To view the contents of a file using cat command
cat file1
This command simply prints the contents of file1 on the terminal screen.
cat file1
2. Creating file using the `touch` command in Linux
We can create an empty file (or multiple empty files) using touch command in Linux. But its main purpose is to change or update the time-stamp of a file. Major operations that can be done using it are as follows:
Creating a file using touch in Linux
touch file2
.webp)
touch file2
Used `ls` command to list files in the current directory and used `cat` command to see the content inside a file.Â
Creating multiple files at same time using `touch` command in Linux
touch file_1 file_2 file_3 file_4
.webp)
touch file_1 file_2 file_3 file_4
How to display timestamp of files in Linux
ls -l
.webp)
ls -l
How to change timestamp of a file in Linux
For example: If we want to change the timestamp of a file_name `file_1`. We use command as follows.
touch file_1
.webp)
touch file_1
Using `ls -l` command to display timestamp of files.
3. Creating File using `vi` or `vim` command in Linux
Its main function is to edit files. It is commonly used by programmers to edit the textual content of any file on vi text editor. Major operations that can be done using it are as follows:
Note: To save and exit from the vi-text editor, press the Escape key and then type :wq and hit enter.
Creating a file using vi in Linux
vi file_1
This command creates a new file file_1 and opens it on the vi-text editor if it doesn’t exist in the present working directory. If a file with the file name file_1 exists in the current directory, then this command just opens the file on the vi-text editor.
.webp)
vi file_1
Used `ls` command to list files in the current directory and used `cat` command to see the content inside a file.Â
Creating a file using vim in Linux
This command creates a new file file_1 and opens it on the vim-text editor if it doesn’t exist in the present working directory. If a file with the file name file_1 exists in the current directory, then this command just opens the file on the vm-text editor. Vim is the update version of vi text editor.
vim file_2
.webp)
vim file_2
Used `ls` command to list files in the current directory and used `cat` command to see the content inside a file.Â
4.Creating file using `nano` command in Linux
It may/may not be found in all distributions of LINUX. We can create as well as edit files.
Note: To exit nano Text Editor press ctrl + x.
nano file_1
.webp)
nano file_1
Used `ls` command to list files in the current directory and used `cat` command to see the content inside a file.Â
5. Creating a file using `gedit` command in Linux
Linux’s users normally use the command line interface (CLI) for writing or editing the text files. But if we want to edit a text file graphically on Linux machines without learning about the powerful editors like vim and nano then gedit text editor makes it easier for us.
“gedit” stands for GNOME text editor, it’s a standard default text editor found in any system with a GNOME desktop environment including Ubuntu, Fedora, Debian, CentOS, and Red Hat. Using gedit we can create as well as write/edit the text files.
Create a file.
gedit file_2

1.

2.
This command creates a new file file_2 (in write mode) on the gedit text editor if it doesn’t exist in the present working directory. If any file with file name file_2 exists in the current directory, then it is opened (in edit mode) on the gedit text editor.
Note: To use the terminal again, press ctrl + c.
Open and edit the file.
As we create or edit a file using gedit command, by default the file gets open on the gedit text editor and wait for it to close before it returns you to the terminal prompt. If you want to use the terminal window while the gedit text editor is open, launch gedit with the following command instead.Â
gedit file_2 &
This command opens gedit text editor as a background task. We get the command line prompt back straight away and carry on using the terminal window even when gedit text editor is running.
.webp)
Open and edit the file
6. Using `mv` command to Create File in Linux
We normally use mv command to move the files or directories from one place to another in Linux systems. But we can also use it to create new files with the contents of some other file on the system.Â
mv file_2 file_3
This command crafts a new file named file_3
in the present working directory, incorporating the contents of file_2
. If file_3
doesn’t already exist, it is created. However, if a file with the name file_3
is present in the current directory, its contents are replaced with those of file_2
. Importantly, it’s crucial to note that this command essentially involves copying the content of file_2
to file_3
and subsequently deleting file_2
.
.webp)
mv command
Note: This command copies the content of file_2 to file_3 and deletes file_2.
7. Creating file using `printf` command in Linux
The printf command can be used to create a file with formatted text. To create a file using the printf command.
Open the terminal and type the following command:
printf "txt_we_want"> File_name
Example:Â
If we want to write “hello connections” and want to create a file_name “file_1”
printf "hello connections"> file_1
printf “hello connections”> file_1
Used `ls` command to list files in the current directory and used `cat` command to see the content inside a file.
Conclusion
In conclusion, mastering file creation in Linux offers versatile solutions for efficient command-line management. From the simplicity of `cat`
to advanced options like `vi`
and `vim`
, user-friendly choices such as `nano`
and `gedit`
, and even unexpected versatility in the `mv`
command, we explored a diverse toolkit. Frequently asked questions clarified distinctions between file creation and editing, providing a comprehensive understanding of command-line editors. Whether you’re a beginner or an experienced user, this knowledge equips you to navigate Linux with confidence, creating and managing files effortlessly
Similar Reads
How to Create a VIM File in Linux CMD
In Linux, there are many ways to open and create files, but Vim stands out as a powerful and versatile text editor. It comes pre-installed on most Linux systems and allows you to create, edit, and manage files directly from the terminal. In this article, we will explain you, how to create a new file
3 min read
How to Run a File in Linux
The command line is one of the most powerful tools in Linux. It allows you to execute commands, manage files, and automate tasks all from a single terminal window. One common task you'll often need to do is run a file, whether itâs a script, a compiled program, or even a text file. In this article,
6 min read
How to Open a File in Linuxâ
In Linux, a file is a fundamental unit of storage, representing everything from documents and images to system logs and program data. Unlike traditional operating systems, Linux treats almost everythingâfiles, directories, devices, and processesâas a file. Whether you're accessing a simple text docu
6 min read
How to Move File in Linux | mv Command
The `mv` command in Linux is like a superhero tool that can do a bunch of cool stuff with your files and folders. Think of it as a digital moving truck that helps you shift things around in your computer. Whether you want to tidy up your folders, give your files new names, or send them to different
7 min read
How to Rename a Folder in Linux
Renaming a folder in Linux is possible with Graphical User Interface (GUI) file managers as well as with powerful command-line utilities such as mv, rename, find, and rsync. Be it a novice utilizing Ubuntu, Debian, CentOS, Fedora, or Kali Linux or an expert dealing with bulk renaming in the terminal
12 min read
How to Compress Files in Linux | Tar Command
File compression is a fundamental task in managing and transferring data efficiently on a Linux system. The Tar command, short for Tape Archive, is a powerful tool that allows users to create compressed and archived files. In this comprehensive guide, we will explore the various options and examples
11 min read
How to Create an Empty File in Linux | Touch Command
The touch command is a standard command used in the UNIX/Linux operating system which is used to create, change and modify the timestamps of a file. Basically, there are two different commands to create a file in the Linux system which are as follows: touch command: It is used to create a file witho
8 min read
How to Create a Shell Script in linux
Shell is an interface of the operating system. It accepts commands from users and interprets them to the operating system. If you want to run a bunch of commands together, you can do so by creating a shell script. Shell scripts are very useful if you need to do a task routinely, like taking a backup
7 min read
How to Find a File in Linux | Find Command
Linux, renowned for its robust command-line interface, provides a suite of powerful tools for efficient file and directory management. Among these, the "find" command stands out as an indispensable asset, offering unparalleled versatility in searching for files based on diverse criteria. This articl
10 min read
How to Create a File in the Linux Using the Terminal?
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 commandUsing the cat commandUsing redirection operatorUsing the echo commandUsing the heredocUsing the dd c
4 min read