Linux basic commands

Source: Internet
Author: User
Tags line editor

Linux basic commands
Linux Introduction
Kernel Library :. so shared object, windows: dll dynamic link library application Linux basic principles: 1. composed of a single small program with a single purpose; combined with a small program to complete complex tasks; 2. Everything is a file; 3. Avoid capturing user interfaces. 4. Save the configuration file in plain text format. User Interfaces: all user interfaces are a shell. GUI interface (desktop graphical user interface): CLI interface (command User Interface): command prompt, prompt, bash (shell) #: root $: Common USER command: is the execution entry of the application. An application may have multiple entries. Command Format: Command Option parameter option: short option:-multiple options can be combined:-a-B =-AB long option: -- parameter: Command action object virtual terminal (terminal): Ctrl + Alt + F1-F6 Shell: GUI: Graphic User Interface windows(javaser.exe is the Windows desktop program) X-Window Gnome KDE: linux graphical desktop Interface Xface CLI: command Line Interface sh bash csh ksh zsh tcsh prompt, Command prompt: Command: magic number shebang #! /Bin/bash # command options... arguments... option: short option-long option -- parameter: list: ls list, list Directory: file, path ing path: File System from the specified start point to the destination: file system lists the file directories in the specified path: working directory, current directory pwd: Printing Working directory ls-l: long format file type:-: normal file (f) d: directory file B: block device file c: character device file (character) l: symbolic link file p: Command pipeline file (pipe) s: socket file permissions: 9 bits, each 3 bits, three sets of permissions (U, G, O), each group: rwx (read, write, execute ), r --, Group 1: The owner user permission of the file. Group 2: user permissions of the file group, group 3: Number of hard links to other user permission files size of the file owner File ), the Unit is timestamp. The last modified time is access: access modification: modify. The file content has changed: change, metadata, metadata-h: for unit conversion-a: Display. hidden file. indicates the current directory .. indicates the parent directory-A-d: displays the attributes of the directory-I: index node, inode-r: reverse display-R: recursive (recursive) display cd: change directory home directory, Home directory, home directorycd ~ USERNAME: Enter the specified user's home directory cd-: Switch the command type back and forth between the current directory and the previous directory: built-in commands (shell built-in), internal, built-in External commands: in a file system path, there is an executable file type that corresponds to the command name: display the specified type date: Time Management Linux: rtc hardware clock system clock COMMAND help: Internal COMMAND: help COMMAND for example: help cd external COMMAND: COMMAND -- help for example: date -- help COMMAND manual: manualman COMMAND whatis COMMAND: 1: user command (/bin,/usr/bin,/usr/local/bin) 2: System Call 3: Database User 4: special files (Device Files) 5: File Format (Configuration File Syntax) 6: Game 7: Miscellaneous (Miscellaneous) 8: management commands (/sbin,/usr/sbin, /usr/local/sb In) MAN: NAME: command NAME and Function brief description synopsis: usage instructions, including available options description: Detailed DESCRIPTION of the command function, may include the meaning of each option OPTIONS: description of the meaning of each option FILES: configuration file BUGS: EXAMPLES related to this command: use the example see also: ALSO refer to flip screen: Flip back screen: SPACE flip forward screen: B flip one row backward: ENTER flip one row forward: k search:/KEYWORD: backward n: Next N: Previous q: exit exercise: use date to obtain the current year, month, day, hour, minute, and second hwclock-w:-s: Exercise: 1. Is echo an internal command or an external command? 2. What is its role? 3. How do I display echo "The year is 2013." echo "Today is 26." As two rows? Set the system time to be consistent with the hardware time. Cpmv File System: rootfs: root file system/FHS: Linux/boot: files related to system startup, such as kernel, initrd, and grub (bootloader)/dev: device file: block device: Random Access, data block character device: Linear access, by character unit device number: main device number (major) and sub-device number (minor)/etc: configuration File/home: the home directory of the user. The home Directory of each user is usually/home/USERNAME/root by default: The home Directory of the Administrator;/lib: static library of the library file ,. a dynamic library ,. dll ,. so (shared object)/lib/modules: kernel module File/lib64/media: mount point directory, mobile device/mnt: mount point directory, additional temporary file system/opt: optional Directory, third-party program installation directory/proc: pseudo file system, kernel ing file/sys: pseudo file system, property ing file related to hardware devices/tmp: temporary file, /var /Tmp/var: Variable file/bin: executable file, USER command/sbin: absolute path of the management command: path starting from the following directory to the target. Relative Path: Start from the current directory to the target path mkdir: Create an empty directory-p:-v: verbose/root/x/y/z/mnt/test/x/m, ymkdir-pv/mnt/test/x/m/mnt/test/ymkdir-pv/mnt/test/{x/m, y} # tree: view the directory tree to delete the directory: rmdir (remove directory) deletes the empty directory-p file creation and deletion # touch-a-m-t-c # stat View File information to create a file, you can use the file editor nano vi (another file) to delete the file: rm-I before deletion confirm-f before deletion don't confirm-r recursively Delete rm-rf/linux suicide cp: copycp src dest-r-I confirm before overwriting-f-p-a: Archive replication, it is often used to back up a cp file1 file2 file3 file to a file multiple files to a directory cp/etc/{p Asswd, inittab, rc. d/rc. sysinit}/tmp/mv: move the mobile file mv SRC DESTmv-t dest src directory management: ls, cd, pwd, mkdir, rmdir, tree file management: touch, stat, file, rm, cp, mv, nano, vi, vim date and Time: date, clock, hwclock, cal, ntpdate view text: cat, tac, more, less, head, tail split screen display: more, less more: backward flip less: head: view the first n rows of tail: next n rows-n tail-f: Check the end of the file, do not exit, wait for the new content appended to this file to be displayed; Text Processing: cut, sort, join, sed, and awk text files: Tom: 23: male: 2013/05/06 cut:-d: Specifies the field separator. The default value is space-f: Specifies the field to be displayed- F 1, 3-f 1-3 text sorting: sort-n: Numerical sorting-r: Descending Order-t: field separator-k: Which field is used as the keyword for sorting-u: after sorting, the same row is displayed only once.-f: When sorting, ignore case-sensitive characters. Text statistics: wc (word count)-l-w-c-Lsed. Basic usage: sed: stream EDitor Line EDitor (full-screen EDitor: vi) sed: by default, the mode space does not edit the original file, and only processes the data in the mode space. Then, after the processing is complete, print the mode space to the screen. sed [options] 'addresscommand' file... -n: silent mode. Content in the mode space is no longer displayed by default.-I: directly modify the original file-e SCRIPT: multiple scripts-f/PATH/TO/SED_SCRIPTsed-f/path/to/scripts file-r can be executed simultaneously, indicating that the extended regular expression Command: d is used TO delete the matching entries. A \ string: append a new row to the specified row. The content is string \ n: can be used to wrap I \ string: add a new line before the specified row with the content of stringr FILE: add the content of the specified FILE to the qualified row w FILE: save the rows within the range specified by the address to the specified file; s/pattern/string/modifier: Find and replace, by default, only strings matching the pattern for the first time in each line are replaced with the modifier g: Global replacement I: Ignore uppercase/lowercase characters s //: s ###, s @ \ (\), \ 1, \ 2 l .. e: like --> likerlove --> lover like --> Likelove --> Love &: The reference mode matches the entire sed string. Exercise: 1. delete/etc/grub. blank line at the beginning of the row in the conf file; sed-r's @ ^ [[: space:] + @ g '/etc/grub. conf2, replace/etc/init In the tab file, "id: 3: initdefault:" The number in a row is 5; sed's @ \ (id: \) [0-9] \ (: initdefault :\) @ \ 15 \ 2 @ G'/etc/inittab3. Delete blank lines in the/etc/inittab file. sed '/^ $/D'/etc/inittab awk: awk is a powerful text analysis tool. Compared with grep search and sed editing, awk is particularly powerful in data analysis and report generation. To put it simply, awk refers to reading files row by row. Each line is sliced with spaces as the default separator, and the cut part is analyzed and processed. Awk '{pattern + action}' {commands} Where pattern indicates the content that AWK looks for in the data, and action is a series of commands executed when matching content is found. Curly braces ({}) do not always appear in the program, but they are used to group A series of commands according to a specific mode. Pattern is the regular expression to be expressed and enclosed by a slash. Case: The five accounts that have recently logged on # last-n 5 | awk '{print $1}' are read into a record separated by '\ n' line breaks, then, the records are divided by the specified domain separator. If the field $0 is filled, all fields are represented. $1 indicates the first domain, and $ n indicates the nth domain. The default domain separator is "Blank key" or "[tab] Key", so $1 indicates the logon user, $3 indicates the logon user ip, and so on. If you only display the/etc/passwd Account # cat/etc/passwd | awk-F': ''{print $1} 'rootdaemonbinsys is an example of awk + action, action {print $1} is executed for each row }. -F specifies that the domain separator is ':'. If you only display the shell corresponding to the/etc/passwd account and the account, separate the account and shell with a tab key # cat/etc/passwd | awk-F ': ''{print $1" \ t "$7} 'root/bin/bashdaemon/bin/shbin/bin/shsys/bin/sh if only the/etc/passwd account is displayed shell corresponding to the account, the account and shell are separated by commas, and the column name and shell are added to all rows, and "blue,/bin/nosh" is added to the last row ". Copy the code cat/etc/passwd | awk-F': ''BEGIN {print" name, shell "}{ print $1", "$7} END {print" blue, /bin/nosh "} 'name, shellroot,/bin/bashdaemon,/bin/shbin,/bin/shsys,/bin/sh .... blue,/bin/nosh awk workflow is like this: first execute BEGING, then read the file, read a record with/n linefeeds separated, then, the records are divided into Domains Based on the specified domain separator. Fill in the domain. $0 indicates all domains, $1 indicates the first domain, and $ n indicates the nth domain, then start the action corresponding to the execution mode. Then read the second record until all the records are read and the END operation is executed. Search for all rows with the root keyword in/etc/passwd # awk-F: '/root/'/etc/passwdroot: x: 0: 0: root:/root: /bin/bash is a pattern usage example. Only the rows matching pattern (root here) can execute action (no action is specified, and the content of each row is output by default ). Regular Expressions are supported in search. For example, search for all rows with the root keyword in/etc/passwd starting with awk-F: '/^ root/'/etc/passwd, the corresponding shell # awk-F is displayed: '/root/{print $7}'/etc/passwd/bin/bash specifies the action {print $7} awk built-in variable awk has many built-in variables used to set environment information, these variables can be changed. The following lists the most commonly used variables. ARGC command line parameter count ARGV command line parameter arrangement ENVIRON supports the number of records in the FNR file browsed by FILENAME awk for system environment variables in the queue. FS sets the input domain separator, equivalent to the command line-F option, the number of NF browsing records, the number of NR read records, the OFS output domain separator, the ORS output record separator, the RS control record separator, the $0 variable, refers to the entire record. $1 indicates the first domain of the current row, $2 indicates the second domain of the current row, and so on. Statistics/etc/passwd: file name, row number of each row, number of columns in each row, full row content: # awk-F': ''{print" filename: "FILENAME ", linenumber: "NR", columns: "NF", linecontent: "$0} '/etc/passwdfilename:/etc/passwd, linenumber: 1, columns: 7, linecontent: root: x: 0: 0: root:/bin/bashfilename:/etc/passwd, linenumber: 2, columns: 7, linecontent: daemon: x: 1: 1: daemon:/usr/sbin:/bin/shfilename:/etc/passwd, linenumber: 3, columns: 7, linecontent: bin: x: 2: 2: bin: /bin: /Bin/shfilename:/etc/passwd, linenumber: 4, columns: 7, linecontent: sys: x: 3: sys:/dev: /bin/sh replace print with printf to make the code more concise and easy to read awk-F': ''{printf (" filename: % s, linenumber: % s, columns: % s, linecontent: % s \ n ", FILENAME, NR, NF, $0 )} '/etc/passwd print and printfawk both provide print and printf print output functions. The print function can be a variable, a value, or a string. The string must be referenced in double quotation marks and the parameters must be separated by commas. If there are no commas (,), the parameters are connected together and cannot be distinguished. Here, the comma serves the same purpose as the separator of the output file, except that the latter is a space. The printf function is similar to the printf function in C language. It can format strings. When the output is complex, printf is easier to use and the code is easier to understand. In addition to the built-in variables of awk, awk programming variables and value assignment can also customize variables. The following statistics show the number of accounts in/etc/passwd awk '{count ++; print $0 ;}end {print "user count is", count}'/etc/passwdroot: x: 0: 0: root:/bin/bash ...... user count is 40count is a custom variable. In the previous action {}, only one print exists. In fact, print is only a statement, and action {} can have multiple statements separated by a comma. Count is not initialized here. Although the default value is 0, the proper method is to initialize it to 0: awk 'in in {count = 0; print "[start] user count is ", count} {count = count + 1; print $0;} END {print "[end] user count is ", count} '/etc/passwd [start] user count is 0 root: x: 0: 0: root:/bin/bash... [end] user count is 40 count the number of bytes occupied by files in a folder. ls-l | awk 'in in {size = 0 ;}{ size = size + $5 ;} END {print "[end] size is", size} '[end] size is 8657198. If it is displayed in the unit of M: ls-l | awk' BEGIN {size = 0 ;}{ size = size + $5;} END {print "[end] size is", size/1024/1024, "M"} '[end] size is 8.25889 M. Note that the statistics do not include subdirectories of folders. The conditional statements in the conditional statement awk are used for reference in the C language. See the following declaration method: copy the Code if (expression) {statement ;......} if (expression) {statement;} else {statement2;} if (expression) {statement1;} else if (expression1) {statement2;} else {statement3 ;} count the number of bytes occupied by files in a folder and filter out files of 4096 size (usually folders): ls-l | awk 'in in {size = 0; print "[start] size is", size} {if ($5! = 4096) {size = size + $5 ;}end {print "[END] size is", size/1024/1024, "M"} '[end] size is 8.22339 M the cyclic statement in awk is also used in C language, and supports while, do/while, for, break, and continue, the semantics of these keywords is exactly the same as that in C. The subscript of an array in awk can be numbers and letters. The subscript of an array is usually called a key ). Both values and keywords are stored in an internal table that uses hash for key/value applications. Because hash is not stored in sequence, you will find that the array content is not displayed in the expected order. Arrays and variables are automatically created when they are used, and awk automatically determines whether they are stored as numbers or strings. In general, arrays in awk are used to collect information from records. They can be used to calculate the sum, count words, and track the number of times the template is matched. Show/etc/passwd account awk-F': ''BEGIN {count = 0 ;}{ name [count] = $1; count ++ ;}; END {for (I = 0; I <NR; I ++) print I, name [I]} '/etc/passwd0 root1 daemon2 bin3 sys4 sync5 games ...... here we use the for loop to traverse the array of linux Quotes 1. The quotation marks are ', the command is replaced with 2, the single quotation marks:', the string 3, the double quotation marks: "", and the variable replaces the pipe: command 1 | command 2 | command 3 |... exercise: 1. count the number of files in the/usr/bin/directory; # ls/usr/bin | wc-l determine whether the/home/goldin directory contains files. 2. Retrieve the shell of all users on the current system. It is required that each shell be displayed only once, and displayed in order; # cut-d:-f7/etc/passwd | sort-u4; Retrieve the 6th rows of the/etc/inittab file; # head-6/etc/inittab | tail-15. Retrieve the usernames and shells of the last 9th users in the/etc/passwd file, display it on the screen and save it to the/tmp/users file; # tail-9/etc/passwd | head-1 | cut-d:-f1, 7 | tee/tmp/users6, display all files starting with pa in the/etc directory, and count the number of files; # ls-d/etc/pa * | wc-l user management: useradd, userdel, usermod, passwd, chsh, chfn, finger, id, chage Group Management: groupadd, groupdel, groupmod, gpasswd permission management: chown, chgrp, chmod, umask/etc/passwd: User name: Password: UID: GID: Note: Home Directory: default SHELL/etc/group: group Name: Password: GID: list of users to which this group is attached/etc/shadow: User name: Password: last password change time: shortest life: maximum life: warning time: inactive time: expiration time: User Management: useradd, userdel, usermod, passwd, chsh, chfn, finger, id, chage useradd [options] USERNAME-u UID-g GID (basic group)-g gid ,... (Additional group)-c "COMMENT"-d/path/to/directory-s SHELL-m-k-M-r: add system user userdel: userdel [option] USERNAME-r: Delete the user's home directory id at the same time: view the user's account attributes-u-g-G-n finger: view User Account Information finger USERNAME modify user account attributes: usermod-u UID-g GID-a-g gid: Do not use the-a option, will overwrite the previous additional group; -c-d-m:-s-l-L: Lock account-U: Unlock account chsh: Modify the user's default shell chfn: Modify the comment information password management: passwd [USERNAME] -- stdin-l-u-d: delete user password pwck: Check User Account Integrity Group Management: Create group: groupaddgroupadd-g GID-r: add as system group groupmod-g GID-n gr name groupdel gpasswd: Set the password newgrp/name for the group <--> exit chage-d: last modification time-E: expiration time-I: inactive time-m: shortest life-M: shortest life-W: warning time permission management: r: w: x: 111 101 101 three types of users: u: Owner g: group o: other users chown: Change the file owner (Only Administrators can use this command) # chown USERNAME file ,... -R: Modify the Directory and its internal file owner -- reference =/path/to/somefile file ,... chown USERNAME: Maid file ,... chown USERNAME. kgname file ,... # chgrp kgname file ,... -R -- reference =/path/to/somefile file ,... chmod: Modify file permissions modify three types of user permissions: chmod MODE file ,... -R -- reference =/path/to/somefile file ,... rwxr-x --- ps-aux netstat-ntpl grepfind yum install lrzsz Upload File rz-linux security login and other logs view limits some IP addresses to ssh Login... change the password once a month. Set the password to a special symbol, in upper case, in lower case, 1223455. Add the rule configuration file with at least 8-bit firewall rules.

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.