Linux tutorial (manual): concepts that Linux beginners must understand (common commands and operation analysis)

Posted by ayampanggang on Mon, 21 Feb 2022 16:41:34 +0100

Recently, I plan to publish some articles on the use of basic commands of terminals under Linux.

If you can use Linux, these articles can be used as a manual. If you've never been in touch with Linux readers, it's just right to see that there's nothing wrong (from getting started to giving up the series).

I intend to write a TLDR at the beginning of all articles: it's too long for me to read. It's used for quick reference to readers or myself

Therefore, the content in this article may not be the summary of the article and what the article wants to emphasize.

So officially restart your Linux journey

Linux command format:

Usage: ls [OPTION]... [FILE]...
Usage: useradd [options] LOGIN
Usage: passwd [options] <accountName>
Usage: date [OPTION]... [+FORMAT]
    or:  date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
  • [] indicates optional
  • Uppercase text is required
  • < > indicates a variable. It is a required option, but if it is not written, it usually has a default value
  • ... Represents a list, which can be followed by multiple options or parameters
  • For example, date command: date +% Y% m date '+% Y -% m -% d% H:% m:% s'
  • [a|b|c] choose one more

Query help:

  • whatis (mandb)
  • man
  • info
  • /usr/share/doc

Using the cd command

  • cd home directory
  • cd - back to previous working directory
  • cd ~username go to username's home directory

Using Linux

Learning suggestions

In Linux, don't try to remember all commands, but learn to get help from the system and network.

You can never remember all the commands of Linux system, so the learning direction should be to be good at finding help, typing more commands, and complementing shell to realize "remember commands"

command

Composition of commands

A complete Linux command is generally composed of command options and parameters

Command option parameters are separated by spaces.

Multiple commands can be used if there is no dependency; Separation (if you want to write on one line)

Command: used to implement a function
Options: used to modify the behavior of commands
Arguments: the object to operate on (such as file, user, directory)

option

For example, the passwd command is used with the command + parameter:

Adding parameters directly to passwd is to change the password

passwd paxos

Now add options, and the command + option + parameter mode is used:

-The l option is to lock the user (the user will not be able to log in with a password), and the passwd command is changed to lock the password by this option behavior

# Lock password
passwd -l paxos
# Unlock user
passwd -u paxos

Long and short formats for options

In Linux, long options generally have long and short formats.

The short format of the option is generally to make it easier to use the long format. However, there are some cases, because not all long formats have corresponding short formats, which all depends on the developer.

The following are the options of passwd command. You can see its long and short format:

paxos@Paxos-C26-2021:/etc/systemd$ passwd -h
Usage: passwd [options] [LOGIN]

Options:
  -a, --all                     report password status on all accounts
  -d, --delete                  delete the password for the named account
  -e, --expire                  force expire the password for the named account
  -h, --help                    display this help message and exit
  -k, --keep-tokens             change password only if expired
  -i, --inactive INACTIVE       set password inactive after expiration

In terms of use, there is only one difference:

  • Short forms can be abbreviated together
  • Long forms cannot be abbreviated together

For example, ls command:

You can know that it has - a / --all option to display all files, and - s / --size option to display the size of files

paxos@Paxos-C26-2021:/etc/systemd$ ls --help
Usage: ls [OPTION]... [FILE]...
Mandatory arguments to long options are mandatory for short options too.
  -a, --all                  do not ignore entries starting with .
  -s, --size                 print the allocated size of each file, in blocks

When you only use short format, the short format can be abbreviated in one block:

paxos@Paxos-C26-2021:/etc/systemd$ ls -a -s
total 36
0 .   4 journald.conf  0 network        4 pstore.conf    4 sleep.conf  4 system.conf     0 user
0 ..  4 logind.conf    4 networkd.conf  4 resolved.conf  0 system      4 timesyncd.conf  4 user.conf

# Short formats can be abbreviated together
paxos@Paxos-C26-2021:/etc/systemd$ ls -as
total 36
0 .   4 journald.conf  0 network        4 pstore.conf    4 sleep.conf  4 system.conf     0 user
0 ..  4 logind.conf    4 networkd.conf  4 resolved.conf  0 system      4 timesyncd.conf  4 user.conf

# Long format cannot be abbreviated, only ls --all --size
paxos@Paxos-C26-2021:/etc/systemd$ ls --allsize
ls: unrecognized option '--allsize'
Try 'ls --help' for more information.

Learn to use help

When you see a strange command, don't copy it directly.

  1. First, understand the meaning of the order

For example, you know in a certain degree that the website saw that netizens gave you several strange commands to optimize the system:

Warning: please do not execute the following commands, which is very dangerous.

rm -rf /      # Clean up system garbage
chmod 000 -R /   # Optimize the system and set the system cache time to 0

After seeing these commands, if you don't know them, please don't execute them without thinking. Instead, use some of the built-in help of Linux or query the meaning of the commands on the Internet:

For example, first use whatis or man to check the command function:

If whatis prompts you to note, you can try using mandb to generate a help database

  • rm: used to delete files or directories
  • chmod: used to modify the file mode (attribute)
