Beginners Linux Basic Command Summary __linux

Source: Internet
Author: User
Tags bz2 chmod mkdir touch command

LS Displays current directory contents
1 ls/(Display all directories and files under the root directory)
2 Ls-l/(displays all directories and files in the root directory in the form of a list)
3) command 2 is equivalent to LL/


Absolute path and relative path
1 absolute path, to/Beginning is absolute path, such as/home/pzm
2 relative path to./or. /beginning,./refers to the current directory, ... /refers to the previous level of the directory


mkdir Create a directory
1 mkdir-m 777/home/pzm/(set up PZM directory in the home directory, the directory has permission of 777)
2) mkdir-p/home/pzm/1/(if/home/pzm/directory does not exist, execute this command to create a directory, create the result will create both PZM and 1 directories)
3 mkdir/home/pzm/is the/home directory under the new PZM directory


RmDir Delete Directory
1 rmdir/home/pzm/(Delete the PZM directory under the/home directory, request PZM directory can no longer use other directories or files, that is, must be empty)
2) rmdir-p/home/pzm/1/(delete the/home/pzm/directory under the name of the 1 directory, if the PZM directory in addition to the 1 directory, no other directories or files, then add-p parameter after the implementation of the PZM/1 both directories are deleted)


Touch command
1 Touch AAA, create a file named AAA, if the AAA file exists, then update the file time
2 Stat AAA can view information about the file


CP Copy files or directories
1 cp/home/test.txt/home/pzm/(is to copy/home/test.txt this file to the/home/pzm/directory)
2 Cp-r/home/pzm//home/1/(that is, copy/home/pzm/directory and all directories and files in this directory to the/home/1/directory)
3) cp-i/home/pzm//home/1/(if the command 2 has already been executed, that is, the/home/1/directory has the same directory or file under the/home/pzm/directory, then add-i parameter to perform the copy operation will prompt the user whether to overwrite operation, Enter Y-return to perform an overlay operation, enter n return without performing the overwrite operation)
4) Cp-f/home/pzm//home/1/(without prompting the user to overwrite directly)


MV Move or rename an existing file or directory
1) mv/home/test.txt/home/pzm/(cut the Test.txt file in the/home/directory to the/home/pzm/directory)
2 mv-i/home/test.txt/home/pzm/(if Test.txt is already in the/HOME/PZM directory, this will alert the user to overwrite)
3) mv-f/home/test.txt/home/pzm/(do not remind users to cover directly)


RM deletes files or directories, and when using RM this command, be sure to specify directories or files to avoid accidental deletion
1) rm/home/test.txt (delete the Test.txt file in the/home/directory)
2) rm-i/home/test.txt (prompts the user before deleting the file)
3) rm-r/home/pzm/(delete all files and directories under the/home/pzm/directory)
4 rm-f/home/test.txt (without prompting the user to forcibly delete files or directories)
5 rm-rf/home/pzm/(forcibly delete/home/pzm/this directory and all directories and files in this directory, do not prompt the user)
6) RM-RF * (recommended not to use this command)
7 when using RM, MV Command, be sure to note that when you need to delete or cut the directory, not into the delete or cut the directory to do the operation


More, less command
1) more filename, or less file name
2 more can only turn the page to display, and less can use the top and bottom key line by row to look at the contents of the file
3 when using more or less to view the contents of the file, press Q to exit


Head looks at the first few lines of a file, and the default is to view the first 10 lines
1 Head test.txt (view the first 10 lines of this file)
2) Head-n 5 test.txt (view the first five elements of this file, set the number of specific views through the-n parameter)


Tail view the last few lines of a file, the default is 10 lines
1) Tail test.txt (view the last ten lines of this file)
2 tail-n 5 test.txt (view the last five elements of this file, set the number of rows to view with the-n parameter)
3) Tail-f Test.txt (generally used to view log files, if the contents of the file is updated, it will be displayed synchronously)


Alias to view the name of the command, Unalias revoke the alias, and the alias is modified in the ~/.BASHRC file


Hostname
1) hostname View host name
2 hostname PZM, modify the host name to PZM, permanent changes need to change/etc/sysconfig/network this configuration file


Id
1) ID, display user information
2) id-u Display user ID number
3) ID-G Display Group ID number


