Basic commands for Linux

Source: Internet
Author: User
Tags bz2 clear screen diff echo display unpack


#文件夹与文件操作 pwd LS CD mkdir CP MV RM file
/*{{{*/
I. PWD displays the absolute path of the current working folder (the path represented by/to the starting point)
Eg:pwd
#选项-P Show link file path
Two. ls display folder under Files and folders
Ability to take a number of parameters, specify a folder or file
Displays the current folder without a number of parameters

Eg: View the contents of the Workdir folder under the home folder (~ Represents the user home folder)
LS ~/workdir
Ls

#A. Option-a displays all files, including hidden files (files beginning with.)
Eg: (with parameters: Specify a folder or file) View all files under the Workdir folder in the home folder
Ls-a/home/tim/workdir

Eg: (without parameters) view all files and folders under the current folder
Ls-a

#B. Option-L displays property information for a file or folder (explains only the file size, and other details are explained in the File System section)
Eg: Displays property information for files and folders under the current folder
Ls-l
Eg: Displays the property information for the specified folder or file, with the ability to take more than one number of parameters!
Ls-l/home/tim Examples.desktop

#C. Option-H displays the file size in K,m,g and with-L.

Note: L,la,ll is named after bash by alias. Effect Please Test yourself

Note: A few special folders. .. / ~ -


Three. CD switch work folders about the path of the file system. Absolute vs. relative

Eg: without options and parameters. Returns the current user's home folder.
CD Enter

Eg: Switch to the home folder under the root folder
Cd/home

Eg: Switch to upper folder
Cd..

Eg: Switch to the previous folder
CD-

Eg: switch to the upper folder of the upper folder and switch from the home folder to the root folder.
CD ~
Cd.. /..

Four. mkdir New empty Folder
Eg: Create folder under current folder Dir
mkdir dir

Eg: Create a folder under the/home/tim/workdir folder dir
Mkdir/home/tim/workdir/dir

Create multi-tiered folders #选项-p recursion
Eg: Create a dir1/dir2/dir3 under ~
mkdir ~/dir1/dir2/dir3-p

Five. CP Copy the source file or folder to the target file or folder, can realize the backup
Eg: Copy the files under the shared folder to the home folder
Cp/mnt/hgfs/share/jeffy-vim-v2.0.zip ~

Eg: Back up the Jeff-vim-v2.0.zip under the home folder to Jeffy-vim-v2.0_bak.zip
CP Jeff-vim-v2.0.zip Jeffy-vim-v2.0_bak.zip

#选项-a Copy entire folder contents
Eg: Copy the folder jeffy-vim-v2.0 to the Desktop folder
CP jeffy-vim-v2.0 ~/DESKTOP-A

Six. MV move the source file or folder to the destination file or folder. can implement renaming
Eg: Move the test.txt file under the home folder to a shared folder
VI test.txt
MV Test.txt/mnt/hgfs/share

Eg: Rename the videos folder under the home folder to Myvideos
MV Videos Myvideo

Seven. RM deletes one or more files or folders. Physical deletion, unrecoverable. -rf
Eg: Remove the Jeffy-vim-v2.0_bak.zip under the home folder
RM Jeffy-vim-v2.0_bak.zip

#选项-rf to forcibly delete a file or folder
Eg: Delete the Dir folder under the home folder
Rm-rf dir

Eight. File View files Type
Eg: View the test.c file type under the home folder
File test.c

Eg: view shared file types under Home folder
File shared

/*}}}*/

Practice:
/*{{{*/
Assuming there are no folders available under the/mnt/hgfs/folder, change the shared folder to ~/videos
Cp
2. Back up the passwd file under the owner's folder to Passwd-bak
Cp
3. Create a new folder under the user's home folder Test_dir
Mkdir
4. Move Passwd-bak to the Test_dir folder
Mv
5. Rename the Passwd-bak file under the Test_dir folder to passwd

6. Copy the Test_dir folder to your shared folder

7. Rename the Test_dir folder under the shared folder to Dir

8. Delete the files generated above
/*}}}*/

#编辑与查看文件内容 Touch Vi/vim cat more/less head/tail sort uniq diff
/*{{{*/

Nine. Touch can be used to create a new file
Eg: Create a file under the home folder Test_touch
Touch Test_touch

10. Vi/vim New or edited
Tips for using:
VI file + line number the cursor navigates directly to the specified line when the file is opened
VI file1 file2-d difference of two files

