Linux may forget the command collation

Because too common we will, too uncommon encountered also less, so to sort out the common or because too common to forget the details of the command good.

Time can have again this, suddenly found notes before doing or very serious/omega \ * (*) : yuanlier. Making. IO/categories /…

Files, directories, and search

Do I need to add -r to copy directories? What about moving and renaming?

  • Copy directory: cp
    • -r is required, otherwise an error will be reported
  • Move or rename: MV
    • I don’t have to add minus r

When is the destination directory required to exist for a copy or move operation?

  • Cp file dir/mv file dir:
    • Dir must exist
  • Cp -r dir1 dir2 / mv dir1 dir2:
    • If dir2 does not exist, one is automatically created

That is, if the target to be moved is a file, the target directory must exist. If the target to be moved is a directory, the target directory can be created automatically.

Rm -rf what does -RF refer to respectively?

  • -r: deletes a directory recursively
    • That is, if the directory to be deleted has subdirectories, the subdirectories will also be deleted
  • -f: ignores non-existent files without prompting for confirmation
    • If you do not add this, you will check whether a file needs to be deleted before deleting it
    • That means always yes, yes, yes

What is the difference between soft links and hard links? What are the advantages?

Hard link: ln

  • Cannot reference a file that is not on the same disk partition as the link;
  • Can only operate on files, cannot reference directories;
  • After deleting the original file, you can still open the file through the hard link

Soft link: ln-s

  • Almost exactly the same as shortcuts on Windows;
  • It’s best to type absolute path for creation otherwise it may be somewhat troublesome to comment on _(:з “Angle)_

The biggest difference between hard and soft links: hard links have the same I nodes and storage blocks as the original file, which can be regarded as the same file, equivalent to two doors of a classroom; Soft links have their own I nodes and blocks that store the file name and I node number of the original file rather than the actual data.

File search collection

Quick search: locate

  • Basic format: locate [filename]
  • Features:
    • Search in the background database faster than find
    • Cannot search for newly created file (because it takes time to write to database)
    • Only files and directories can be searched

Command to search whereis/which

  • Basic format: whereis/which [command name]
  • Features:
    • You can also use whereis with the option ==> -b to find only executable files; -m Searches only help files
    • Unable to find system native commands, such as CD

File search: find

  • Basic format: find [search scope] [options] [search criteria]
  • Features:
    • Default absolute match, if you want to fuzzy query to add a wildcard
    • Wildcards must be enclosed in double quotation marks
  • Common options:
    • -iname: indicates case-insensitive name matching
    • -mtime-10/10 /+10: searches for files modified within the last 10 days, 10 days before today, or 10 days before today.
    • – size-25K / 25K /+ 25K: searches for files whose size is smaller than, equal to, or greater than 25 KB in the current directory
    • -a: logic and; -o: logical or

String search: grep

  • Basic format: grep [option] [string] [filename]
  • Find:
    • Find: Searches the system for matching filenames, using a full match
    • Grep: Searches the file for the line containing the string, using a regular expression (including matches)

redirect

Standard output redirection: >

  • Basic format: [command] > [file name]
  • Features:
    • Only standard output is redirected
    • By default, overwrites start at the beginning of the file
    • Can be used to empty a file or create a new empty file, such as: > [filename]
    • If you want to append content from the tail, just replace > with >>

Standard error Redirection: 2>

  • Basic format: [command] 2> [file name]
  • Features:
    • In fact, 0, 1, 2 correspond to standard input, standard output, standard error file respectively

Redirect standard output and standard error to the same file: &>

  • Basic format: [command] &> [file name]

Handling unwanted output:

  • Redirect to /dev/null
  • /dev/null: This is a system device called a “bit bucket” that accepts input but does nothing with it; You can usually use it to swallow error messages so they don’t show up anywhere

Permission and user management

