How to Create a New File in Linux from Bash?


Before getting into the ways of creating a file using Bash, let's first understand how Linux treats its files. Linux organizes all its data into files and files are organized into directories. Further, the directories are organized into tree-like structures called the file system. When you have to work in a Linux environment, you would definitely have to spend a lot of your time working on different types of files.

There are various ways in which one can create a file in Linux. You can create a file from the Bash Shell or you can use the Desktop File Manager to do so. In this article, we will focus on different Shell commands that you can use to create a file.

You can use any of the following five commands to create a new file in Linux −

  • The "touch" command

  • The "cat" command

  • The Redirection operator

  • The "echo" command

  • The "printf" command

We will elaborate each of these commands one by one. Let's start with the "touch" command.

Using the "touch" command

The touch command is by far the most frequently used command for creating a new file in Linux. To create a new file, you need to run the touch command followed by the name of the file. For example,

$ touch hello.txt

It will create an empty file called "hello.txt" in the current directory. Use the "ls" command to verify if the file has been created or not.

Using the "cat" command

Normally we use the "cat" command to read the contents of a file; however, we can also use this command to create a new file. Let's see how.

To create a new file, run the "cat" command and then use the redirection operator ">" followed by the name of the file. Now you will be prompted to insert data into this newly created file. Type a line and then press "Ctrl+D" to save the file.

$ cat > secondFile.txt
Welcome to Tutorialspoint!

The above command will create a new file called "secondFile.txt" and save it with the content "Welcome to Tutorialspoint".

Again, use the "ls" command to verify if the new file has been created or not.

$ ls
hello.txt newdirectory secondFile.txt

Next, use the "cat" command to see the contents of "secondFile.txt".

$ cat secondFile.txt
Welcome to Tutorialspoint!

Using the Redirection Operator

You can simply use the redirection operator ">" to create a new blank file in the current directory. Run the ">" operator followed by the name of the file.

$ > thirdFile.txt

Now use the "ls" command again to verify −

$ ls
hello.txt newdirectory secondFile.txt thirdFile.txt

Note that the ">" operator overwrites the contents of a file if it is already present. For example, the following command will overwrite the contents of "secondFile.txt" because the file already exists and we know it contains the line "Welcome to Tutorialspoint!"

$ > secondFile.txt

Now use the "cat" command to check the contents of "secondFile.txt".

$ cat secondFile.txt

It will display nothing because the file is now empty.

You can use the redirection operator ">>" to append the contents of a file into another. For example,

$ cat hello.txt
This is the first file.
$ cat secondFile.txt
This is the Second File.

Now we can use the following command to append the contents of "secondFile.txt" at the end of "hello.txt".

$ cat secondFile.txt >> hello.txt
$ cat hello.txt
This is the first file.
This is the Second File.

Using the "echo" command

The "echo" command takes a string as argument and displays it as output. For example,

$ echo "This is the Fourth File"
This is the Fourth File

We can redirect this output to a new file, such as −

$ echo "This is the Fourth File" > fourthFile.txt

The above command will create a new file (or overwrite the file if it already exists) with the string passed as the argument to "echo". Verify using the "cat" command −

$ cat fourthFile.txt
This is the Fourth File

If you simply want to create a blank new file, use the "echo" command without any argument −

$ echo > fourthFile.txt

Using the "printf" command

The "printf" command works just like the "echo" command with the only exception that the "printf" command provides additional formatting options that you can use to pass a formatted string as the argument.

The following "printf" command redirects the input formatted string into a new file "fifthFile.txt". If the file already exists, then it will overwrite its contents.

$ printf "First Line.
Second Line.
" > fifthFile.txt $ cat fifthFile.txt First Line. Second Line.

Using the Vim Editor

Besides the above-mentioned Bash commands, you can consider using the screen-oriented text editor "Vim" to create a file. Vim is the improved version of the "vi" editor which is generally considered as the de facto standard in Unix editors. You can use the vim editor to create a new file, edit an existing file, or simply read a text file.

To open the Vim editor, type "vim" at the command prompt. You will get to see a screen like the one shown below:

In the Command mode, type "vi abc.txt" and press Enter to create a new text file called "abc.txt".

Next, press "i" to enter the Insert mode to insert some data into this newly created file.

Now it's time to save the file. Press "Esc" to return to Command mode and then use the command ":wq" to save the file and quit. It will take you back to the console.

Next, on the console, type the command "vim abc.txt" to open the newly created file for reading or editing:

It will open the file "abc.txt" for reading or editing:

To learn more about the Vim editor and its various options, you can check this page.

Linux File System

One of the key features of Linux is its file system, which is different from those found in other operating systems like Windows or MacOS

The Linux file system is structured in a hierarchical manner, with all its files and directories organized under a single root directory. The root directory is denoted by a forward slash (/) and it is the top-level directory in the file system hierarchy. All other directories and files are located within this root directory

Directories in Linux are similar to folders in other operating systems. They can contain files, subdirectories, or both. In Linux, directories are also files, but they differ from regular files in that they contain a list of other files and directories.

The root directory contains several important directories, such as −

/bin − It contains executable files that are essential for the functioning of the system. These files are used by all users, including the root user.

/boot − It contains the files that are required for the boot process, such as Linux kernel and boot loader

/dev − It contains the device files that represent hardware devices connected to system, such as disk drives and printers.

/etc − It contains the configuration files for system and applications

/home − It contains the home directories for the users on the system.

/lib − It contains the libraries that are used by the system and applications.

/mnt − It is used for temporarily mounting the file systems, such as CD-ROMs or USB drives

/proc − It contains the data about the processes running on the system.

/root − This directory is the home directory for the root user.

/sbin − It contains the executable files that are used by the system administrator (root) to perform system maintenance tasks.

/tmp − It is used for temporary files created by applications or system.

/usr − It contains user-related files, such as applications, libraries, and documentation.

/var − It contains variable data files such as log files and spool directories.

File Types in Linux

In Linux, files are classified into several types based on their purpose and contents. The most common file types in Linux are −

Regular Files − These are normal files that contain data such as text files, images, or audio files. Regular files can be read, written, or executed.

Directories − Directories are special files that contain a list of other files and directories.

Symbolic Links − These files are used to create a shortcut or alias to another file or directory.

Character Devices − These files represent hardware devices that transfer data one character at a time, such as serial ports and terminals.

Block Devices − These files represent hardware devices that transfer data in blocks, such as hard disk drives and CD-ROMs.

Named Pipes − These files are used to transfer data between processes or programs.

Sockets − These files are used for communication between processes over a network or a local machine.

In order to work effectively on a Linux platform, it is important that you understand the Linux file system structure and how to use different types of files.

Conclusion

In this brief tutorial, we highlighted five different ways that you can use to create a new file in Linux from the terminal. In case you don't have Linux on your system, then use Windows Subsystem for Linux (WSL) to create a Linux environment on your Windows system. In addition, we explained in brief the Linux File System and the File Types in Linux.

Updated on: 16-Apr-2024

83K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements