Linux Common commands encyclopedia (linux Basic Command + command Memo + interview review)

Source: Internet
Author: User
Tags bz2 create directory parent directory system log cpu usage file permissions install redis yum repolist


Date: 2018.7.3

Foreword this article characteristic
    • to teach the fishing : Learn the command learning method, use: No longer rote, refuse to aimless;

    • accurate : All commands are executed through (environment for CENTOS7), refuse to copy paste;

    • High practicality : commands are used in practical work, examples are explained, rejecting pure theory;

    • Clear : Classification and induction, quickly find the desired command, refuse to unrestrained;

    • Summary of strong : Only the common syntax, error-prone situation emphasized, refused to copy the set.

Pre-school Information
    • applicable people : development, testing, operation and maintenance
    • purpose of the document: Linux command Learning, command memos, interview review
    • Learning pathway : Books (Bird's-brother's Linux private dishes), Video: Mu class network, Baidu Google search, QQ group, etc.
    • Learning Environment : virtual Machine VIRTUALBOX+CENTOS7
    • Learning record : Multi-hands, multi-associative, multi-record, especially when encountering problems
    • Encounter problems : Baidu Google search, System log (/var/log/), QQ Group
Preschool theory
    • Linux Main features : Everything and files (directories, hard disk, etc. are files), hardware are in the/dev directory, such as hard disk, USB flash drive is/dev/sd[a-d],/dev/sr0 (/dev/cdrom) is the device name of the optical drive (DF Command view), for the device file, Represents the optical drive itself, it is necessary to mount the device to the directory (generally/mnt) (file system temporary mount point), in order to read and write files on the device operations;

    • do not understand the command : Man command (check usage, full name), only remember the key words, can be man-k keywords;

    • common structure of Linux commands :Command [-option] [argument]
      Command: That is to run the commands themselves, plainly is a software (program);
      Option: is optional, option is to control command run state and behavior (can be multiple options together, such as DF-HT);
      Argument: is the parameter (optional), is the command to manipulate objects such as files, paths, data, directories, etc.;
      In the first part of the instruction Press [tab] key for [command completion], two for all command selection, in the non-first part of the [tab] key two for [file completion];

    • Linux commands are case sensitive ;

Switch machine
    • Sync : Write the in-memory data to disk (you need to perform sync before shutting down and restarting)
    • shutdown-r now or reboot : Restart immediately
    • shutdown-h Now: Turn off the machine immediately
    • shutdown-h 20:00 : Scheduled time to shut down the system (8 o'clock in the evening, if it is more than 8 points, tomorrow night 8 o'clock)
    • shutdown-h +10 : Scheduled time off system (10 minutes after shutdown)
    • shutdown-c : Cancel to shut down the system at a predetermined time
System Information
  • Who am I : View the currently used terminal
  • who or w : View all Terminals
  • uname-m : Displays the processor architecture of the machine (e.g. x86_64)
  • cat/proc/version : View Linux version information
  • uname-r : Displays the kernel version in use
  • Rpm-qa | grep kernel-devel : View the Kernel-devel version (compile the kernel when installing the software, so you need to maintain kernel version consistency)
  • Yum install-y "kernel-devel-uname-r = = $ (uname-r)": Installs and matches the Linux kernel version kernel-devel
  • Date: Displays the system date (date +%y/%m/%d: Display effect as of 2018/01/01)
  • Date 070314592018.00 : Sets the time (in the form of a month and year). seconds)
  • clock-w : Saving time changes to the BIOS
  • Cal 2018 : Show Calendar for year 2018
  • Clear : Empty the command line
  • ifconfig : Displays or sets the network card (check IP, etc.) (similar to ipconfig in Windows)
  • ping-c 3 : Test the connection between Baidu and native (-C 3 means test 3 times)
  • cat/proc/cpuinfo : Displaying information about the CPU
  • cat/proc/cpuinfo| grep "Physical id" | sort| uniq| wc-l : View the number of physical CPUs
  • cat/proc/cpuinfo| grep "CPU Cores" | Uniq : View the number of cores per physical CPU
  • cat/proc/cpuinfo| grep "Processor" | Wc-l : View the number of logical CPUs that are threads
