Summary of basic Linux commands

Source: Internet
Author: User
Tags svn svn update what file system blank page

Switch directories
CD ~
Home Catalogue
CD-
Go back to the directory you just
Cd..
Back to Parent Directory
Cd
Home Catalogue


——————
Show current directory
Pwd
Show actual path, not connection path
Pwd-p


——————
Create a new directory
mkdir Directory Name
granting permissions
MKDIR-M 777 Test
Recursive setup
Mkdir-p Test/test1/test2


——————
Delete Empty Directory
RmDir Directory Name
Recursively Delete empty directories
Rmdir-p Test/test1/test2


——————
viewing directories and files
Ls
-t time sorting,-s size sort,-r reverse arrangement,-a show hidden file
--full-time Show Full Date


——————
Copy
CP Source File Destination file
-P Copy property, the-D link file is copied as the property of the linked file,-R recursively replicates,-a equals to-PDR
-S generates a shortcut,-l generates a hard link,-u if the source file replaces the target file with a new target file,
-I ask before overwrite if target file already exists,-f force overwrite
Note: Copy the shortcut without any parameters, copy the source file, copy only the shortcut with-A or-D.


——————
Delete
RM file or directory
-F Force Delete,-I asks whether to delete,-R recursive deletion


——————
Move or rename
MV Source File Destination file
-F Force move,-I asks whether to move,-u if the target file already exists and the source file is relatively new and replaced


——————
View file contents directly
Cat file name
-V lists special characters,-t displays Tab key ^t, displays newline characters $
-N Display Line number


——————
Reverse Display
TAC file name
Contrary to cat, the inverted display


——————
Add line numbers when viewing content
NL file Name
-B A blank line also to line number,-B t blank line no line number


——————
Flip Page view
More file names
Blank page, enter the next line, Q exit,/String query


——————
Flip Page view
Less file name
Front page: Pageup,pagedown, other ibid.


——————
View first few lines
Head-n number of rows file name


——————
View the following lines
Tail-n number of rows file name


——————
viewing bytecode files
Od-t C file name
-t A default output,-t C with ASCII output,-t D decimal, F floating point, o octal, x hex


——————
Create an empty file, modify the file time
Touch file name
Touch-t yymmddhhmm file name


——————
Default permissions for Files
Umask-s
Change default permissions umask 002 (deleted permission value)


——————
Set hidden properties for a file
chattr [+-=][asacdistu] File name
+ Add attribute,-delete attribute, = Set Property
+i setting Read-only


——————
Show hidden properties of a file
lsattr [-ar] File name
-a displays hidden file properties,-R recursive subdirectory


——————
Resolving file types
File File name


——————
Find execution files
which [command]
-A Find out all commands


——————
Finding the specified file
Whereis file name
-B Looking for executable files


Locate partial file name


Find [PATH] [OPTION] File name
Example: Find/etc-name * (wildcard) name


——————
To view the capacity of a directory
DF Directory
-a show hidden (with in-memory files),-K shown in KB,-m in MB,-H plus unit,-I display inode capacity


