- 4 Ways to Create a Text File in Linux Terminal
- Create file in Linux command line
- 1. Create an empty file using touch command
- 2. Create files using cat command
- 3. Create new file using echo command
- 4. Create a new file using a text editor like Nano or Vim
- How To Create A File In Linux: Touch, Cat, Echo, Printf Command
- Types of files in Linux
- How to identify the type of file?
- Create a new file in Linux
- Create a new file using Terminal Window
- 1. Using the touch command
- 2. Using the cat command
- 3. Using the redirection operator
- 4. Using the echo command
- 5. Using the printf command
- 6. Create a zip file and ignore the directory structure in Linux
- Summary
4 Ways to Create a Text File in Linux Terminal
In this Linux beginner series, you’ll learn various methods to create a file in Linux terminal.
In this Linux beginner series, you’ll learn various methods to create a text file in Linux terminal.
If you have used the desktop oriented operating system such as Windows, creating file is a piece of cake. You right click in the file explorer and you would find the option of creating new file.
Things won’t look the same when you are in a command line environment. There is no right click option here. So how do you create a file in Linux then? Let me show you that.
Create file in Linux command line
There are various ways of creating a new file in Linux terminal. I’ll show you the commands one by one. I am using Ubuntu here but creating files in Ubuntu terminal is the same as any other Linux distribution.
1. Create an empty file using touch command
One of the biggest usages of the touch command in Linux is to create a new empty file. The syntax is super simple.
If the file doesn’t exist already, it will create a new empty file. If a file with the same name exists already, it will update the timestamps of the file.
2. Create files using cat command
Another popular way of creating new file is by using the cat command in Linux. The cat command is mostly used for viewing the content of a file but you can use it to create new file as well.
You can write some new text at this time if you want but that’s not necessary. To save and exit, use Ctrl+D terminal shortcut.
If the file with that name already exists and you write new text in it using the cat command, the new lines will be appended at the end of the file.
3. Create new file using echo command
The main use of the echo command is to simply repeat (echo) what you type on the screen. But if you use the redirection with echo, you can create a new file.
To create a new file using echo you can use something like this:
echo "This is a sample text" > filename.txt
The newly created filename.txt file will have the following text: This is a sample text. You can view the file in Linux using cat or other viewing commands.
You are not obliged to put a sample text with echo. You can create an (almost) empty file using the echo command like this:
This will create a new file with just one empty line. You can check the number of lines with wc command.
4. Create a new file using a text editor like Nano or Vim
The last method in this series is the use of a text editor. A terminal-based text editor such as Emacs, Vim or Nano can surely be used for creating a new file in Linux.
Before you use these text editors, you should make sure that you know the basics such as saving an existing from the editor. Unlike the GUI tools, using Ctrl+S in the terminal won’t save the file. It could, in fact, send your terminal into a seemingly frozen state from which you recover using Ctrl+Q.
Let’s say you are going to use Vim editor. Make sure that you are aware of the basic vim commands, and then open a new file with it like this:
What’s your favorite command?
So, I just shared 4 different ways of creating a file in Linux. Personally, I prefer using touch for creating empty file and Vim if I have to edit the file. On a related note, you may want to learn about the file command in Linux that is helpful in determining the actual type of the file.
Which command do you prefer here? Please share your views in the comment section below.
How To Create A File In Linux: Touch, Cat, Echo, Printf Command
A file is a container in a Linux-based system for storing information. Linux considers everything as a file and organizes all its data into files. Files are then organized into directories. Further, the directories are organized into tree-like structures called the filesystem. Partitions, hardware device drivers, and directories are all included in the definition of a file in addition to file creation, text files, file images, file details, and compiled programs.
Types of files in Linux
A file type helps us in identifying the type of content that is saved in the file. Linux supports six different types of files.
The different types of names of files present are :
Regular file: Regular or ordinary files store data of various content types such as text, audio, video, images, scripts, and programs. In Linux, regular files can be created with or without an extension.
Directory file: File systems use directories to organize files in a hierarchy. Directories are also files, but instead of storing data, they store the location of other files. Each directory entry stores the name and location of a single file.
Link file: Link files allow us to use a file with a different filename and from a different location. For this, we use link files. A link file is a pointer to another file. There are two types of links: a hard link and a symbolic or soft link.
Special file: Linux treats all hardware devices (such as hard drives, printers, monitors, terminal emulators, and CD/DVD drives) as special files. Linux places all special files or device files under the /dev directory. There are two types of special files: a character special file and a block special file.
Socket file: A socket is a communication endpoint that applications use to exchange data. Each application that provides services to other applications or remote clients uses a socket to accept connections. Each socket has an associated IP address and port number that allow it to accept connections from clients.
Named pipe file: Named pipe files are empty pipe files. The kernel processes named pipe files without writing them to the file system. Named pipe files can exist anywhere in the file system. They are also called the FIFO (First In First Out) files.
How to identify the type of file?
There are many ways to identify the type of file in Linux. The easiest way is to use the file command. To find the type of a file, specify the names of files as an argument in the current directory. Open your terminal window and run the following command:
The output of this command not only displays the type of the specified file in the current directory but also shows the type of content stored in the specified file.
Create a new file in Linux
There are various ways in which one can create a file in Linux with different names of files. You can create a file from the Terminal Window or you can use the Desktop File Manager to do so. You can create various files in Linux such as plain text files, file with echo, file with cat, file with ownership, file with printf, file with redirect, file without contents, sorted file, test6 files, and so on.
Create a new file using Terminal Window
1. Using the touch command
The touch command is the most commonly used command for creating a new file in Linux. To create a new file in the current directory, you need to run the touch command followed by the name of the file.
Command: $ touch abc.txt
This will create a file with the file name abc.
2. Using the cat command
Usually, we use the cat command to read the contents of a file; however, we can also use the cat command to create a new file. We can not edit files using cat commands.
To create a new file using cat, run the cat command and then use the redirection operator «>» followed by the name of the file. Now you will be prompted to insert file content into this newly created file. Type a line and then press «Ctrl+D» to save the file.
Command: $ cat > abcFile.txt
Hello Everyone this is my file!
The above command will create a new file with the file name «abcFile.txt» and save it with the file content «Hello Everyone this is my file».
3. Using the redirection operator
The redirection operator is added after the normal command is written. We can simply use the redirection operator «>» to create a new blank file in the current directory and save the file contents. Run the «>» operator followed by the name of the file.
The above command will create a new file with the file name «abcFile.txt».
4. Using the echo command
The echo command takes a string as an argument and displays it as output. The echo command is also one of the commonly used commands to create a Linux file. Open the terminal and run the following command:
Command: $ echo «This is the File name file1»
This is the File name file1
This will first create a blank text file file1 and save the given file name contents in it.
We can also redirect this output to a new file, such as −
Command: $ echo «This is the File name file3» > file3.txt
This will create a text file file3 and redirect the output to the new file.
5. 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 and save the file content.
Command: $ printf ‘Studying hard will never fail you.\n’ > file2.txt
This will create an empty file with file name file2 and add the given content to the file.
6. Create a zip file and ignore the directory structure in Linux
In order to be able to zip files and ignore the directory structure that gets created with it, we can run the following command in the terminal:
Command: zip -j zipContent d1/file.txt d2/2.txt d2/3.txt
In the above command, we can notice the -j option which stands for “junk the path”, which will help you in ignoring the directory structure and make sure that you don’t get a directory structure mimicking the files.
Summary
From the above article, we can easily understand that creating files in Linux is very easy. It can be done both manually using the file manager or by using different commands in the terminal window. There are different types of files present in Linux and different methods or options through which we can create files in the Linux operating system.
Suggested reads:
I am a storyteller by nature. At Unstop, I tell stories ripe with promise and inspiration, and in life, I voice out the stories of our four-legged furry friends. Providing a prospect of a good life filled with equal opportunities to students and our pawsome buddies helps me sleep better at night. And for those rainy evenings, I turn to my colors.