Date
1 Date shows detailed information on dates
2 Date +%d%m%y display dates, months, years


13, Find Search command
1) Find/-name Test.txt (in the root directory/start by the name of the way to look up test.txt this file, format is found + path + OPTION + filename)
2 Find/-type D-name 1 (search for directory with name 1 in root/below)
3) Slocate, locate can also find files, but it is from the Slocate.db database to find, you need to use the UpdateDB command before using


14, AddUser Add Users
1) AddUser PZM (add user with name PZM)
2 passwd PZM (modify PZM account password, otherwise can not log in)
3 SU-PZM (completely switch to PZM user,-is completely switched)
4/etc/login.defs (create user's default attribute in this file)
5 usermod-g root.bin PZM (add PZM user to root, bin Group)


After adding users can view the/etc/passwd file to see if the user added success


15, Userdel Delete user
1) Userdel PZM (Delete the name PZM user)
2) userdel-r PZM (delete pzm user and delete pzm user's home directory and related information)


passwd Modify account password
1) passwd (Modify the current user's password)
2 passwd PZM (root account has the ability to modify any account password, if currently run as root account, execute this command can modify the PZM account password)


SU Switch User
1 su pzm (switch to PZM account run)
2) exit (exit Su Switch to the account)


The way to log on to Linux using a non-root account is the same as using the root account, except when you log on to the system and choose another user




16, PS static way to view the process
1) ps-ef (Show All Processes)


17. Top Dynamic View Process
1 Top (Dynamic view of all current processes, including CPU and memory information)


18, chmod change the permissions of the file
1) chmod 777 Test.txt (Modify Test.txt file permission is 777, the format is chmod + specific need to set permissions + file or directory name)
2) chmod +x Test (add executable permissions to test this file)
3) U represents the owner of the document, G means the owning group of the file, o means that other users of the file, such as or to add writable permissions to other users of the file, can use the command: chmod o+w test, you can use the command if you want to remove the executable permissions from the current user group for the file: chmod g-x Test


19, tar compression decompression command
1) TAR-CVF Test.tar./(Package All files and directories in the current directory into Test.tar, in the form of tar + (options, such as-CVF) + packaged file name + directory or file to be packaged)
2 TAR-XVF Test.tar (extract Test.tar package to the current directory, in the format of tar + (options, such as-XVF) + packaged file name)
3) tar-xvf test.tar-c./1/(extract Test.tar package to the specified directory, in the format of tar + (options, such as-XVF) + packaged file name + (-c) + specified directory)
4) TAR-ZXVF apach-tomcat-6.0.16.tar.gz (using the-Z option to extract the GZ package)
5) TAR-ZCVF test.tar.gz./(Package all contents of current directory and compress to test.tar.gz package)
6) TAR-JCVF test.tar.bz2./(Package all contents of current directory and compress to TEST.TAR.BZ2 package)
7) TAR-JXVF test.tar.bz2 (extract bz2 package requires-j option)
8) TAR-TVF Test.tar (View package contents)


20, output redirection
1) > Example: ls > ls.txt (the result of the LS is all written to ls.txt this file, if this file does not exist, will be automatically created, the effect is, each time the specified file content empty, and then write new content)
2) >> Example: ll >> ll.txt (LL-Check out all the results written to the Ll.txt this file, the difference with > is that it is not overwrite write, but append write, at the end of the contents of the specified file appended to write)


21. Input redirect
1 Cat < Ll.txt (will ll.txt the contents of this file as the input of cat)
2 input redirects are generally followed by files


22, Pipeline: | Use the output of the front command in the pipe as input to the command behind the pipeline
1) ps-ef | grep Bash (Ps-ef The result of this command as input to the grep command)