——————
List the total capacity of all files in the current directory
Du
-a lists the capacity of all files,-K units kb,-m units mb,-h Plus units,-s statistics the total size of the current directory
du-smh/home/weblogic/* each file size in the current directory


——————
Establish a connection
The ln source file destination file establishes a hard link
-S symbolic connection, F-target is deleted and added if present


Symbolic Connection: Modify Connection File = Modify source file
The default number of connections for a directory is 2 (including: and. Two connections)


——————
Fdisk-l Device Name
-L Output all partition contents of the device
Only root can perform fdisk,fdisk only partition table does not actually delete data
d Delete partition, p view partition details, n new partition (p primary partition 1-4,e extended partition, L logical partition), capacity fill +100m (not filled is the default value)


——————
Disk formatting
MKE2FS named device name
-B Set block size 1024, 2048, 4096,-i set inode size,-c test read,-C-C test reading and writing,-l set volume label,-j format into EXT3
Example: Mke2fs-j-L "logical"-B 4096/DEV/HDB5


Mkbootdisk--device/dev/fd0 ' uname-r ' set up a bootable floppy
fdformat/dev/fd0h1440 low-level formatted boot floppy


——————
Disk self-Test
Only the Lost+found directory will be available under the loading point directory, and the error files found in the self-test will be sent here.
FSCK Device Name
-C Display progress bar,-T specify file system-a scan all devices,-a/-y automatic repair,-r repair when asked
Example: Fsck-c-T EXT3/DEV/HDB5
Only the root user can use


Check for bad tracks (fsck can replace this command)
Badblocks Device Name
-S show progress,-V Show Progress


——————
Sync synchronizes the in-memory data to the hard drive


——————
Loading the established partitions into the file system
Mount device name/code load point
For example Mount-t EXT3/DEV/HDB5/MNT/HDB5
-T prompts for what file system to load (can not write to let it identify), loading point can also not write.
-l load with device code/volume label
For example Mount-t ext3-l LOGICAL/MNT/HDB5
Remount Re-loading
Write-only mount will list all mount points and file systems
Only the root user can use


——————
Uninstalling partitions from the file system
Unmount Device name/code/Load Point


——————
To modify a volume label
E2label Device Name Volume label


——————
Multifunction Modify Disk Properties
TUNE2FS Device number
-j device file system changed from ext2 to Ext3
-L Change the volume label
-L read out super block data


——————
Adjust disk properties to turn on DMA
Hdparm Device Name
-I display test information for the disk,-D turn on DMA mode D1 on, D0 cancel
Only root can be used


——————
Setting up virtual memory devices
The first method:
FDISK/DEV/HD[A-D] First create a partition and change the partition ID to 82
MKSWAP/DEV/HD[A-D][1-16] Format the created partition into a swap file format
SWAPON/DEV/HD[A-D][1-16] Start swap
Complete
If virtual memory is not required, it can be closed with Swapoff


The second method:
DD If=/dev/zero of=/tmp/swap bs=4k count=16382 DD command is used to convert the format while copying the file, if is the input format of which is the output format, BS is how large a partition is, and count is how many partitions (64m)
Then a/tmp/swap file was created
Mkswap/tmp/swap to format the file as a virtual memory file format
Swapon/tmp/swap start virtual memory, do not want to manually start to write this sentence into the/etc/rc.d/rc.local
If you want to close, use Swapoff/tmp/swap.


——————
Compress compression decompression, compressed single file
Compress file name--compression
compress-d file name. z--decompression, uncompress is OK
Compress has been replaced by grip.


——————
Gzip Compress unzip, compress single file
-C Data Flow redirection,-D decompression,-t check consistency,-#压缩等级1-9, default is 6
Gzip file name--compression
Zcat *.gz--Read the contents of the compressed file (extracted content)


——————
BZIP2 compression decompression, compressed single file
-C Data Flow redirection,-D decompression,-Z compression,-#压缩等级1-9, default is 6
Bzip-z file name--compression
bzip-d file name--unzip the available BUNZIP2 instead
Bzcat *.bz2--Read the contents of the compressed file (extracted content)


——————
Tar package unpacking, file or directory
Files or directories that need to be packaged after the tar is packaged
-C Set up a package file (create),-X unpack,-T view internal files, cxt three conflicting only one
-Z Compress or decompress with gzip,-j compress or decompress with bzip2
-V shows the log of the compression decompression process
-f Specifies which file to manipulate
-N Filter by Time (YYYY/MM/DD), newer than this time will be packaged
--exclude file is excluded from the process of compression
TAR-CVF/TMP/ETC.TAR/ETC only package does not compress
Tar-zcvf/tmp/etc.tar/etc after packing and then using gzip compression
View GZ Files Tar-ztvf/tmp/etc.tar
Unzip only one of the files Tar-zxvf/tmp/etc.tar Etc/password
New than the date to pack tar-n ' 2015/11/11 '-ZCVF home.tar.gz/home
Exclude a file when packaging two directories tar--EXCLUDE/HOME/DMTSSI-ZCVF myfile.tar.gz/home/*/etc


——————
Back up, write back
DD if= "Input_file" of= "output_file" bs= "block_size" count= "number"
If: Input file/device, of: Output file/device, BS: Plan the size of a block, count: how many blocks
If and of directories cannot be the same, otherwise fail


——————
Checkout files to a local directory
SVN checkout [path]
such as SVN checkout Svn://192.168.1.1/pro/domain


——————
Add a new file to a repository
SVN add [file]
For example, SVN add test.php (add test.php, you can add all *.php with a wildcard character)


——————
Commit the changed file to the repository
SVN commit-m "LogMessage" [-n] [--no-unlock] PATH (use –no-unlock switch if hold lock is selected)
For example, SVN commit-m "Add test file for my test" test.php