File attributes

  • Example: – rw – rw – r –
  • File attribute classification:
    • Bit 1: File type
    • 2-4 bits: owner rights
    • 5-7 bits: group permission
    • 8-10 bit: Other user rights
  • File type symbol description:
    • – : common file
    • D: Directory file
    • L: Symbolic links. Note that for symbolic links, the remaining file attribute is always RWXRWXRWX, which is a pseudo-attribute value; The properties of the file to which the symbolic link points are the real properties of the file.
  • Permission attribute symbol description:
    • R: The file is readable
    • W: The file is writable
    • X: The file is executable

Change file mode: chmod

Octal numeric representation:

  • Common examples are:
    • 7:111 => RWX
    • 6 :110 => rw-
    • 5:101 => R-x
    • 4:100 => r
    • 0:000 => —

Symbolic representation:

  • The directive is divided into three parts:
    • Who will be affected by the change:
      • U: indicates the owner of a file or directory
      • G: indicates the group to which a file belongs
      • O: Others, indicating all other users
      • A: All means all of the above three types
    • Which operation is to be performed:
      • + : Adds a permission
      • – : Deletes a permission
      • = : All permissions except the specified permission are deleted
    • The permissions to set are:
      • R/w/x
  • Common examples are:
    • U +x: adds executable permissions to the file owner
    • U-x: deletes the executable permission of the file owner
    • +x: equivalent to a+x
    • U +x, go=rx: Add the executable permission for the file owner and set the degree and execution permission for the owning group and all other users. Use commas (,) to separate multiple permissions

To change the status

Su: Run the shell as another identity

  • Basic format: su [-[l]] [User name]
  • Matters needing attention:
    • If the -l option is included, the specified user’s runtime environment will be loaded and its working directory will be changed to the specified user’s home directory, which is often required
    • If no user is specified, the superuser root is assumed by default
    • -l can be abbreviated as -, and is often used, as su –
    • When you use su to switch the identity, you need to enter the password of the user

Sudo: Execute commands as another identity (usually superuser)

  • Basic format: sudo [command]
  • Matters needing attention:
    • You do not need to enter the password of the super user. You only need to enter your own password for authentication

Chown: Changes the file owner and group

  • Basic format: chown [new owner] [: new group]

Changing a User password

  • Basic format: passwd [user name]
  • Matters needing attention:
    • If you want to change your own password, you do not need to enter a user name

User management

Create and delete groups and users:

  • Add a new group: groupadd [option] Group name
  • Delete a group: groupdel [option] Group name
  • Add a user: useradd [option] user name
    • -m: automatically creates a user home directory. This is a good thing to add when you create a user
    • -g Group name: specifies the group to which the user belongs. Otherwise, a group with the same name as the user will be created. For example, useradd -m -g Group name Indicates the user name
  • Delete a user: userdel [option] user name
    • -r: Deletes the user home directory
  • Group member change: gpasswd [option] User name Group name
    • -a Adds a user to a group
    • -d Deletes the user from the group

Useradd and adduser:

After useradd is created, you need to manually set the password and assign permissions to this user, as if creating a “three no” user, the actual operation will be tedious; So we can directly use adduser [user name] to create, it will prompt us to set a password during the creation process, and the successful creation of the user has all the rights of ordinary users, it will be more convenient to use.

Process management

View the process running status: ps

  • Common examples are:
    • Ps aux: Displays all processes in the system and displays process users (warning if “-” is added before the BSD style option)
    • Ps -ef: Displays detailed information about a process and displays the relationship between the process and its parent
    • Ps – ef | grep Java: select all the specific process, this is Java
    • Pstree: Displays the process tree of the current system
    • Ps x: Displays all processes, regardless of terminals

To send a signal (but is usually used to terminate a process) : kill

  • One is with ps – ef | grep XXX use:
    • Ps – ef | grep find out about XXX and XXX process of pid
    • Kill -s 9 PID End the process
    • Note: “9” indicates that the signal passed to the process is 9, that is, to force and terminate the process as soon as possible
  • You can also use pkill or killall to end a process by its name
    • Pkill -9 XXX, give a part of the process name to terminate all related processes
    • Killall-9 XXX, but this must give the name of the completed process, otherwise an error will be reported

