Introduction
How to Create a File in Linux Using Terminal/Command Line
1. Open the terminal window.
2. Type the command “touch” followed by the name of the file you want to create. For example, if you want to create a file called “myfile.txt”, type “touch myfile.txt”.
3. Press Enter. The file will be created in the current directory.
4. To view the file, type “ls” and press Enter. The file should appear in the list of files.
5. To edit the file, type “nano myfile.txt” and press Enter. This will open the file in the nano text editor.
6. Make any changes you want to the file and save it.
7. To exit the nano text editor, press Ctrl+X.
Introduction
Creating a new file in Linux is straightforward, but there are also some surprising and clever techniques.
In this tutorial learn how to to create a file from a Linux terminal.
Prerequisites
- Access to a command line/terminal window (Ctrl–Alt–F2 or Ctrl–Alt–T)
- A user account with sudo privileges (optional for some files/directories)
Creating New Linux Files from Command Line
Linux is designed to create any file you specify, even if it doesn’t already exist. One smart feature is that you can create a file directly, without needing to open an application first.
Here are a few commands for creating a file directly from the command line.
Create a File with Touch Command
The easiest way to create a new file in Linux is by using the touch command.
In a terminal window, enter the following:
touch test.txt
This creates a new empty file named test.txt. You can see it by entering:
ls
The ls
command lists the contents of the current directory. Since no other directory was specified, the touch
command created the file in the current directory.
If there’s already a file with the name you chose, the touch
command will update the timestamp.
Create a New File With the Redirect Operator
A redirection operator is a name for a character that changes the destination where the results are displayed.
Right angle bracket >
This symbol tells the system to output results into whatever you specify next. The target is usually a filename. You can use this symbol by itself to create a new file:
> test2.txt
This creates a new empty file.
Use the ls
command to list the contents of the current directory and find the file test2.txt.
Create File with cat Command
The cat
command is short for concatenate. It can be used to output the contents of several files, one file, or even part of a file. If the file doesn’t exist, the Linux cat command will create it.
To create an empty file using cat
, enter the following:
cat > test3.txt
Note the redirection operator. Typically, the command displays the contents of test2.txt on the screen. The redirection operator > tells the system to place it in the test2.txt file.
Verify that the file was created:
ls
The system should now have test.txt, test2.txt, and test3.txt in the list.
Create File with echo Command
The echo
command will duplicate whatever you specify in the command, and put the copy into a file.
Enter the following:
echo 'Random sample text' > test4.txt
Verify that the file was created:
ls
You should see the test4.txt file added to the list. Use the cat
command to display the contents of the new file:
cat test4.txt
The system should display Random sample text (or whatever you entered with the echo
command.)
Create File with printf Command
The printf
command works like the echo
command, and it adds some formatting functionality. To add a single line of text, enter:
printf 'First line of text\n' test5.txt
To add two lines of text, separate each line with the \n
option:
printf 'First line of text\n Second line of text' test6.txt
You can use the cat
command on either of these files to display their contents.
Note: To use several terminal instances in a single window manager, consider using Linux screen. It enables additional features and an enhanced command line for working with Linux files.
Using Text Editors to Create a Linux File
All Linux distributions have at least one text editor. Some have multiple editors. Each editor has different strengths and features. This will show you three of the most popular.
Vi Text Editor
Vi is the oldest text editor in Linux. It was created alongside the Linux operating system for directly editing text files. Since it’s unlikely you’ll see a Linux distribution without it, it’s a safe editor to know.
To create a file using Vi, enter the following:
vi test7.txt
Your screen will change. Now you’re in the text editor. Press the letter i
to switch to insert mode, then type a few words to try it out.
To save and exit press Esc :x
and hit Enter
.
Vim Text Editor
You may have noticed that the Vi editor wasn’t very user-friendly. Vim is a newer version, which stands for Vi editor, Modified.
Use vim
to create a new text file:
vim test8.txt
This screen will look similar to the Vi editor screen. Press i
to insert text, and type a few words. Save file and exit by entering:
Esc :wq Enter
(Escape, colon wq, then Enter.)
Nano Text Editor
Nano is a newer and much easier text editor to navigate.
Create a new file by entering the command:
nano test9.txt
By default, Nano puts you directly into editing mode. It also displays a helpful list of commands at the bottom of the screen.
Enter some text, then press Ctrl+O to save the changes.
Press Ctrl+X to exit the editor.
Conclusion
Now you have several options to create new files in Linux from the command line. Next, learn how to copy files and directories in Linux to manage your files more efficiently.
How to Create a File in Linux Using Terminal/Command Line
Creating a file in Linux using the terminal or command line is a simple process. All you need to do is use the touch command. This command will create an empty file with the name you specify. Here’s how to do it:
Step 1: Open the Terminal
The first step is to open the terminal. You can do this by pressing Ctrl+Alt+T on your keyboard. This will open the terminal window.
Step 2: Use the Touch Command
Once the terminal window is open, you can use the touch command to create a file. The syntax for the command is as follows:
touch [filename]
Replace [filename]
with the name of the file you want to create. For example, if you want to create a file called myfile.txt
, you would use the following command:
touch myfile.txt
Once you press Enter, the file will be created in the current directory.
Step 3: Verify the File Was Created
To verify that the file was created, you can use the ls
command. This command will list all the files and directories in the current directory. If you see the file you created, then it was successfully created.
Conclusion
Creating a file in Linux using the terminal or command line is a simple process. All you need to do is use the touch command. This command will create an empty file with the name you specify. Once you have created the file, you can verify it was created by using the ls
command.