New Linux commands

Source: Internet
Author: User

I wrote a quick training material for my wife-the most common command and explanation. Of course, each person's work content is different, and Common commands are also different. This is only my personal experience and the basis of the recipient should be taken into account.

Ls: List contents in the directory, similar to the dir in dos.

Common parameters include:

Ls-l. The detailed content is displayed line by line;

Ls-a: displays hidden files or directories. -- in linux, the file name and directory name prefixed with. are hidden by default.

Parameters can also be combined with ls-al to display the contents including hidden files in detail.

When-a is added, two special directories... And... are displayed, indicating the current directory;... indicating the parent directory.

Cd: Switch Directories

As you can imagine, cd. will not change, because switching to the current directory does not mean that it is not correct; cd... switch to the parent directory (at this time, we can see that the path before the command prompt is changed)

There are several common usage of cd:

Cd/go directly to the "root" (linux file system is similar to windows c: d:, etc., but no matter how many hard disks and partitions, linux only has a unique root /), so we don't need a cd once .. back up, right?

Cd ~ Step by step to the home directory. What is home directory? In linux, each time a user is created, a home directory is automatically assigned to the user. The user has full read and write permissions on his/her home directory, the default working directory after the user logs on is also in his own "home" (the same is true for windows, do not believe you log on with a new account, the "user" folder on disk C is displayed with a folder with the same account name, it contains "My documents", "my images", "My videos", and other things)

The general usage is cd a/B/c. Of course, the/B/c Level Three directory is under the current directory, which is the relative path; if it is not in the current directory, it cannot be found. Only cd/xxx/a/B/c can be written from the "root". This is the absolute path.

As mentioned above, no matter how many partitions there are, there is only one root. Where will it be placed if a hard disk is added? -- The answer is to put it wherever you like. The operation is like this. First

Mkdir: create a directory

Using mkdir to create a directory is quite simple to use and few parameters are added. Mkdir xxx is to create a xxx directory in the current directory. What if it is not created in the current directory? One way is to switch over with cd; the other way is to use the absolute path mkdir/xxx/a/B/c/xxx

Next step: "Attach" a partition of the new hard disk to the directory we just created.

Mount: mount the device.

Mount/dev/sdb1/xxx/a/B/c/xxx ), then go to the directory cd/xxx/a/B/c/xxx, and ls will see the content of the new hard disk. (Of course, here I assume that the number of the new hard disk is/dev/sdb1. As for why this number is used, I will not explain it here due to complexity issues)

If you want to check the content, you will raise the following question: assume that my original directory/xxx/a/B/c/xxx is not a new directory and contains content. What should I do when I mount a new hard disk in this directory? -- The answer is "you can't see it ". Don't be surprised, because it's not lost, but it's just invisible. When we unmount the device, they will be back.

Umount: uninstall the device

Umount/dev/sdb1, unmount it, simple.

Therefore, you can use this encryption tips to mount a device to the directory to be encrypted (however, to encrypt a directory and buy a hard disk, the cost is a bit high. As soon as someone else finds out, umount will be exposed)

Some people may say why is it so complicated that it can be recognized automatically like windows. Isn't a drive letter on a hard disk (partition) good? In fact, such multiple root directories may be more troublesome. Linux inherits the unix tradition and is mostly used on servers. It is common to add, replace, or even swap hard disks on servers. Imagine what if I got d: disk on this machine and changed it to e: disk, and many programs couldn't run? Even if we use personal computers, If you often install the system, there are many problems. Mounting to a directory is much easier. As long as the directory tree is consistent, it does not matter how to add, delete, or change the hard disk.

Imagine that in this scenario, the/xxx/a/B/c/xxx directory stores a database file. It is observed that the disk space is growing rapidly and the disk space is in a hurry. Don't worry, you can do this. First mount the new hard disk to a temporary directory and mount/dev/sdb1/temp to copy or move the original file (of course, to maintain data consistency, stop the database now) cp/xxx/a/B/c/xxx/*/temp-R. After the copy is complete, mount the hard disk at the mount/dev/sdb1/xxx/a/B/c/xxx location again to start the database, in addition, no changes are detected for all programs and users. If it is windows, you have to wait for a long time :) so it is easy to use a single directory. Have you seen Microsoft advocate single-partition disk :)

Mentioned copying and moving --

Cp: Copy command

Cp a. sh/tmp copy the. sh file in the current directory to the/tmp directory

Cp/tmp/. sh/xxx/a/B/c. sh copy. sh to/xxx/a/B/c and renamed it c. sh

The most common parameter-R indicates all subdirectories under the recursive loop operation, with uppercase letters

Mv: Mobile Command

Mv/tmp/a. sh/xxx/a/B/c move the file a. sh in/tmp to the/xxx/a/B/c directory without changing the name

In addition, change mv a. sh B. sh to a. sh

If the directory is not changed, change the name (because it is moved under the same directory)

If the copy is incorrect or the copy is too large, delete it.

Rm: delete a file or directory

Rm a. sh delete the file a. sh in the current directory

Rm *-R deletes all contents in the current directory, including Recursive Sub-directories (-R must be used with caution; otherwise, regret it late)

As mentioned above, it is easy for others to find your new hard disk. How can they find it? Df command

Df: displays the file system.

Df: check which hard disk is attached to which directory, how much space is occupied, and how much space is left at a glance. If you want to carefully understand that the directory or file occupies our valuable disk space, the du command

Du: view the file disk space