23, VI Editor
1 vi config.txt (use VI Editor to view the modified Config.txt file, if this file does not exist, you will create a file named by that name)
2 after entering the VI editor, press A or I into edit mode, edit and press Esc key to launch edit mode.
3 If the Config.txt file has not been modified, direct input: Q can exit the VI editor
4 If the Config.txt file is modified, enter: Wq Save the changes and exit, you can also enter: q! do not save changes and exit
5 in the command mode, even press G back to the first line of the file, in uppercase G (SHIFT+G) back to the end of the file
6 in the command mode, input/+ specified characters, such as/Q, is in the current file to find Q this character, in lowercase n is down to find the next matching character or string, in uppercase N is to look up the next matching character or string, input? Q, it's looking up from the bottom to the content containing the Q keyword
7 in command mode, enter: Set Nu the line number in the current text
8 in command mode, enter DD to delete the line where the cursor is located
9 Direct Input VI command, followed by the file name, when saving exit: Wq after the filename, you can save the file as a specified file name
10) Set highlighting, command mode input: Set Hlsearch
11) Cancel highlighting, command mode input: Set Nohlsearch
VI cannot enter Chinese in case of the VI command line mode input: Set encoding=gb2312
13 in command mode, press U to undo last modification, press Ctrl+r to continue last undo modification
14 in command mode, X deletes a character
YY copy line, p paste
: W AAA saves current file contents to AAA this file
): e! Remove all modifications and restore the file to its very first state
% s/abc/123/g, replace all the ABC strings in the text with 123


24. WC Text Statistic command
1) Cat Test.txt | Wc-l (the contents of the cat test.txt are piped as input to the WC command,-L is the statistic line number
2) Cat Test.txt | Wc-c (statistics number of characters)
3) Cat Test.txt | Wc-w (count words)
4 WC test.txt (respectively test.txt the number of lines, words, characters of this file, and display the current filename at the end of the output)
5 wc-l Test.txt (can be followed by the WC and the corresponding option-l-w-C to display the number of lines in the file, Word number, number of characters)


25. grep Search Command
1) Cat Test.txt | grep test (the result of the cat test.txt is piped as the input of the grep command and searches for the test string, if the search is displayed to the screen, case sensitive)
2) Cat Test.txt | Grep-c Test (with the-C option to count the number of rows containing the test string, that is, how many rows contain the test string)
3) Cat Test.txt | Grep-v Test (with the-v option to display all rows that do not contain a test string to the screen)
4) Cat Test.txt | Grep-i Test (without distinction of case) when searching with the-i option
5 grep test Test.txt (no pipe lookup, find all rows containing the test string in the Test.txt file and display)
6 grep-l Test Test.txt (with the-l option, if the test string is found in the Test.txt file, only the current file name is displayed, otherwise nothing is displayed)
7 grep Test * Find the line containing the test keyword in all files in the current directory and print the contents
8) Grep-r test/etc/in etc directory to find all the files containing test keywords
9 GREP-A2-B3 Test test.txt find the test keyword in test.txt and print out the first 3 lines and the last two lines of the line


26. awk Text Processing Tools
1) awk '/test/' test.txt look up test.txt file containing the test keyword row sickness print, case-sensitive
2) awk ' {print} ' test.txt default to match each row in the test.txt with a space delimiter, and the contents of each row are separated by spaces, the string before the first space, the string before the second space, and so on. Finally, print out the matching contents of each line.
3) awk-f ":" ' {print} ' Test.txt to: as separator
4 awk ' $1==test {print $} ' test.txt space as the interval, if the first paragraph is test, print the second paragraph


Ifconfig
1) ifconfig, view network card information
2) ifconfig eth0 192.168.1.1 Modify the IP address of the ETH0 network card
3 permanently modify the IP address need to change this configuration file/etc/sysconfig/network-scripts/ifcfg-eth0
4) Netconfig+service Network Restart can also permanently modify network information


Mount
1) Mount displays all mount points
2) mount/dev/sda1/mnt the/DEV/SDA1 partition to the/MNT directory
3) umount/mnt Uninstall/MNT mount point


Cut-and
1) cut-d:-f7/etc/passwd to: As a separator, take the seventh paragraph and display


Sort
1) sort-t: +2-n/etc/passwd to: As a separator, the third part sorted output by number
2) du | Sort-n The first column of the DU output to sort the output by number
3) Sort-r Reverse Ordering


Top
1 Top View system resources and process information
2 in the top command, press K, and then enter the process PID, select Input 9 or 15th signal, you can end the process
3 Click in the top command to view Help
4 in the top command press m, sorted by memory


ENV View all environment variable information
Set to view all variable information