System performance
    • Top : Dynamic real-time display of CPU, memory, process usage (similar to Task Manager under Windows)
    • top-d 2-p 7427 :-D for the number of seconds to update the screen, default 5 seconds,-p for the specified process PID information
    • Vmstat 2 : Collect server Status every 2 seconds, collect 10 times (view memory, IO Read and write status, CPU)
    • free-h : Viewing system memory and virtual memory usage
    • df-h : Show disk space usage
    • iostat : can check IO read/write, CPU usage
    • sar-u 3 5 : View CPU Usage (3 seconds, total 5 times)
    • sar-d 2 3 : Evaluating Disk Performance
    • PS aux|grep Firefox : Get the process number (PID) of Firefox (can see the path of CPU, memory percentage and process trigger instruction)
    • kill-9 Process number : Force kill Process
Files and directories

? CD: is an abbreviation for change directory, used to switch working directory, syntax: CD [relative or absolute path or special symbol]

  • CD : Enter the user's home directory ~ (root user is/root, other user is/home/user name)
  • CD.. : Return to the previous level of the directory (note to space)
  • CD- : Return to the last directory
  • CD/ : Return root directory (absolute path)
  • CD./directory 1/Directory 2 : Enter subdirectories under current directory (relative path)
  • pwd : Show work path (abbreviation for Print working Directory)

    LS: is the list's abbreviation for listing files in the directory, syntax:ls [选项][目录或文件名]

  • ls-a : Lists all files under the file, including "." Hidden files at the beginning
  • **LS-LH *.log**: Lists the details of the file (the. Log end, * is a wildcard for any number of characters)
  • File or directory : Displays the type of file (directory, text, zip, shell script, etc.)

  • mkdir dir1 : Create directory (DIR1) (mkdir as the abbreviation for Make directory)
  • mkdir-p./dir1/dir2 : Recursive Create directory (-P: Parent directory does not exist, simultaneous creation)
  • Touch a.txt : Create file A.txt

    RM: You can delete one or more files or directories in a directory, or you can delete a directory and all of its subordinate files and subdirectories; Syntax: RM (option) (parameter) (note: If the parameter contains a directory, you must add the-r option);

  • RM files : Deleting files
  • rm-r directory or file : Delete directory (and all Files under directory) (Non-empty)
  • rm-rf directory or file : Force Delete, such as: RM-RF * To delete all files in the current directory

    ? MV: is the abbreviation of move, can be used to cut moving files, directories or renaming files;
    Syntax: MV source file Destination file (renamed) or directory (mobile);

  • mv a B : Move or rename a file or directory (the existing move directory or overwrite file, the name does not exist)
  • mv/opt/git/g/opt/a : Move g to opt directory and rename to a (a directory does not exist, if present, move G to a directory)
  • mv-t./test a.txt b.txt : Move multiple files to a directory

    CP: Copy files or directories; CP command can copy single or multiple files to an existing directory;
    Common: Cp-ai file or directory target directory;

  • cp-ai/opt/abc/opt/git/ : Copy the ABC directory (or file) into the Git directory (option A means that the properties of the file are also copied, all files in the directory are copied; I means ask before overwriting)

    Ln:link abbreviation for establishing a hard (soft) link, often used to build soft links (similar shortcuts) to path in software installation;
    Syntax: LN [-S] Source file destination file

  • ln-s/opt/a.txt/opt/git/ : Creating a soft link to a file (shortcut not renamed or a.txt)
  • ln-s/opt/a.txt/opt/git/b : (the shortcut is renamed B) (the following can be renamed)
  • ln-s/opt/mulu/opt/git/ : Creating a soft link to a directory
  • ln/opt/a.txt/opt/git/ : Creating a hard link to a file

File permissions
    • chmod [-r] 777 file or directory : Set permissions (chmod a+rwx a=chmod Ugo +rwx A=chmod 777 a)

      Note: R (read) corresponds to 4,w (write) corresponding to 2,x (execute) to perform the corresponding 1;
      -R: Recursively change the group of files, that is, when changing the genus of a directory file, if you add the-r parameter, then all the files in that directory will change the genus Group)

    • chmod [{ugoa}{+-=}{rwx}][文件或目录]: such as chmod u-w,g+x,o=r test.txt for the user (owner) remove the Write permission, group (owning group) plus execute permissions, and other (others) permissions equal to read-only;
    • Chown [-R] admin:root/opt/ : Change file and directory owner and owning group (-R recursively handles all files and folders, admin is owner, Root is the owning person)

