Console for the beginner. Part 2.

    And so I continue to introduce newcomers to the course of affairs. This is the second article, the first to be here .

    After you can confidently walk around the expanses of the file system using the console, you need to learn about its formation. Suppose you need to create a directory, in this case use the mkdir command .
    vir @ home: ~ $ mkdir Test
    vir @ home: ~ $ ls
    Images Other Test Work Desktop

    In this example, we created the test directory in the working directory / home / vir . However, as I said to many utilities, you can pass a parameter, so let's try to create a directory by specifying the full path for it:
    vir @ home: ~ $ mkdir / tmp / Test

    Here, using the passed parameter, we indicated to the utility that it would create the Test directory in the / tmp directory . Again, remembering the keys (options) that you can pass to utilities, and once again make sure that this is an extremely useful feature, for example, you need to create the / tmp / Test / One / More directory, in this case we already have the / tmp and / tmp / Test , it remains to create / tmp / Test / One and / tmp / Test / One / More . We try:
    vir @ home: ~ $ mkdir / tmp / Test / One / More
    mkdir: cannot create directory `/ tmp / Test / One / More ': No such file or directory

    Did not work out. But don’t worry. It did not work out because we are trying to create the directory / tmp / Test / One / More , while the directory / tmp / Test / One does not exist. You can of course create them in turn, but why when are the keys? We use the -p switch :
    vir @ home: ~ $ mkdir -p / tmp / Test / One / More

    Using this key, we create all the missing directories in the path, if necessary. Yes, by the way, the information that mkdir is short for Make Directory (creating a directory) will not be harmful .

    We figured out the creation of directories, now let's try to create a file:
    vir @ home: / tmp / Test $ touch file
    vir @ home: / tmp / Test $ ls -l
    total 4
    -rw-r - r-- 1 vir vir 0 2008-07-15 21:41 file
    drwxr-xr -x 3 vir vir 4096 2008-07-15 21:38 One

    And so, using the touch command, we created a file called file . This file is empty, does not take up space on your hard drive. You ask: "But what about the output of the ls command, where is the file and where is the directory?" It's very simple, firstly in all modern distributions, the ls command is an alias for the ls --color = auto command , which means that directories and files are highlighted in different colors (though different distributions have different colors). The second way is determined by the output of the ls -l command :
    vir @ home: / tmp / Test $ ls -l
    total 4
    -rw-r - r-- 1 vir vir 0 2008-07-15 21:41 file
    drwxr-xr-x 3 vir vir 4096 2008-07-15 21:38 One

    Pay attention to the letter "d", which I highlighted in red in the example, it then shows us that One is a directory, and file with an empty attribute (with a dash).

    Let's move on to moving files and directories. To move, use the mv command (short for move ). Example:
    vir @ home: / tmp / Test $ mv file One /
    vir @ home: / tmp / Test $ ls One /
    file More

    And so, we moved the file file from the directory / tmp / Test to the directory / tmp / Test / One. Everything is simple here. If you want to move the file, and at the same time give it a different name, then this must be clearly indicated:
    vir @ home: / tmp / Test $ mv One / file ../moved_file
    vir @ home: / tmp / Test $ ls
    moved_file One

    Here, in addition to moving the file file back to the / tmp / Test directory , we also set the new name moved_file to it .
    Well, for a final understanding of the work of the mv command, I will show that it can be used simply to rename a file (without moving):
    vir @ home: / tmp / Test $ ls
    moved_file One
    vir @ home: / tmp / Test $ mv moved_file new_name
    vir @ home: / tmp / Test $ ls
    new_name One
    vir @ home: / tmp / Test $

    Let's move on ... to copying. Copying files is as easy as moving. The principle is exactly the same:
    vir @ home: / tmp / Test $ cp new_name second_file
    vir @ home: / tmp / Test $ ls -l
    total 4
    -rw-r - r-- 1 vir vir 0 2008-07-15 21:41 new_name
    drwxr- xr-x 3 vir vir 4096 2008-07-15 21:54 One
    -rw-r - r-- 1 vir vir 0 2008-07-15 21:59 second_file

    I think everything is absolutely clear here. And you probably already guessed that cp is short for copy (copy) .
    During the training, we have created a lot of unnecessary (training) garbage. Well, it's time to clean it all up. We started with your home directory, and we’ll go there:
    vir @ home: / $ cd ~
    vir @ home: ~ $ ls
    Images Other Test Work Desktop
    vir @ home: ~ $

    Here we went to the home directory and looked at what kind of garbage we have there. Yes, by the way, in order to quickly go to your home directory (/ home / username), just use the alias " ~ ".
    And so we see traces from our samples - this is the Test directory , which we no longer need, and we delete it:
    vir @ home: ~ $ rm -rf ~ / Test

    Here I again use the " ~ " feature , this is equivalent to indicating the absolute path / home / vir / Test . So we use the rm utility for removal (short for remove ). The -rf switches are my habit. Namely, the -r switch means - recursively, that is, delete everything in this directory, including itself. If we would not use this key when deleting the Test directory , we would get an error:
    vir @ home: ~ $ rm -f Test
    rm: cannot remove `Test ': Is a directory

    And the -f switch tells us that there is no need to ask us every time if we are sure that we want to delete something. Therefore, I am used to using rm -rf , although I do not encourage such use of the rm utility . I think now you understand that you should not listen to the advice of various "gurus" telling you that "rm -rf /" cures all troubles. It’s not difficult for you to guess the meaning of this line.

    And yet, remember we created test directories and files in the / tmp directory? Delete them too:
    vir @ home: ~ $ rm -rf / tm [Tab] p / Test /
    What kind of strange "" you ask? I will answer, so I have displayed the Tab button clicked on your keyboard. That is, I typed rm -rf / tm then pressed Tab, and lo and behold ! The remaining p / characters were added by myself, then I just typed the capital letter T and again pressed Tab, again a miracle! Symbols est / added themselves! And all because the Tab button in the console is an indispensable auto-completion assistant. Of course, in my example, using this button does not greatly reduce the time, but imagine that you have a directory fotografii-moey-lyubimoy-devushki . What is long? This is where Tab helps you. Dial foto, press Tab and everything else is supplemented by itself. But what if you have two directories or two files that start with " foto "? Then the console will display both options for you until the written expression matches only one. Example:
    vir @ home: ~ $ mkdir foto-moey-devushki
    vir @ home: ~ $ mkdir foto-brata-Olega
    vir @ home: ~ $ rm -rf
    foto-foto-brata-Olega / foto-moey-devushki /
    Two options are issued , making it clear that the directory starting with " foto- " two. Supplement until there will be a difference between them manually :). Well, about Tab, I explained to you, now delete some unnecessary directories:
    vir @ home: ~ $ rm -rf foto *

    And again, tricks, in the form of some kind of asterisks (*). And this is nothing more than the concept of regular expressions. Regular expressions are a rather complicated topic and we won’t go into it, but reading about them yourself will not hurt you. I’ll explain to you about the asterisk - since we had two directories with a similar beginning " foto " we can delete them at the same time indicating only the beginning. Because an asterisk (*) means any number, any characters. That is, literally:
    vir @ home: ~ $ rm -rf foto *
    Delete everything that starts on foto

    Another example with a starlet, suppose you have 10 files in the directory with the extension mp3, and 20 files with the extension ogg! Yes, we do not like propietarism, therefore we will delete all mp3 files. But individually, this is time-consuming, indicating the name of each file, so we will use the magic star:
    vir @ home: ~ $ rm -rf * .mp3

    I think the essence of the star is now clear to you.

    I would also like to tell you about the great utility man . man (short for man (manual) ) allows you to read the manual on utilities, various configuration files, and other things. We use it like this:
    vir @ home: ~ $ man rm

    And you will have a guide to the rm utility. This is a non-replacing utility that should accompany you all your conscious life in Linux.

    That's enough for today. After this lecture, you can not only walk around the expanses of the file system (FS), but also form it yourself. In addition, do not forget the tricks with ~, *, .., Tab.

    Thanks for attention.

    PS I decided not to make the article huge and loaded. Better quality and little by little, so as not to overdo it.

    Also popular now: