Briefly understand the two common methods of creating files in Linux terminal

Briefly understand the two common methods of creating files in Linux terminal

We all know that we can use the mkdir command to create a new directory, but more often it would be more pleasant if we could directly create a file (ordinary file); in this way, we don’t have to open a special software to create text files, and then set the file name and save path. Here are some methods to create files directly in the Linux terminal.

Method 1

We can use the dd command to create a file of a specific size, which is stored in the directory /bin. The dd command copies the given input and then writes an identical copy to the output.

Standard input, ordinary files, and device files can all be used as input; standard output, device files, ordinary files, etc. can be used as output.

The usage format of the dd command is: dd if=... of=... bs=... count=...

if indicates input file, of indicates output file, bs specifies the block size in bytes by default (the units are byte c, word w, block B, kilobyte m, megabyte m, gigabyte G), and count indicates the number of blocks to be copied;

For example: create a 1024-byte file with all contents of 0 (empty); /dev/zero is a device file that can provide a null character (\0) and can be used to generate an empty file of a specific size.

If bs=2048, count=2, then a file with a size of 4096 bytes is created.

If if is not specified, dd will read input from standard input. If of is not specified, dd will use standard output stdout as output.

Be careful when using dd as root. If you accidentally clear or damage important files or devices, it will cause irreparable trouble.

Method 2

We can also use the touch command to generate a blank file. This command is simpler to operate and is generally located in the /usr/bin directory:

touch uses a non-existent file name as a parameter to create a blank file. The format is: touch filename; such as:

Of course, some friends may say that they can also use the command vi/vim. This is because I emphasized that it is to create a normal file, and vi/vim, as an editor, emphasizes more on editing. After we create a file, we can use the vi editor, vi filenmae, and then enter the insert mode to edit the content. Finally, press :wq to exit and save.

Note that the above file name can be in any format, it can be a text file (txt), or a c, c++ file, etc. The file suffix depends on the content and function of your file.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Commands to find domain IP address in Linux terminal (five methods)
  • Detailed explanation of how to use socks5 proxy for Linux terminal
  • Android's implementation method of executing shell scripts in the Linux terminal to directly print the log of the currently running app
  • Two tools for splitting the screen in the Linux command line terminal
  • How to display asterisks when entering a password in the terminal in Linux
  • Two methods of terminal split screen under Linux (screen and tmux)
  • Detailed explanation of commonly used shortcut keys for Linux terminal command line
  • How to exit the Python command line in the Linux terminal
  • Simple way to use Python in Linux command line terminal (recommended)

<<:  MySQL scheduled database backup operation example

>>:  Vue implements QR code scanning function (with style)

Recommend

N ways to cleverly implement adaptive dividers with CSS

Dividing lines are a common type of design on web...

Why is your like statement not indexed?

Preface This article aims to explain the most bor...

Use native js to simulate the scrolling effect of live bullet screen

Table of contents 1. Basic principles 2. Specific...

How to use CSS style to vertically center the font in the table

The method of using CSS style to vertically cente...

Markup language - for

Click here to return to the 123WORDPRESS.COM HTML ...

MySQL insert json problem

MySQL 5.7.8 and later began to support a native J...

How to build nfs service in ubuntu16.04

Introduction to NFS NFS (Network File System) is ...

Implementation of Nginx domain name forwarding https access

A word in advance: Suddenly I received a task to ...

User experience of portal website redesign

<br />From the launch of NetEase's new h...

What do CN2, GIA, CIA, BGP and IPLC mean?

What is CN2 line? CN2 stands for China Telecom Ne...

Nginx sample code for implementing dynamic and static separation

In combination with the scenario in this article,...

MySQL helps you understand index pushdown in seconds

Table of contents 1. The principle of index push-...

CentOS uses expect to remotely execute scripts and commands in batches

Sometimes we may need to operate servers in batch...

Example code for implementing raindrop animation effect with CSS

Glass Windows What we are going to achieve today ...