Let's run cd/to the root directory, du. Wow, let's see it clearly. List all files and directories step by step. But we don't need to look at this much. We only need to know how much space a level-1 directory occupies. Adding a parameter -- max-depth = 1 is to display Level 1, = 2, that is, two levels are displayed. For more detailed usage, see help. By the way, most commands are followed by the parameter -- help, which can help information.

What if I want to find one of these files? Find search

Find: find Files

Find can be searched by name, by type, by date, etc. In short, it is powerful enough to make people unable to look directly. For more information, see help. The most common search by name: find-name xxx. sh. find the xxx. sh file in the current directory and subdirectory.

Why have I repeatedly used the. sh file as an example? According to the habit, xxx. sh files can be executed as a shell file (similar to. bat in dos. But is it a real sh file? -- Wait. What does this mean? Are there any fake shell files? Of course it is possible. In Linux, file types are not differentiated by the extension, and the extension concept should not be used. The reason for writing xxx. xxx is to cater to the representation of windows users (of course, this representation is indeed good ). The system does not limit whether or not you want to write "extensions" or letters. Of course, if you want to rename the legend of Phoenix to haha. sh, you cannot do it. Conversely, if you change a. sh to a. xxoo, it can be executed. Isn't the file type messy? Therefore, we recommend that you use the naming method as agreed. In addition, you can use the command file to view the actual file type.

File: view the file type

File a. sh. If it is indeed a shell file, the display type is ASCII text-it is a text file (batch processing is a text file, and bat is the same ). You can run cat to view the file content.

Cat: View File Content

Cat a. sh to display the content. If the name was changed to a. sh by the legend of Phoenix, It would be garbled.

Of course, you can edit it with the edit command.

Vi: editing files-vi is actually an editing software. It is not detailed in length, So google

Well, I have mentioned a lot. Let's make a small example.

Cd ~ -- Go to your home directory

Vi a. sh -- create and edit a. sh file. Click the letter I to enter the editing status. In this case, enter. Input echo $ PATH (case sensitive), and add spaces to enter the 10 characters. Press ESC and enter: wq

Ls-l -- check if the file a. sh already exists. If so, if not, kill the computer and ask it out loud.

In this way, we can write the first shell script file. Run the command and enter a. sh to display "no command found ". What's going on? Clearly, don't rush to hit the computer and ask it. This is because in windows, the default situation is: first, search for and execute files in the current directory. If not, search for and execute files in the directory specified by the path environment variable. Linux does not search in the current directory for security reasons, but only in the directory specified by the PATH environment variable. Therefore, the "command not found" is displayed here ". What should we do now? Of course, the first method is to add the current directory to the path environment variable, but this is too troublesome. The second method is to specify the absolute path (I will tell you the exact location if you cannot find it). It can be like this/home/xxx/. sh, xxx is the home directory corresponding to the current user name. You can also do this ,~ /A. sh, don't forget ~ It is our home directory; it can also be like this./a. sh, don't forget. It refers to the current directory (forget to flip forward)

This prompt is different. If "permission is not enough" is displayed, at least the file is found. But what is the permission? Let's take a look at the existing permissions. Ls-l to list details. Check a. sh Line with the first character

-Rw-r --, remove one-to turn around, and there are 9 left. The three groups are rw-, rw-, and r-, respectively --, this indicates permissions for the three roles. The three roles are the owner, the same group, and others. (The division of roles is not very precise, but it is also basically enough. The key is that the roles are too complicated, if you are interested, refer to the various roles of the windows File System ). R indicates read, w indicates write, and x indicates execution. In summary, the permissions are divided as follows: the owner has the read and write (rw-) permissions, and the same group has the read and write (rw-) permissions, others have the read permission (r --). You have no execution permission. So how can I add the execution permission? Command chmod

Chmod: Change File Permissions

Execute chmod + x a. sh to add the execution permission to the. sh file. Next, let's look at ls-l. The permission is changed to rwxrwxr-x, which is divided into three groups: Self-reading, writing, and executable. The same group reads, writes, and executable, others read and execute. Execute permissions are added to each role. In fact, this is not safe, it is equivalent to everyone can execute it. If you want more precise control, it can help you understand the detailed usage of chmod.

Now, execute./a. sh to display a string of characters. The command we entered is echo $ PATH, so the execution result is to display the directory in the PATH environment variable. Note that the command is separated by:, and the command is separated by; in windows. Check that the current directory is definitely not in it; otherwise, the command will not be "not found ".

A running program is called a process in the system. Check which programs are running?

Ps: View Processes

Ps aux displays all user processes in detail. PID indicates the process number. When a process occupies too many resources or gets out of control, we can kill it.

Kill: kill the process

Kill PID

The resource information displayed by ps is static. If you want to observe the system resource usage in real time, use the top command to monitor

Top: monitors system resources and displays cpu usage and memory usage of each process. Refresh once every 5 seconds by default. Press q to exit.

All right, this rough + quick tutorial will end here. The reason why the introduction is not very detailed, there are not many commands, and there is no step-by-step approach, is to let people who want to see it know more. As for the specific operation of commands, the help is actually the best reference. What's more, there is a more powerful man command. I want to see the detailed explanation and usage of a command: man xxx.


What can I do if I cannot remember the command? -- If you only remember the first few letters of a command, press the tab key. If the command is not what we want, press the tab key twice in a row, all the commands starting with these letters will be listed. It's almost time to remember.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.