——————
Update SVN to a version
SVN update-r m [path]
SVN update If there is no directory behind it, the default is to update all files in the current directory and subdirectories to the latest version.
SVN update-r test.php (Restore the file test.php in the repository to version 200)


——————
View svn files or directory status
SVN status [path] (State of files and subdirectories under directory, normal status not shown)
?: not in SVN control; M: Content modified; C: conflict; A: Scheduled to be added to Repository; K: Locked
-V Path (shows file and subdirectory status)


——————
Delete svn files
SVN delete [path]-M "Delete test Fle"
Example: SVN delete svn://192.168.1.1/pro/domain/test.php-m "Delete test file"


——————
View SVN logs
SVN log [path]


——————
View SVN file information
SVN info [path]


——————
SVN comparison Differences
SVN diff [Path]


——————
Merging differences between two versions into the current file
SVN merge-r m:n Path
For example: SVN merge-r 200:205 test.php (the difference between version 200 and 205 is merged into the current file, but generally conflicts occur and need to be addressed)


——————
VI Editor
Create a new File VI Test.text
Press A, O, I into edit mode
Press ESC to return to normal mode
In general mode, press: Wq store and leave VI
You can wq! force write when permissions are not allowed to write


Commands in general mode:
+ or-cursor moves to next line/previous line
0 or $ the cursor moves to the first/last face of this line
G or GG cursor moves to the last line/first line of the file
n+ carriage return cursor moves down n rows
/word Search for keywords? Word up search keywords
: N1, n2s/word1/word2/g from N1 and N2 line word1 to Word2
For example: 1, $s/word1/word2/gc replace word1 with Word2 from the first line to the last line, confirm ask when replacing
X or x Delete one character backward/forward
DD Delete the entire line where the cursor is located
NDD Delete the n rows after the cursor
yy copies the entire line of the cursor
Nyy n rows after the cursor is copied
P or p paste to the next line/previous line of the cursor
U undo
Ctrl + R redo or.


Commands in command-line mode:
: w Save,! Force Save
: w [filename] Save As
: R [FileName] paste the contents of another file into the current file
: Q leave,! Force left
: e! restore the file to its original state
ZZ If changed then save left, if not changed, go straight away
: Set NU Displays line number
: Set Nonu hidden line number


When you edit a file in VI, a. SWP temporary hidden file is generated for recovery purposes.


——————
Vim Editor
Block Select Ctrl + V then move the cursor up or down to select the target area
Y copy target area, d delete Target area, p paste


Multi-file Edit vi file 1 file 2
: N Edit Next file
: N Edit Previous file
: File lists all files that are currently open


Multi-window Editing VI file 1
: SP [filename] does not fill in the same file as the second window in the same screen
Ctrl+w +j cursor down a window
Ctrl+w +k cursor toward the last window


——————
DOS and Linux file mutual transfer
Dos2unix filename
Unix2dos Dilename
-K reserved Mtime (last modified time) does not change
-N old new preserves legacy files and outputs the converted new files


——————
To set an alias for a command
For example alias lm = ' Ls-al '


——————
Tell if the command is input kernel
Type [command]


——————
Variable
View variable values
Echo $varname
Set variable varname=varvalue cannot have spaces (spaces enter with \ Escape), variable names cannot begin with a number, reference variables in double quotes are translated into variable values, and single quotes are handled only with ordinary strings
Extension variable varname= "$varname": Varvalue
To upgrade a variable to an environment variable export varname
Bash opens a new shell,exit out of the shell, and the parent program's custom variables are no longer available within the subroutine unless promoted to an environment variable
Delete variable unset varname


Commands that are ' enclosed ' are executed first, such as Ls-l ' locate Crontab '
If the/etc/profile system parameter file is not changed, the variable settings for this login will be canceled at the next logon


——————
View environment variables
Env
Where the random is the generation of the stochastic number 0~32767
Declare-i number= $RANDOM *10/32767 generates a random number of 0~9 to the variable numbers


——————
View variables (also including environment variables) in the current shell (current user)
Set


——————
To view the language types supported by the system
Locale-a
Set Language Lang=zh_tw.big5 (support Chinese)


——————
Reading a value from the keyboard to a variable
Read-p "Please input your name:"-T-Name
-P prompt message,-t wait time


