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

A brief analysis of MySQL cardinality statistics

1. What is the cardinality? Cardinality refers to...

How to use VUE and Canvas to implement a Thunder Fighter typing game

Today we are going to implement a Thunder Fighter...

Talk about the understanding of CSS attribute margin

1.What is margin? Margin is used to control the sp...

js to realize the mouse following game

This article shares the specific code of js to im...

How to encapsulate timer components in Vue3

background When you open the product details on s...

Compile CPP files using G++ in Ubuntu

When I used g++ to compile the cpp file for the f...

How to install pip package in Linux

1. Download the pip installation package accordin...

Detailed explanation of the wonderful uses of SUID, SGID and SBIT in Linux

Preface Linux's file permission management is...

Nginx URL rewriting mechanism principle and usage examples

URL rewriting helps determine the preferred domai...

Vue must learn knowledge points: the use of forEach()

Preface In front-end development, we often encoun...

Play with the connect function with timeout in Linux

In the previous article, we played with timeouts ...

Detailed explanation of how to easily switch CSS themes

I recently added a very simple color scheme (them...

Nginx cache files and dynamic files automatic balancing configuration script

nginx Nginx (engine x) is a high-performance HTTP...