Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand touch and mkdir commands

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you about how to understand touch and mkdir commands. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Introduction to 01 command

Everything has a life cycle, and so do files and directories in Linux: create, modify, delete. Today we will talk about the creation of files and directories.

Touch-- modifies the access time and modification time of the file (can be used to create an empty file)

Mkdir--make directory, create a directory

02 order detailed explanation

Grammar

Mkdir [option] directory name touch [option] file name

Both mkdir and touch commands have options and parameters. It's just that the argument to the mkdir command is the directory name, while the argument to the touch command is the file name.

Let me emphasize here: the options and parameters are optional, and the requirements of different commands are different.

Options are generally optional for commands. Whether the parameters are optional or not, different commands have their own rules. If optional, I will add square brackets "[]"; if not optional, that is, necessary, I will not add square brackets "[]".

Therefore, the option "[]" is added in the syntax, which means that the option is not required and can be omitted, but I did not add "[]" to the parameters of both commands, which proves that these two commands must provide arguments when they are used. Please pay attention to these details.

As you can see, the command is explicitly prompted to lack operands, that is, parameters.

Parameters.

Mkdir directory name

The argument to the mkdir command is the name of the directory to be created. Strictly speaking, the parameter is the pathname of the directory to be created.

We can provide only the name of the directory to be created. In this case, the system creates a directory in the current working directory.

You can see that the new_dir directory is created in the current working directory. In addition, the directory name can contain spaces, but at this time the directory name needs to be enclosed in quotation marks.

We can also specify the path to the directory. At this point, the directory will be created under the path we specified.

These two can be imagined as the default installation and custom installation of the software, which may be more vivid and easy for everyone to understand.

If the directory you want to create already exists, the system reports an error, indicating that the directory already exists.

The touch command is mainly used to modify the access time and modification time of files and directories, but by default, the touch command creates the file if the file specified by the parameter does not exist.

Just like creating a directory, if you do not specify a path, a file will be created in the current working directory. We can also specify the file path.

Option

The mkdir command and touch command can handle multiple parameters at the same time. When providing multiple parameters, we only need to separate the parameters with spaces.

As you can see, two directories and files are created in the sibling directory. Can you create a new directory while creating a new directory?

Just try it. Create a new son_dir directory in it while creating the father_dir directory with the following command.

Mkdir father_dir/son_dir

Found that the system reported an error, the reason I have marked in the picture. The meaning of this command is not to create a new father_dir directory while creating a new son_dir directory, but to create a new son_dir directory in the father_dir of the current directory, which is actually specifying the relative path to the new directory. However, the father_dir directory does not exist in the current directory at this time, so the system will report an error.

Can only create a new directory, and then change to a new directory and then create a new subdirectory, such a layer by layer?

Of course not, if it can only be like this, dozens of layers of directory creation can not be created for half a day? Of course, there are usually not so many floors.

The mkdir command provides a very human "- p" option that makes it easy for us to do this.

Mkdir-p directory name / directory name /... / directory name

After adding the "- p" option, the system did not report an error. While creating a new father_dir directory, a new son_dir directory was created in it.

This is what the "- p" option does: create all directories that lead to the specified directory but do not yet exist. In other words, all directories that do not exist in the path are created.

Father_dir/son_dir, at the beginning, if the father_dir directory does not exist in the current directory, create the father_dir directory first; after creation, you will find that there is no son_dir directory under the father_dir directory, then continue to create the son_dir directory until all the directories in the path exist. Some people call this "recursive creation".

The common options for the mkdir command are the "- v" option and the "- m" option. The "- v" option prints a message each time a directory is created, and the "- m" option sets the permissions for the directory. Here we will first introduce the "- v" option, "- m" option and so on when we introduce the permissions later.

As mentioned in the previous section, if the directory to be created already exists, the system will report it, so will the system report an error if the created file already exists?

As you can see, the system did not report an error, but it does not seem to make any difference. This involves the time attribute.

Use the long format to view the details and find that the file time is different after two commands. We can use the stat command to see more detailed status information about the file.

There are three times in the information listed.

Access: access time; read but not modify file content Modify: modification time; modify file content Change: change time; attribute and location change

When a file exists, the touch command modifies the access time and modification time of the file by default. However, it provides the "- a" option and the "- m" option, which only modify the access time or modification time, respectively.

Touch-a file.txt

The "- a" option only changes the access time.

Touch-m file.txt

The "- m" option only changes the modification time.

In addition to specifying which time attribute to modify, you can also specify the value of the time property (the default is the current time).

There are two options for specifying the value of the time property: the "- t" option and the "- d" option. The effect is the same, except that the format of the value is different.

The time format of the "- t" option is:

[[CC] YY] MMDDhhmm [.ss]

In fact, it is the initials of century, year, month, day, hour, minute and second.

The "- d" option has more time format than the "- t" option. The picture is one of them. I will not repeat the other formats here. Interested alumni can check the information on their own.

The touch command also has a "- c" option. By default, the touch command creates a new empty file if the file does not exist; when the "- c" option is added, the file is not created.

03 knowledge summary

Syntax: mkdir [option] directory name touch [option] File name purpose: mkdir---- create empty directory touch---- modify access time and modification time of files and directories (create empty files by default) Parameter: mkdir: the path name of the directory (if only the directory name is provided Then create a directory in the current working directory) touch: the path name of the file (if only the file name is provided Then create a file in the current working directory) option: mkdir:-p---- recursively create touch:-a---- only update access time-MMMI-only update modification time-t, -dmurmurmuri-specify the value of the time attribute-c Murmuri-do not create a file even if the file does not exist. Other: stat command: view the status information of the file or file system. This is how to understand the touch and mkdir commands shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report