HomeLinuxCreate New Recordsdata in Linux Terminal

Create New Recordsdata in Linux Terminal


To date, on this Terminal Fundamentals collection, you will have discovered to:

Let’s now find out about creating information within the Linux command line. I am going to briefly focus on including content material to the file. Nonetheless, particulars on enhancing textual content information will likely be lined later.

Create a brand new empty file with contact command

Utilizing the contact command is fairly easy.

contact filename

Change to your property listing and create a brand new listing known as practice_files and change to this listing:

mkdir practice_files && cd practice_files

💡

The && is a approach to mix two instructions. The second command solely runs when the primary command is executed efficiently.

Now, create a brand new file named new_file:

contact new_file

That is it. You’ve gotten simply created a brand new empty file.

Record the listing content material and examine the properties of the file with ls -l command.

Using touch command to create new files
Utilizing contact command to create new file

💡

The contact command’s unique goal is to ‘contact’ a file and alter its timestamp. If the supplied file doesn’t exist, it creates a brand new file with the identify.

Create a brand new file utilizing the echo command

I ought to have launched you to the echo command lengthy again. Higher late than by no means. The echo command shows no matter you present to it. Therefore the identify echo.

echo Hi there World

You should use redirection and route the output to a file. And therefore creating a brand new file within the course of:

echo "Hi there World" >> other_new_file

This fashion, you create a brand new file named other_new_file with the textual content Hi there World in it.

Using echo command to create new file
Utilizing echo command to create new file

Keep in mind, if the supplied file already exists, with >> redirection, you add a brand new line to the file. You may as well use > redirection however then it would change the prevailing content material of the file.

Extra on redirection could be discovered within the beneath tutorial.

Enter Output & Error Redirection in Linux [Beginner’s Guide]

Redirection is a necessary idea in Linux. Discover ways to use stdin, stdout, stderr and pipe redirection in Linux command line.

Create new information utilizing the cat command

The unique goal of the cat command was to concatenate information. Nonetheless, it’s primarily used for displaying the contents of a file.

It will also be used to create a brand new file with the choice so as to add content material. For that, you should utilize the identical > and >> redirections.

cat >> another_file

However this one will create a brand new file and assist you to add some textual content to it. Including textual content is non-obligatory. You’ll be able to exit the cat coming into mode by utilizing Ctrl+d keys.

Using cat command to create new file
Utilizing cat command to create new file

Once more, the append mode >> provides new textual content on the finish of file content material whereas the clobber mode > replaces the prevailing content material with new.

🖥️

Use the lengthy itemizing show with ls -l and see the timestamps. Now contact the file contact other_new_file. Do you see the distinction within the timestamps?

Check your data

You’ve gotten discovered about creating new information. Listed below are a couple of easy workouts to observe what you simply discovered. It features a little little bit of the earlier chapters as nicely.

  • Use the contact command to create three new information named file1, file2 and file3. Trace: You needn’t run contact 3 times.
  • Create a listing known as information and create a file named my_file in it.
  • Use the cat command to create a file known as your_file and add the next textual content in it “That is your file”.
  • Use the echo command so as to add a brand new line “That is our file” to your_file.
  • Show all of the information in reverse chronological order (consult with chapter 3). Now use the contact command to change the timestamp of file2 and file3. Now show the content material in reverse chronological order once more.

That is fairly enjoyable. You’re making good progress. You’ve gotten discovered to create new information on this chapter. Subsequent, you will find out about viewing the contents of a file.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments