Getting started with Linux, how to create a file? How to copy, cut, delete files? --(three)

Introduction: Review of the last issue

    In the previous chapter, you learned the basic format of commands and the concepts of directory command handling , groups an owner belongs to, and others . The previous chapter is just the beginning of the introduction , and we will have more and more commands in the next chapters. I hope you remember to practice regularly. Now that we have learned how to view directory files, in this chapter we will learn how to create files, how to create folders, and the related operations of copying, pasting and cutting files. Before starting to learn, the editor will list the content that needs to be mastered one by one, so that everyone can master and practice better. Don't underestimate these commands, what options do each command have, and under what circumstances to use these options These are all things that everyone needs to be familiar with and master, and also require everyone to spend more time to practice.

     1. How to create files and folders.

    2. How to switch the current directory and how to view the absolute path of the current directory.

    3. How to copy, cut, delete a file.

1. Creation of files and folders

    1 Create a folder

          Command: mkdir, the original English make directories

          Now we use the root user to log in, and the default login directory is the root home directory (mentioned in the previous lesson). Now we create a new folder in the root file, the command is "mkdir test", so we have created a folder named test in the current directory, if you need to create it in other directories, you can also specify other The location, for example, to create a folder under tmp command is "mkdir /temp/test", so that's it (Figure 1).

        We need to pay attention to a problem when creating folders. What if we also want to create subfolders when creating folders? I want to create a folder named xiaodianying in the root directory and then create a folder named duobo under xiaodianying. Now we use the mkdir command to create it should be "mkdir xiaodianying/boduo", you can try it first, see The result should be an error "No such file or directory", why is this? Because we now need to create the boduo folder under the xiaodianying folder, but our xiaodianying folder has not been created, so an error will be reported. At this time, we only need to add a "-p" option to create recursively : it means the system If the xiaodianying folder cannot be found, the xiaodianying folder will be created first and then the duobo folder will be created. The final command is "mkdir -p xiaodianying/boduo" (Figure 2).

        

 2 Create the file

            Command : touch 

           Now we use touch to create a file in the folder we just created. For example, if I want to create a jspan.list file in the test folder, the command is "tochou test/jspan.list", which creates a file.

               There are a few details to note here

               1.  "test/jspan.list", this means to create under the test folder of the current directory, if you need to write the absolute path in other directories, for example, if I want to create a file under tmp, it should be written like this "touch /tmp/jspan.list", you must write an absolute path. If you write a relative path, it is only for your current directory. Many children's shoes create a folder and don't know where to create it.

               2. In our linux system, except for the "/" slash symbol, other symbols can be used as the name of a file.

                3. What if we want spaces in our filenames? If you write the command "touch test/java net" like this, you will find that two files are created, one is in the test folder, and the other is in your current root home directory, because the net file does not specify a folder All are created in the current directory by default. When we really need the filename to contain spaces, we just need to enclose the filename in quotes, like touch "test/java net". However, when we are using the Linux system, it is best not to add spaces to the file or folder name, otherwise you need to add quotation marks to view and edit this file, which is inconvenient.

                 4. You can create multiple folders and files at one time, just separate them with spaces.

                

Second, view the current directory and switch directories

   1 View current directory

         Command: pwd

         Sometimes when we write commands, there may be a lot of files, and then we write and find that we don't know which directory we are in now, and we are very confused. What should we do? As long as we enter pwd, it will display the directory where we are now . I use the root user to log in, so the default is in the /root home directory.

        


    2 Change directory

         Command: cd, original English change directories

        在我们实际操作过程中,一直待在家目录肯定是不行的,所有我们得切换目录。只需要在命令后加上需要切换目录位置即可,例如"cd /tmp"。在cd命令后除了写目录位置,也可以使用".",如果写一个点"cd ."代表着当前目录,如果写两个点"cd ..",目录就会返回上一级

           

三 对文件进行复制粘贴删除操作

    1 复制

            命令:cp ,原英文copy

            现在我们在root家目录下创建3个文件了,我们需要把1.txt复制到tmp目录下,"cp 1.txt /tmp",只需在命令写上原文件路径和目标路径就可以了。可能有些童鞋会碰到这样的问题,在其他的目录下,然后写上另外一个目录下的文件名,一直复制不过去,这一点在上文就已经强调了,我这样写"cp 1.txt /tmp",我现在所在的目录下是有1.txt文件的,如果说我在其他目录下需要做同样的操作只需要写成绝对路径就可以了"cp /root/1.txt /tmp"。(图1)

            cp命令还有一个选项是"-r",意思就是我们现在需要复制一个目录,才会使用到"-r"的选项,如果没有写会有报错信息。(图2)

            现在我们把2.txt复制到/tmp,现在我们把/root目录下和/tmp的详细文件信息显示出来,对比看看有什么不同。我们看到两个文件的修改时间是不一样的,也就是说复制过来的文件和之前的原文件中的属性不是一致的。那想要复制文件的属性和之前原文件是一样的我们需要加上"-p"选项(图3)

            在linux中有时候是比windows方便的,比如说我们在windoes中复制一个文件然后需要改文件名,必须先复制然后在改名。但在我们linux当中可以复制改名一步完成,只需要在复制的目标路径最后写上文件名即可。(图4)

              

    3 剪切

            命令:mv ,原英文move

             在使用windows系统当中,剪切也是我们经常用到的操作,现在我们将/tmp目录下到4.txt文件,移动到root目录下,也只需要"mv /tmp/4.txt /root"。(图1)剪切和复制都是可以在操作的同时进行改文件名的,但是有一点不同的是剪切是可以直接进行目录操作不需要在使用选项。(图2)

              

    4 删除

            1 删除空目录命令 :rmdir,指定空目录删除,前提是只能删除空目录。(图1)

            

            

            2 删除文件命令:rm ,命令后指定文件名即可。如果需要删除一个目录的时候,我们需要加上一个"-r"的选项。(图1) 由图可以看见我们删除一个目录的时候,会一个一个提示是否需要删除,如果目录下有100个文件会提示100次。这个时候我们写上-f的选项,-f的意思就是强制删除,linux就不会一个一个文件进行提示。

            

四 总结

      这一章节主要学习了如何创建文件夹以及文件,以及对文件夹、文件的复制剪切删除等操作,需要大家花时间练习。下一章节主要学习如何编辑保存文件以及几种浏览文件的方式,目前小编也是边学边记录自己的学习经验,一个月最少会更新四章。如果有不好的地方多多指教,如果有大神路过请指定指点不足的地方。写博客不容易,如果觉得有收获的童鞋、麻烦各位动动小手点点赞(浏览器右边隐藏了一个小箭头鼠标悬浮点击一个图标),在此谢过!!!

Guess you like

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