File Lookup
    • Locate A.txt : Find files with a.txt words in the system global scope (faster than find);

Locate: The principle is that updatedb will put the information in the file system into the database databases (but usually only once a day, so locate can not find the newly created file, you need to manually execute UpdateDB, then execute locate), Locate reading data from the database;

? Find: Searches for files in the directory structure and performs the specified actions
Syntax: Find Pathname-options [-print-exec ...]
Pathname: The directory path that is found for the Find command. For example, use. To represent the current directory, and/to represent the system root directory (find finds all files and directories in the target directory and its subdirectories);
The-exec:find command executes the shell command given by the parameter to the matching file. The corresponding command is in the form of ' command ' {}; Note the space between {} and;
The-print:find command outputs the matched file to the standard output;

    • find/home-mtime-2 : Check the files changed in the last 2*24 hours at/home
    • Find .-size +100m : Look for files larger than 100M in the current directory and sub-directories
    • find.-type f : F means file type is normal file (b/d/c/p/l/f for block device, directory, character device, pipe, symbolic link, normal file)
    • find.-mtime +2-exec rm {}; : Find the file that changed time at 2 * 24 hours ago and delete it * *
    • **find. -name ' *.log '-exec grep-i Hello {}; -print**: In the current directory and subdirectories to find the file name suffix. log files and the contents of the file contains a Hello Word and print,-exec command {} \ for the isolated file operation,-I is not case-sensitive;
    • **find. -name ' *.log ' |grep hello**: Locate the file with the. log file name suffix in the current directory and subdirectories and the filename contains the Hello Word (grep is used to process the string);

    • grep-i ' Hello '.-r-n : Finds files in the current directory and subdirectories that contain HELLO in the file contents and displays the file path (-I for ignoring case)

    • which Java : finds eligible files in the directory where the environment variable $path is set and displays the path

View the contents of a file
    • Cat [-n] File name : Displays the contents of the file, along with the line number

    • Less file name : page-by-page display of file contents (search page with the man command)

    • head [ -n] File name: Displays the file header n rows, n specifies how many rows are displayed

    • tail [-nf] File name : Displays the last few lines of the file, n specifies how many rows are displayed, F is used to track all updates to the file in real time, and is often used to view the log file that is changing (such as Tail-f-N 3 a.log to start displaying the final 3 rows, and append the display in real time when the file is updated , no-n default 10 rows)

    • sed-n ' 2, $p ' ab : Displays the second line to the last row;
    • sed-n '/search keyword/p ' a.txt : Display includes keywords in the row

    • Less a.txt|grep git : Displays the line where the keyword is located, the pipe symbol "|" It can only handle the correct output from the previous instruction, and has no direct processing power to the error message information. It is then passed to the next command as a standard input;

    • cat/etc/passwd |awk-f ': ' {print $} ' : Show first column

Text Processing
    • ls-l>file : Output REDIRECT > (changes the default execution of the original system command): Ls-l command results are output to file files, if present, overwrite

    • cat file1 >>file : Output redirect cat command result output appended to file

    • ls fileno 2>file : 2> indicates redirection standard error output (file does not exist, error message saved to file);

    • Cowsay <a.txt : Redirect standard input ' command < file ' means input of the file as a command (for reading data from a file as input)

    • sed-i ' 4, $d ' a.txt : Delete line fourth to the last line ($ = last row) (SED can be added or modified to find the contents of the file)
    • sed-i ' $a added string ' a.txt : Increments the string on the next line of the last line
    • sed-i ' s/old/new/g ' a.txt : replace string, format sed ' s/to replace string/new string/g ' modified file

    • vim file : Edit view file (same VI)