Process switching between front and back

  • Background run: &
    • When a Linux terminal runs a command, it adds an ampersand to the end of the command to make the program run in the background
    • Xlogo &, for example, allows the window to go to background execution
  • Switch the running program to background: bg
    • If the program is running in the foreground, use Ctrl+Z to pause the program
    • The program is then run in the background with bg %[job number]
  • Switch running programs to foreground: fg
    • The operation mode is basically the same as bg
  • Look at the program running in the background: Jobs-l
    • This is a very easy way to look up the job number

Networks are related to communication

Network inspection and monitoring

  • Ping: sends special packets to the network host to check whether the network connection is normal
  • Traceroute: Traces the transmission paths of network packets
    • Note: For those routers that do not provide identity information due to router configuration, network congestion, or firewalls, they are directly represented by ***
  • Netstat: Checks network Settings and related statistics
    • Netstat -ie: checks network interface information in the system

Secure communication with remote hosts

  • Log in to the remote host over SSH
    • Basic format: SSH username @IP address (-p port number)
  • Linux -> Linux file transfer:
    • Local -> Remote: SCP [local path] [remote username]@[remote IP address]:/[storage path]
    • Remote -> Local: SCP [remote username]@[remote IP address]:/[file path] [local storage path]
    • Note that -r is required to upload directories
  • Linux -> Windows file transfer:
    • SCP [inux file path] [username]@[INdows IP address]:/[Indows file path]
  • Windows -> Linux File transfer:
    • SCP [username]@[INdows IP address]:/[Indows file path] [Inux file path]

Archiving and backup

Compression and decompression

The zip file:

  • Zip [compressed file name] [source file]
    • Note that -r is used to compress the directory
    • The source file will be retained after compression
  • Unzip: unzip [compressed file]
    • Unzip the directory directly, without adding -r
    • The source file will be retained after decompression

Gz file:

  • Zip: gzip [source file]
    • To compress a directory, you need to add -r, but this will not compress the entire directory into a single file, but will compress the files in the directory individually
    • The compressed source file disappears
  • Extract: gzip -d [compressed file]
    • You can also unzip it with gunzip, as with zip
    • You need to add -r to decompress the directory
    • The source file will disappear after decompression

Bzip2 file:

  • Compression: bzip2 [source file]
    • If you want to preserve the source file, add -k
    • The bzip2 command cannot compress directories
    • This algorithm has high quality data compression ability, but reduces the compression speed
    • The compressed file name ends with. Bz2
  • Unzip: bunzip2 / bzip2-d
    • Again, adding -k preserves the source file

Tar – Archiving of files:

  • How it works: Package the directory as a. Tar file, and then compress it to end with.gz/.bz2
  • .tar.gz compression and decompression:
    • Zip: tar -zcvf [zip package name] [source file]
    • Decompress: tar -zxvf
  • .tar.bz2 compression and decompression:
    • Compressed: tar -jcvf [compressed package name] [source file]
    • Decompress: tar -jxvf [package name]

A storage medium

Mount and unmount disks

The Linux operating system with a GRAPHICAL user interface (GUI) can automatically mount the USB flash drive, but you need to use the mount command to mount the USB flash drive manually.

Run the fdisk -l command to view the disk list when mounting the disk. Then insert the USB flash drive and check again. The extra disk is the USB disk we want to mount, remember its device name;

Create a directory as the mount point (usually under/MNT), and then mount it using mount: mount [device name] [newly created directory];

Use the CD command to go to the directory you just created, and then type ls to view the information on the USB drive.

After the USB flash drive is used, you need to unmount [device name] to avoid damage to the USB flash drive or data loss. Remember to exit the mount point directory before unmounting.


The others are replenished as they come across