11. Cat displays file contents on standard output (display)
Eg: Displays files under the home folder test.c
Cat test.c

#选项-N Displays line numbers when displaying file contents
Eg: Show file under home folder test.c line number at the same time
Cat-n test.c

12. more/less Split Screen display file
Cat display files all at once on the screen, it is not easy to view the previous content,
more/less command can solve the problem
Use similarities and differences.
Eg: split-screen display of file contents
more/etc/passwd
less/etc/passwd

Same method of operation
D key next page, B key page
Carriage return under the line, the space to turn the page

Different operations: less supports up and down keys, PageDown and PageUp keys up and down the page.
Show the difference. More displays the current percentage of display content.

Exit Press the Q key.

13. Head/tail Show file start/end
If you just want to see the beginning or the last few lines of the file, you can use the Head/tail command to display 10 rows by default
Eg: View the beginning of the/etc/passwd file 10 content
head/etc/passwd
Eg: View the 10 content at the end of the/etc/passwd file
tail/etc/passwd

#选项-num Specifies the number of rows that display content
Eg: view/etc/passwd file at the beginning of 20 lines of content
head-20/etc/passwd
Tail using similar methods
Head and tail are often used in conjunction with pipelines


14. Sort by row small to large display file contents, note that sorting results do not affect the original file
Display results are related to the language family, in order to use Lang=c Unified for the result of ASCII code values
Ability to view through Echo $LANG
Eg: Sort view file contents
VI test.c
Each line writes characters, numbers. Symbol
Sort test.c

#选项-R sort display from large to small
Eg: reverse order view File contents
Sort-r test.c

#选项-N Sorts numbers by numeric results instead of ASCII values
Eg: Sort by numeric value
VI Test
Write numbers per line
Sort test
Sort-n Test
Show Differences

#选项-o Specifies the sort result output to a file
Eg: write test.c sort results back to test.c
Sort Test.c-o test.c

15. Uniq Displays the contents of the file. Remove adjacent rows that are repeated
Eg: repeated rows are displayed with only one row displayed.
Uniq Test

16, diff by Line compared to two files, the two files in different content display
Often used in the program of the old and new version number control
Eg: show sort1.c and sort2.c different places
Diff sort1.c Sort2.c
Display results are not intuitive. It is suggested to replace with vim sort1.c sort2.c-d, visually control
A indicates append in the display result. D means delete, C means change,
< represents the first file,,>, that belongs to the second file.

/*}}}*/

#查找与搜索 find grep
/*{{{*/
17, find in the current folder recursively find the file display path, that is, include subfolders
How to use: Find-name filename
or find-name ' filename '
or find-name "filename"
Eg: Find the test.c file under the current folder (home folder)
Find-name test.c

How to use: Find path-name filename
Eg: Find the test.c file under the Workdir folder
Find ~/workdir-name test.c

18. grep finds a string in a file
Eg: Find "Tim" in the/etc/passwd file
grep "Tim"/etc/passwd

#选项-N Displays the line number where the string is
Eg: Look for "Tim" in the/etc/passwd file. and displays the line number
grep "Tim"/etc/passwd

Eg: Search for "main" (no subfolders) in all files under the current folder
grep "Main" *

Eg: Search for "main" (including subfolders) in all files under the current folder
grep "Main" *-NR

#搜索字符串的匹配方式 ^ $
Eg: Find all strings that contain Tim
Copy the/etc/passwd to the main record
CP/ETC/PASSWD ~
grep "Tim" passwd

Eg: Find a string that starts with Tim
grep "^tim" passwd

Eg: search for a string ending with Tim
grep "tim$" passwd

Eg: search for a string containing only "Tim"
grep "^tim$" passwd

/*}}}*/

#打包文件夹与压缩文件, (combined with PPT 3 66 pages) Zip unzip gzip gunzip bzip2 Bunzip2 Tar
/*{{{*/

19. zip to compress multiple files, generate. zip suffix name zip file, unzip to unzip
! Usage: Zip archive package name. zip file 1 File 2 ...
Eg: Compress the test.c test1.c test2.c into the Test.zip file
Zip test.zip test.c test1.c test2.c

Eg: Unzip the test.zip into the current folder
Unzip Test.zip

20, gzip a file for normal compression, can not be compressed folder
Eg: Compress the test.c into test.c.gz
Gzip test.c

21, Gunzip to the GZ format compressed file decompression
Eg: Unzip the test.c.gz
Gunzip test.c.gz