Users and Permissions
    • useradd User name : Create user
    • userdel-r User name : Delete User: (-r means to delete the user's home directory)
    • usermod-g Group name User name : Modify User's Group

    • passwd [ludf] User name : Users change their own password, do not need to enter the user name, option-D: Specify an empty password,-l: Disable a user,-u unblock a user,-F: Forcing users to change the password the next time they log on

    • groupadd Group name : Create user Group
    • Groupdel User group : Deleting a group
    • groupmod-n New group name old group name: Modify user Group name

    • Su-username : Full switch to a user environment (equivalent to login) (this is recommended) (Exit User: Exit)
    • su user name : Switch to the user's identity (environment variables are not changed, resulting in many commands to add an absolute path to execute)
    • sudo command : Execute command as root (enter user's own password, and SU is input to switch user's password, normal user needs to set/etc/sudoers to use sudo)

Disk Management
    • df-h : Show disk space usage and mount points
    • df-h/var/log : (Displays the log partition (mount point), the disk on which the directory is located, and the available disk capacity)

    • du-sm/var/log/* | Sort-rn : Sort by disk space size (MB) file and directory size under a directory

    • fdisk-l : Check all partitions and total capacity, add/DEV/SDA to check hard disk a partition)
    • Fdisk/dev/sdb : Partitioning the hard disk SDB

    • mount/dev/sda1/mnt : HDD sda1 mount to/mnt directory (Mount appliance filename mount point)
    • mount-t cifs-o username=luolanguo,password=win user account password, vers=3.0// : Remote Linux Share mount Windows USB drive, G for the USB drive share name, need to set up USB drive sharing
    • mount-o loop/opt/soft/centos-7-x86_64-dvd-1708.iso/media/centos : Mounting ISO file
    • umount/dev/sda1 : un-mount (umount appliance file name or mount point)

Compress, decompress, and package backups

? Only tar is packaged (multiple files are wrapped into one large file), plus the parameter-j (bzip2 format. bz2),-Z (gzip format. GZ) can be backed up, compressed (-C), decompressed (-X), backup is generally more than the compression parameter-p (retain the permissions and attributes of the original file),- C can be specified to extract to a specific directory; bzip2, gzip only for a single file compression;

    • File file name : Check the type of files (can be seen in which way compressed)

    • tar-zxvf a.tar.gz-c./test : Unzip the tar.gz to the current directory in the test directory
    • tar-zcvf/opt/c.tar.gz./a/ : Compress tar.gz (to compress all files in directory A and directory under current directory to c.tar.gz in/opt/directory)

    • TAR-JXVF a.tar.bz2 : Unzip tar.bz2 (to current directory)
    • tar-jcvf c.tar.bz2./a/ : Compress tar.bz2 (to compress all files under the directory under the current directory to the current directory as C.TAR.GZ2)

    • Unzip : Unzip zip (to current directory)
    • zip-r : Compress zip (to compress all files under the directory under the current directory to the current directory for

    • bzip2-k file1 : Compress a ' file1 ' file (-K means keep source file) (bzip2 format, better than gzip)
    • bzip2-d-K file1.bz2 : Unzip a file called ' file1.bz2 '

    • gzip file1 : Compress a file called ' file1 ' (gzip format) (cannot keep source file)
    • gzip-9 file1 : Maximum compression
    • gzip-d file1.gz : decompressing a file called ' File1 '

Software Installation
  • As far as possible with the Yum source (apt-get) installation, not the RPM, Deb package installation, can not be compiled manually do not compile manually;
  • Dpkg can only install the Deb package that has been downloaded to the local machine. Apt-get can download and install the Deb package online, can update the system, and can also automatically handle the dependencies between package and package, this is not the dpkg tool;
  • RPM can only install RPM packages that have been downloaded to the local machine. Yum can download and install the RPM package online, update the system, and automatically handle the dependencies between package and package, which is not available in the RPM tool;
  • Yum, RPM installation files distributed in/Usr bin, lib, share different directories, without the configuration path, directly with the command, but the use of commands to uninstall the update;
  • Manually compile the software, the default location is/usr/local under different sub-directories, without configuring the path directly with the command (manually specify the installation path needs to add path), making software updates and deletes become cumbersome. The software that compiles and installs does not have the uninstall command, the uninstall is to remove all the files of this software.
Binary (Binaries) package Yum Installation

? Download and install RPM packages online for CentOS, Fedora, Redhat and similar systems

    • Yum Install Epel-releas : Installing third-party yum Source Epel (abbreviated for Enterprise Linux add-ons)
    • Yum repolist enabled : Displays the available source repositories (configured under the/etc/yum.repos.d/directory)
    • Yum Install yum-fastestmirror : Automatically selects the fastest yum source
    • Yum List installed |grep Java : List installed software (view installed JDK)
    • Yum Remove java-1.8.0-openjdk.x86_64 : Uninstalling the Software (uninstalling the JDK)
    • Yum list java* : List installed and installable software (view JDK packages in the Yum library)
    • yum install [-y] java-1.8.0-openjdk : Installing the Software jdk (-y automatic installation) (Recommended for installation in this way)
    • yum check-update [kernel] : List all updatable software (check for updates kernel)
    • Yum update tomcat : Update software (available all)
    • RPM-QL software name : Query yum installation path (software name available via Rpm-qa|grep Java)
    • Yum Info kernel : View software (kernel) information
    • Yum Clean All: (clear the cache so that the latest Yum configuration takes effect)
RPM Package Manual Download installation

? No time use in Yum for CentOS, Fedora, redhat and similar systems;

    • wget-p/opt https://url : Download to/opt directory

    • RPM-IVH wps-office-version. x86_64.rpm : Install RPM Package (package to download first) (to load the dependency package first)
    • rpm-e wps-office : Uninstall software (note not software name not version number)
    • Rpm-qa |grep wps : View installed RPM packages
    • RPM-QL software name : View RPM Package Installation path (software name available via Rpm-qa|grep Java)

Apt mode installation

? Install the Deb package, similar to Yum installation, for Debian, Ubuntu and similar systems;

    • apt-get Install aptitude : Installing the aptitude tool, enabling automatic installation dependent, dependent version auto-downgrade or upgrade
    • aptitude Install software : Installing software (recommended this way)

    • Apt-cache Search software : Searching software
    • apt-get Install software : Installing software
    • apt-get purge software : Uninstall software (including configuration files, remove software only purge replace with remove)
    • apt-get upgrade : Update all installed Packages
    • apt-get update : Upgrading packages in the list
    • apt-get Clean: Cleaning up the cache from the downloaded package

Deb Package Installation

? Suitable for Debian, Ubuntu and similar systems;

    • dpkg-i package.deb : Install a deb package
    • dpkg-r package_name : Remove a Deb package from the system
    • dpkg-l |grep Chrome : Query all installed Deb packages in the system
    • dpkg-l software name : Check the Software installation files
Unzip ready to use

? Most non-open-source commercial software adopts this approach;

? Binary (Binaries) package such as apache-jmeter-3.3.tgz, download copy extract to/opt, then add the bin directory of the software to the PATH (Vim/etc/profile export path= $PATH:/opt /apache-jmeter-3.3/bin);

Software's own module/Package Manager

? such as Python: the source of the system may not contain all the modules of the software, the system's source of the software module updates are far behind the latest version; Manually install Python and install the module (similar to Yum) with a python-brought Pip;

    • pip Install redis : Install python package Redis
    • pip Unstall redis : Uninstall
    • pip show--files redis :p IP View installed packages
    • pip list--outdated : Check for updates
Source Code Package compilation installation

? Source code package (usually have install file) such as hello-2.2.tar.bz2, download copy to/opt;

    • TAR-JXVF hello-2.2.tar.bz2 : Unzip
    • ./configure--prefix=/opt/Software Catalog name : Prepare for compilation, plus prefix manually specify the installation path
    • make: Compile
    • Make install : Install
    • make clean: Deletes temporary files that are generated during installation
    • vim/etc/profile export path= $PATH:/opt/directory/bin : Manually specify the installation path and path is required
    • Hello : Execution software: Look at the Install and Readme files (whether the source package, how to install, execute all look at these two)
    • RM-RF Software Catalog name : Uninstalling software

Reprint Please specify source:

Linux Common commands encyclopedia (linux Basic Command + command Memo + interview review)

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: 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.