——————
Declaring variables (and declaring types)
Declare varname default type is string
-a defines an array,-I is defined as an integer number,-X is defined as an environment variable, and-r is defined as read-only (cannot change cannot delete unset)
Array variable Varname[index]=varvalue
Reference array echo ${varname[index]}


——————
Alias settings for commands
Alias lm= ' Ls-l | More ' format is the same as defining variables
The same name will replace the original command.
Cancel alias settings
Unalias LM


——————
Lists the history commands you have entered
History n
N is a list of the last few history records


!n execution of the nth historical command
!! Executes the previous command
!al perform the most recent command that starts with Al


——————
Executing executable files in the current directory cannot be called directly by name
The way to require relative paths, such as./squid


——————
Modify the system login prompt
Vi/etc/motd
The added sentence will appear after the login host


——————
Apply settings file now
source file
such as: Source ~/.BASHRC or. ~/.bashrc


——————
Settings for shortcut keys
Ssty-a Show all shortcut keys (^ for ctrl)
Stty Erase ^h Change the deletion character to Ctrl+h


——————
Combining Keys and wildcards
* Wildcard with 0 or more characters,? There must be a character, #注释, \ Escape character,/path split, | pipe character, continuous command separator
$,& The command into background work before calling the variable, () between the beginning and end of the shell, [] in the middle of the character combination, {} in the middle of the command block
Combination keys:
CTRL + C terminate command, CTRL+D input end, CTRL + Z pause command, ctrl+s pause screen output, ctrl+q restore screen output


e.g. CP Test[1-5]/tmp copy TEST1,TEST2....TEST5 to/tmp
[!a-z]* don't want to have a file with lowercase letters


——————
Data Flow re-orientation
Enter data that should be output to the screen elsewhere (file or device)
Standard input code 0 using < or <<
Standard output code 1 using > or >>
Standard error Output code 2 using 2> or 2>>


Command-guide device or file


For example, record the list of files in the root directory
Ls-l/> ~/rootfile (> The way to create a file is to overwrite it if it exists, and the new,>> does not overwrite it, but writes it down below)
Log the correct information about the abandonment error message
Find/home-name testing > List_right 2>/dev/null (black hole Trash, data entered here will disappear)
Log to the same file
Find/home-name Testing >list 2>&1 (special wording)
< represents changing data from the keyboard to read from a file
such as: Cat > Catfile < Somefile (Cat > Catfile will require keyboard input text into Catfile, now read from Somefile)


——————
If the file does not exist, create a new
Ls/tmp && Touch/tmp/testingagain The former does the latter without problem
Ls/tmp/testingagain | | Touch/tmp/testingagain The former has a problem before performing the latter
Can be used, for example
Ls/tmp/vbriding && echo "Exist" | | echo "not exist" if correct displays exist otherwise displays not exist
Note that the problem with the callback value,&& to be in the | | Front


——————
Pipe symbol | Used to handle the standard input passed by the previous command.
Choose a command:
Cut breaks down the data of your peers and selects the part you want
-d ' delimiter '-f n1,n2 broken down by delimiter, select section N1 and N2


grep parses a row and selects it if it contains the required information
grep ' search string ' filename
-N line number,-V reverse selection
such as: Last | grep ' Root ' | Cut-d '-F 1 take the first column of the record with root


Sort command:
Sort by default by text
-F ignores case,-B ignores the front space,-n is sorted by pure number,-R is reversed,-u same data only shows one row, and-t delimiter-K sorted by which column after separation
Example: CAT/ETC/PASSWD | Sort-t ': '-K 3-n by: Delimited by third column number


Uniq to display duplicate data only once
-I ignores case,-c counts


WC display number of rows, words, characters
-l display only the number of rows,-W displays only the number of words (English word),-m displays only the characters


awk text Analysis
command line Form awk [-f field-separator] ' commands ' input-file (s)
-F with what split, optional, default space
awk ' {pattern + action} ' {filenames}
For example
#last-N 5 | awk ' {print '} '
Reads a record with a ' \ n ' newline, then divides the record by the specified domain delimiter, fills the field, and $ $ represents all fields, representing the first field, $n representing the nth field. The default Domain delimiter is the "blank key" or "
Awk-f "" ' {print NF} ' gets the length of each line
awk ' nr>=10 && nr<=15 ' get data from the first line to the first row