22, bzip2 a file for a high proportion of compression. Unable to compress folder
Eg: Compress test.c files to test.c.bz2
Bzip2 test.c

23, BUNZIP2 the BZ2 format compressed file decompression
Eg: Unzip the test.c.bz2
BUNZIP2 test.c.bz2

24. Tar to package multiple files or folders
#经常使用选项
-C Create
-X Release

-Z Processing of gzip compressed tar packages
-J processing of bzip2 compressed tar packages

-V Displays specific information
-f Specifies the package or unpacked file to be placed at the end of the option

! How to use
Packaged
Eg: package jeffy-vim-v2.0 to generate Jeffy-vim-v2.0.tar
TAR-CVF Jeffy-vim-v2.0.tar jeffy-vim-v2.0

Unpack
Eg: Unpack the Jeffy-vim-v2.0.tar
TAR-XVF Jeffy-vim-v2.0.tar

Pack first. Then use the gzip bzip2 command to compress
TAR-CVF Jeffy-vim-v2.0.tar jeffy-vim-v2.0
Gzip Jeffy-vim-v2.0.tar <= Build jeffy-vim-v2.0.tar.gz
or bzip2 Jeffy-vim-v2.0.tar <= generate jeffy-vim-v2.0.tar.bz2

Package and compress directly
Compress-CZVF with gzip, suffix. tar.gz or. tgz
TAR-CZVF jeffy-vim-v2.0.tar.gz jeffy-vim-v2.0
TAR-CZVF jeffy-vim-v2.0.tgz jeffy-vim-v2.0

With bzip2 compression-cjvf, suffix. tar.bz2 or. tbz2
TAR-CJVF jeffy-vim-v2.0.tar.bz2 jeffy-vim-v2.0
TAR-CJVF jeffy-vim-v2.0.tbz2 jeffy-vim-v2.0

Unzip and unpack-XVF, valid for gzip and bzip2 processed files
TAR-XVF jeffy-vim-v2.0.tar.bz2
TAR-XVF jeffy-vim-v2.0.tbz2
TAR-XVF jeffy-vim-v2.0.tgz

/*}}}*/

#其他命令 Clear Date echo which Whereis man
/*{{{*/

25. Clear Clear Screen
Shortcut keys CTRL + L

26. Date and time of view
Eg:date

27, Echo display a line of text, often used to display environment variable values
Eg: Display string Hello world!
echo "Hello world!"
echo Hello world!
Pay attention to the difference. Add "" As-is output. Otherwise count multiple string outputs

Eg: Displays the value of the environment change to Lang, note that the view environment variable needs to be added $
Echo $LANG

28. Which find the path of the command
Eg: find the path to LS
which LS

29, Whereis find the path of the command, source, man manual file location
Whereis ls

30, man check help manual
Eg: Check the ASCII code table
Man ASCII

eg: check Whereis command help page
Mans Whereis

Eg: check function strcmp

/*}}}*/

#系统维护命令 sudo su passwd shutdown
/*{{{*/
31. Sudo runs a command as Superuser
Eg: Check the system under the account password file content, need Superuser privileges
$cat/etc/shadow
$cat:/etc/shadow:permission denied

$sudo Cat/etc/shadow
$[sudo]password for Tim: <= Enter the password of the Tim user. Note that password will not echo!

32, SU temporarily switch users
Eg: switch to the root user
Su
PASSWD: <= input root User password!
Exit the user who can exit the switch

Eg: switch to the root user and switch to the root home folder/root
Su-
PASSWD: <= input root user password

Note Assuming that the first switch root user may prompt for password error

33, change passwordpasswd
Eg: change Tim user Password
sudo passwd Tim <= plus Sudo is able to set short password

Eg: Change Rootpassword
sudo passwd root

34. Shutdown Shutdown Command
Eg:5 minutes after shutdown
sudo shutdown +5

Eg: Close the machine immediately
sudo shutdown-h now

Eg: Restart now
sudo shutdown-r now
sudo reboot

/*}}}*/

#用户管理 AddUser Deluser
/*{{{*/
35, AddUser Join the user, will be asked to enter the new user password. Name
and create a new user home folder under the/* folder
Eg:sudo adduser Max <= Enter the current user password

36. Deluser Delete User
Eg: delete user max
sudo deluser max

#选项--remove-home, delete the user's home folder at the same time
Eg:sudo Deluser--remove-home Max
/*}}}*/

The rest of the order
Du DF Mount Umount Ln "ls-l" chmod chown chgrp PS Kill

Basic commands for Linux

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.