in order to quickly create an empty file, what command can be used?
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 last.
Prerequisites
- Access to a control 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 whatever file you specify, even if information technology doesn't already be. One smart characteristic is that you lot can create a file directly, without needing to open an application first.
Here are a few commands for creating a file straight from the command line.
Create a File with Touch Command
The easiest way to create a new file in Linux is past using the impact command.
In a terminal window, enter the post-obit:
touch test.txt
This creates a new empty file named exam.txt. You can run across it by inbound:
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'southward already a file with the name yous chose, the touch on
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 whatsoever you specify next. The target is commonly a filename. Yous can use this symbol past itself to create a new file:
> test2.txt
This creates a new empty file.
Utilize the ls
command to listing the contents of the electric current directory and find the file test2.txt.
Create File with cat Command
The true cat
control is short for concatenate. Information technology tin exist 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 volition create it.
To create an empty file using true cat
, enter the following:
cat > test3.txt
Notation the redirection operator. Typically, the control displays the contents of test2.txt on the screen. The redirection operator > tells the system to identify it in the test2.txt file.
Verify that the file was created:
ls
The organisation should at present have test.txt, test2.txt, and test3.txt in the list.
Create File with echo Control
The echo
command will duplicate whatever you lot 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
Yous should see the test4.txt file added to the list. Employ 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 repeat
control.)
Create File with printf Command
The printf
command works like the repeat
command, and information technology adds some formatting functionality. To add a single line of text, enter:
printf 'Start line of text\n' test5.txt
To add 2 lines of text, separate each line with the \north
option:
printf 'Starting time line of text\north Second line of text' test6.txt
Yous can employ the cat
command on either of these files to display their contents.
Notation: To use several terminal instances in a unmarried window manager, consider using Linux screen. It enables additional features and an enhanced control line for working with Linux files.
Using Text Editors to Create a Linux File
All Linux distributions have at least ane text editor. Some have multiple editors. Each editor has different strengths and features. This will evidence y'all 3 of the nigh pop.
Vi Text Editor
Vi is the oldest text editor in Linux. It was created alongside the Linux operating organisation for directly editing text files. Since it's unlikely yous'll run into a Linux distribution without it, it's a safe editor to know.
To create a file using 5 i, enter the following:
6 test7.txt
Your screen volition change. Now you're in the text editor. Press the letter i
to switch to insert way, then type a few words to endeavour it out.
To save and exit press Esc :10
and hit Enter
.
Vim Text Editor
You may accept noticed that the V i editor wasn't very convenient. 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 await similar to the Six editor screen. Press i
to insert text, and blazon a few words. Save file and exit by entering:
Esc :wq Enter
(Escape, colon wq, so Enter.)
Nano Text Editor
Nano is a newer and much easier text editor to navigate.
Create a new file past entering the command:
nano test9.txt
Past default, Nano puts yous directly into editing manner. It also displays a helpful listing of commands at the lesser of the screen.
Enter some text, then press Ctrl+O to save the changes.
Press Ctrl+10 to exit the editor.
Conclusion
Now y'all have several options to create new files in Linux from the command line. Next, acquire how to copy files and directories in Linux to manage your files more efficiently.
Was this article helpful?
Aye No
Source: https://phoenixnap.com/kb/how-to-create-a-file-in-linux
0 Response to "in order to quickly create an empty file, what command can be used?"
Post a Comment