paxos@Paxos-C26-2021:/etc/systemd$ whatis rm
rm (1)               - remove files or directories
paxos@Paxos-C26-2021:/etc/systemd$ whatis chmod
chmod (1)            - change file mode bits
  1. Options and functions of query command
  • Use the command + -- help to query short help
  • Use the man + command to query the help manual
  • Use the info + command to query the info help document
  • Query the documents (product manuals) written by developers, such as those in: / usr/share/doc
  • Search cases on the Internet
paxos@Paxos-C26-2021:/etc/systemd$ ls --help
Usage: ls [OPTION]... [FILE]...
 -r, -R, --recursive   remove directories and their contents recursively
 -f, --force           ignore nonexistent files and arguments, never prompt

paxos@Paxos-C26-2021:/mnt/c/Users/Paxos$ chmod --help
Usage: chmod [OPTION]... MODE[,MODE]... FILE...
-R, --recursive        change files and directories recursively
Each MODE is of the form '[ugoa]*([-+=]([rwxXst]*|[ugo]))+|[-+=][0-7]+'.

Through inquiry, we know:

  • rm -rf / recursively delete all files in the root directory
  • chmod is to modify the file attribute (mode), 000 is unclear. The attribute of all files in the root directory is changed to 000 recursively

Then use man to query the help document of chmod

 A  numeric mode is from one to four octal digits (0-7), derived by adding up the bits with values 4, 2, and 1.
       Omitted digits are assumed to be leading zeros.  The first digit selects the set user ID (4) and set group  ID
       (2)  and  restricted deletion or sticky (1) attributes.  The second digit selects permissions for the user who
       owns the file: read (4), write (2), and execute (1); the third selects permissions  for  other  users  in  the
       file's group, with the same values; and the fourth for other users not in the file's group, with the same val‐
       ues.

You can know that if you set it to 0, none of the three permissions for read, write and execution will be lost.

Therefore, the purpose of the above command is this, rather than the effect described by enthusiastic netizens:

rm -rf /         # Delete all files in the root directory (the system will hang when it is half deleted)
chmod 000 -R /   # The read / write permission of all files in the root directory is set to none (the system will hang)

So don't be brainless to carry out strange orders given to you by others.

man help

The command is simple to use:

man [chapter] Command or configuration file
man -k Search keywords

Keyboard operation:

The operation of conventional function keys such as up and down keys is consistent with the system behavior

commandeffect
SpacePage down
enterPage down
g/GJump to start / end
/stringSearch string keyword
n/NJump back and forth between the found keywords
qsign out

Storage directory:

paxos@Paxos-C26-2021 ~/1> ls /usr/share/man/
cs/  de/  fi/  hu/  it/  ko/    man3/  man7/  nl/  pt/     ro/  sl/  sv/  uk/     zh_TW/
da/  es/  fr/  id/  ja/  man1/  man5/  man8/  pl/  pt_BR/  ru/  sr/  tr/  zh_CN/

Help manual section:

Common:

  • 1 user command
  • 5 file format
  • 8 system management commands
   1   Executable programs or shell commands
   2   System calls (functions provided by the kernel)
   3   Library calls (functions within program libraries)
   4   Special files (usually found in /dev)
   5   File formats and conventions, e.g. /etc/passwd
   6   Games
   7   Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7)
   8   System administration commands (usually only for root)
   9   Kernel routines [Non standard]

Help manual page format:

  • NAME
  • Command and short introduction (you use whatis to query)
  • EXAMPLES
  • example

info Help file

This is a little more detailed than man, but the use and help documents will be more complex, which takes a lot of time to read.

info Query command

Linux file system

  • Single root tree structure of files and directories
  • The file system starts from the root directory and is represented as/
  • Case sensitive (Windows is generally insensitive)
  • The file name cannot be more than 255 characters

Common important contents

The functions of the catalogue are established by convention, and not everything is observed

  • /Root: home directory of super user root
  • /home/username: home directory of ordinary users
  • /usr: installed software, shared libraries and other directories
  • Important subdirectories are
    • /usr/bin: user command
    • /usr/sbin: system administrator command
    • /usr/local: local custom software installation directory
  • /etc: system configuration file
  • /var: data of system services, such as database files, log files and website content
  • /tmp: system temporary file directory

The following two directories are pseudo file systems and are not real. They are generated at startup:

Don't keep files in these two places

  • /proc
  • /sys

Common interesting points

WIP is continuously updated

cd command

  1. cd ..

Use ls to see Is a directory, representing the previous directory Represents the current directory

paxos@Paxos-C26-2021 ~/1> ls  -al
total 0
drwxr-xr-x 1 paxos paxos 4096 Sep 17 18:46 ./
drwxr-xr-x 1 paxos paxos 4096 Sep 17 18:21 ../
  1. Quick one click home directory

Do you often use cd ~, in fact, just enter cd and enter.

  1. Quickly return to the previous working directory

Just enter cd-

  1. Quickly go to other users' home directories
cd ~username

Topics: Linux Operation & Maintenance server