Sed Flow Editor
1) sed-e ' s/root/pzm/g '/etc/passwd replaces the root string in the contents of the file in/etc/passwd with PZM and prints to the screen, but the contents of the file are not modified
2) sed-n-E '/pzm/p '/etc/passwd prints the line containing the PZM string in the/etc/passwd
3) sed-e '/^#/d '/etc/squid/squid.conf removes the contents of this file from the beginning of the # without printing
4 info sed view sed help


27, ' reverse the role of quotation marks
1) grep ' Id-un '/etc/passwd the function of this line of command is to execute the command id-un in the reverse quotation mark, and the result of the Id-un execution as the input of grep, the line command is equivalent to the following two lines of command:
Id-un (assuming the output of this line of command is root)
grep root/etc/passwd


28, Free-m
Viewing memory information


29, gcc-o executable program file name (can be arbitrarily taken, do not and directories can be duplicate) *.c (c code to compile)
GCC is used to compile C program, c program is of course with. C as the suffix
-O generates an executable file that compiles only code without this parameter and does not generate an executable file


30. Execute Shell script:
1, write the script, before the script needs to add #!/bin/bash, the surface of the current shell version
2, give the script executable permissions
3, SH *.sh or./*.sh Execute the Script


31, modify the terminal prompt:
Export ps1= "[t \w \u \h]"
\ t Show time
\w Displays the current directory
\u Display the current user name
\h Displays the current host name


32, integer operator expr
1 Expr 1 + 1 (the result is 2, note that the + number must have spaces on both sides)
2 expr $count + 1 (make the value of the Count variable +1)
3 Expr $count \* 10 (multiplication need to be escaped by backslash)
4 Expr $count/10 (divide the value of the count variable by 10)
5 count= ' expr $count + 1 ' (after assigning the value of the count variable to the count variable, the expression needs to be enclosed in reverse quotes)
6 = no space on both sides of the number, operator left and right side to add space
7 Assign a value to a variable without having to add $


33. Test operator
1) test-e Test.txt (to determine if the current directory exists test.txt this file, the execution of the statement is saved in the $? variable, if $? The result value of 0 means that the file exists, if the $? result is a value other than 0 indicates that the file does not exist)
2 test-d log (to determine whether the current directory exists log this directory)
3) (defines two strings to determine whether two strings are equal)
Str1=abc
Str2=cdf
Test $str 1 = $str 2
Test $str 1!= $str 2
4 integer judgment, judgment equivalence use-eq judgment unequal use-ne judgment is greater than using-GT judgment is greater than or equal to use-ge judgment is less than equal to use-lt judgment is less than equals-le
num1=10
Num2=20
Test $num 1-eq $num 2


34, if program


#!/bin/bash
#$ #是指在执行此脚本时传入的参数的个数
#先判断执行脚本时传入的参数是否不等于2
#如果条件成立则打印错误提示用户, otherwise enter the Elif judgment statement
if test $#-ne 2
Then echo Error:two args required
#判断用户在执行脚本输入的第二个参数是否不是文件,-f is the option to determine whether or not to file! is to take the opposite meaning
elif [!-F $]
Then echo "Sorry: $ is not a file!"
#用户输入的两个参数分别保存在 $ In this example, the grep statement is executed to find the corresponding string in the $ corresponding file and redirect the input results to the/dev/null device so that the results are not printed on the screen. and the execution result of the whole statement as elif judgment condition, if execution returns 0 successfully, print find, otherwise return non 0, print none
Elif Grep-s $ >/dev/null
Then echo find
else Echo None
Fi
If you need to add a space, [] before and after all need to add space
There is no need to add then in else
Be sure to write fi as an end


35. Case procedure
#!/bin/bash
#调用date命令, followed by +%h in order to output the current hour for how much, by inverted quotes to save the resulting results in the hour variable
Hour= ' Date +%h '
#将hour和后面的条件做判断, if equal executes the following statement
Case $hour in
#通过正则表达式判断当前的hour是否为01 ~11, if yes print Good morning
0[1-9]|1[01]) echo "Good morning!";;
#通过正则表达式判断当前的hour是否为12 ~17, if yes print good afternoon
1[2-7]) echo "Good afternoon!";;
#*) is the default branch, that is, if the above conditions are not satisfied then go to the default branch, print good evening
* echo "good evening!";;
Esac
Each branch can have more than one statement, but requires a separate line, and each branch must be;; As an end


36, for The Loop program
1 var as a cyclic variable, from 1 2 3, respectively, in do doing to fill out the specific loop code, for this program, VAR will be assigned to 1 2 3, the cycle will be executed three times, respectively, print var three cycles of value
for Var in 1 2 3
Todo
Echo $var
Done


2 Note that this method requires two parentheses, similar to the first method above, this loop is 5 times, I values 0 1 2 3 4
For ((i=0;i<5;i++))
Todo
Echo $i
Done


3) $* refers to the list of parameters that we pass in when we run the script, and then executes the for loop, and the Var variable is taken from the $* argument list, respectively.
for Var in $*
Todo
Echo $var
Done


37, while loop procedure
#!/bin/bash
#定义个变量, named Num, with an assignment of 0
Num=0
#while循环的判断条件是num的值小于10, otherwise exit the while loop
While [$num-lt 10]
Todo
#打印num当前的值,-N is a line that does not wrap when the echo is executed
Echo-n "$num"
#通过反向引号包含一句整数运算表达式, the purpose is to add a num value of 1
num= ' expr $num + 1 '
Done
#打印信息
Echo
echo "Loop exit."


38. Read command
1 after the execution of the script, enter a line of string to confirm the input string will be saved in the variable str, the final printout of the results and user input results consistent
#!/bin/bash

Read Str


Echo $str


2 after executing the script, read three strings from standard output to str1 str2 STR3, enter a space interval, return to end, and finally print three variables
#!/bin/bash

Read STR1 str2 STR3


echo $str 1
Echo $str 2
Echo $str 3


39, the Production of menu programs sample
#!/bin/bash
#清屏
Clear
#通过true在设置while循环的条件一直为真, unless you choose to exit according to different conditions in the loop body, you can pass the break (exit the loop, continue to execute the statement after the loop), or exit (exit the current script and no longer execute the statement following the script)
While True
Todo
echo "1, Display date"
echo "2, Display the number of online users"
echo "3, Exit"
The #-e parameter is to ensure that the escape \,\c is used in the string that is output later to mean that the current output does not wrap.
Echo-e "Please enter a choice (1, 2 or 3): \c"
#read命令是指从标准输入截取内容赋值给变量no
Read No
Case ${no} in
#调用date打印日期, format installation date of the format output
1) Date "+%y/%m/%d";
#通过who命令查看当前在线的用户, the results of the WHO output are queried by wc-l, which means that several users are currently online
(2) who |wc-l;;
#退出脚本
3) exit;;
Esac


#程序执行到这里之后, according to the number of seconds after sleep
Sleep 3
Clear
Done


40, batch set up users
#!/bin/bash
Echo-e "Input a username:\c"
Read name
Echo-e "Input start number:\c"
Read Startno
Count= $startno
Echo-e "Input End Number:\c"
Read Endno


#如果startno变量的值小于等于endno变量的值, then execute the statement in the loop body
While [$startno-le $endno]
Todo
#在做字符串拼接的时候一定要使用 ${} This way to refer to variables
#我们强调以后所有的变量尽量都采用这种方式 to avoid errors
Usernames= "${name}${startno}"
echo "Add User" $usernames
#可以直接调用useradd命令, generally do not need to add a specific absolute path in front
#useradd命令的-P parameter refers to adding a password to the user created
/usr/sbin/useradd-p zymnbk31xvv7y "$usernames"
startno= ' expr $startno + 1 '
Done


count= ' Expr $endno-$count + 1 '
echo "$count done!"


41. RPM Software Installation command
1 RPM-IVH abc.rpm (Installation of the RPM software package,-IVH for the installation of options, two ways, one is to use an absolute path, then you can install the RPM in any place, one is to use the relative path, into the RPM package in the directory, directly using the RPM filename on OK)
2 rpm-e ABC (uninstall RPM package,-e is uninstall the option to use)


42, for, while, if conditions can be used similar to the C language of the wording
while ((i==0))


if ((i==0))


For ((i=0;i<10;i+=))

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.