Linux create and delete (5)

Compared with the right-click new file and delete under Windows, I prefer the imperative creation and deletion under Linux, which is really convenient. However, it can also be implemented with tools under Windows, such as git bash, cmder and other terminal tools.

Create a file

Use the touchcommand to create a new file, followed by the file name directly, such as creating a js file named demo:

touch demo.js

 

Note that if there is already a file with the same name in the current directory, the last modification date of the file will be modified.

View file information

ls -l

There is a 111.txt file with information:

-rw-r--r--   1 root root     0 Apr 19 16:04 111.txt

Now use the touch command to create it again:

touch 111.txt

Take another look at the information:

ls - l  

// -rw-r--r--   1 root root     0 Apr 22 14:57 111.txt

Apparently, time has changed~

 

Create a directory

To create a directory use the mkdircommand, followed directly by the directory name.

Combine the cd command to create a multi-level directory

mkdir a
cd a
mkdir b
cd b
mkdir c
cd c

This level is very clear, look at the current position:

pwd

//  /a/b/c

 

Although the creation command is simple, the above looks very cumbersome, and it is easy to write mistakes when there are more levels, so here is an option to create a multi-level directory more easily:

-p : can create directories recursively

Then create a directory like the above, you can write like this:

mkdir -p a1/b1/c1

 

delete

The delete command is rm, which can delete files or directories. It should be noted that:

Files deleted with rm are not recoverable

Delete Files

To delete a common file, directly rm + file name, for example:

// Delete the test.txt file 
rm test.txt

You will be prompted whether to delete this file:

rm: remove regular empty file ‘test.txt’?

Enter y after the question mark to confirm the deletion, and enter n to cancel the deletion.

 

delete directory

If you want to delete a directory file, it is not possible to use rm directly, for example:

// delete a directory 
rm a

The system will prompt:

rm: cannot remove ‘a’: Is a directory

 

At this point, we need to add an option, which is -r, and try again:

rm -ra

At this point, you will be prompted:

rm: descend into directory ‘a’?

Enter y to confirm:

rm: descend into directory ‘a/b’?

This is because there is also a b directory under the a directory, confirm again:

rm: remove directory ‘a/b/c’?

There is still a c directory in the b directory, and it has been determined since then:

rm: remove directory ‘a/b’?y

rm: remove directory ‘a’?

 

It can be seen that the process of deleting a directory is more troublesome. It will enter the deepest directory layer by layer, and then delete it layer by layer from the inside out. It's cumbersome, but reasonable, because if you don't want to delete all files in the entire directory, such a query is still necessary. Now, you may ask, I just want to delete the entire directory, what should I do if I don't want it to keep prompting? For this, Linux offers another option.

 

Force delete

Use option -f to force delete without prompting, even if the file does not exist.

Now delete the a directory, you can:

rm -rf a

At this point, the entire a directory will be deleted at one time without prompting again.

 

Before this, if we delete a file that does not exist, it will prompt that the file does not exist:

//删除aaa.txt文件
rm: cannot remove ‘aaa.txt’: No such file or directory

If you add -f, it will not prompt:

rm -f aaa.txt

 

Finally, I want to say that whether it is the touch, mkdir or rm commands, they all have more than one option available. I just said everything I used, and it is not necessary to say too much. It is better to check the manual.

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326479358&siteId=291194637