bidirectional re-orientation:
Tee file retains data stream output to the screen while the data stream is entered into the file or device
-a adds file in an additive way (not write is overwrite)
As last | Tee Last.list | Cut-d '-f1


Character Conversion Commands:
Delete or replace text in a message
TR [-ds] SET1
-D Delete the SET1 string in the message,-s replaces the duplicate string
such as: Last | TR ' [A-z] ' [A-z] '


Turn the TAB key into a reciprocal space
Col [x]


——————————————
Shell Script
#! /bin/bash tells the script to parse it with bash


filename=${fileuser:-"FileName"} to determine whether the Fileuser variable has a value, if it is not used-the following value


Date= ' Date +%y%m%d ' Gets the day date string
Date= ' date-date= ' 1 days ago "+%y%m%d" gets yesterday's date string


Numerical operation Method:
total=$ (($a * $b)) # $ (Operation content)


Test command:
TEST-E/dmtsai && echo "Exist" | | echo "not exist" test directory exists
The other option-F is whether the file-D is a directory-B or whether the block device-L is a linked file
-R is readable-W whether writable-X can be executed-s is not blank
Test File1-nt File2 # New than older than file
Test File1-ot File2 # old than
Test File1-ef File2 # Whether the same file is using the same inode to judge hard links
Test N1-eq n2 #两数值相等 Other options:-ne unequal-gt greater than-lt less than-ge greater than or equal to-le less than or equal to
Test-z string #是否空字符串 Other options-n is not an empty string
Test str1 = str2 #字符串是否相等 other options! = is not equal to
Test-r filename-a-x filename #多重判断-A and,-O or! Non-




——————————
Judging symbols []
["$HOME" = = "$MAIL"] note that spaces and double quotes are mandatory
["$choice" = = "Y"-o "$choice" = = "Y"] && echo "OK" && exit 0
["$choice" = = "n"-o "$choice" = = "n"] && echo "No" && exit 0


——————————
Default variables for shell scripts
$ A is the name of the execution file the first parameter is
Special examples are:
The script name is called test.sh Three: 1 2 3
After running test.sh 1 2 3
$* is "1 2 3" (enclosed in quotation marks)
[Email protected] is "1" "2" "3" (separately wrapped)
$ #为3 (number of parameters)
——————————
Conditional judgment
If [conditional judgment expression]; Then
#命令
Fi


If ["$a" = = "Y"] | | ["$a" = = "Y"]; Then
Echo OK
Exit 0
Fi


If [conditional judgment 1]; Then
#命令1
Else
#命令2
Fi


If [conditional judgment 1]; Then
#命令1
elif [conditional judgment 2]; Then
#命令2
Else
#命令3
Fi


——————————
Netstat-tuln getting the current host-initiated service
Port-to-function correspondence:
Www;22:ssh;21:ftp;25:mail


——————————
Case in ESAC equivalent to switch
Case $ in
"Hello") #符合第一个条件, output hello,;; Indicates end of code block
echo "Hello"
;;
"") #符合第二个条件
echo "haha"
;;
*) #其他条件
echo "NONE"
;;
Esac




——————————
function function
function fname () {//parameter does not appear in (), denoted by $
#程序段
Echo-n "Your choice is"
}
Calling function: fname parameter
such as printf 1


——————————
Cycle
while [condition]
Do
#程序
Done
Continue looping when conditions are met


until [condition]
Do
#程序
Done
The loop terminates when the condition is met


for ((I=1; i<100; i=i+1)
Do
#代码段
Done


For Var in con1 con2 Con3 ...
Do
#代码段
Done
The first cycle of the $var value is Con1, the second is Con2, the third layer is Con3


For example, all files under the display directory
filelist= ' ls $dir '
for filename in $filelist
Do
#echo
Done


————————————
Debugging of shell scripts
SH-NVX script.sh
-N Do not execute script, check syntax error no problem, no display
-V Print script to screen before execution
-X Displays the contents of the script used to the screen = = Print execution track

————————————————
#用mv取代rm
VI. BASHRC
Mkdir-p ~/.trash #加到文件尾
Alias Rm=trash
Alias R=trash
Alias rl= ' ls ~/.trash '
Alias Ur=undelfile
Undelfile () {
Mv-i ~/.trash/[email protected].
}
Trash () {
MV [email protected] ~/.trash/
}
SOURCE ~/.BASHRC

Summary of basic Linux commands

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.