Basic commands for Linux

Source: Internet
Author: User
Tags create directory unpack


#目录与文件操作 pwd LS CD mkdir CP MV RM file
/*{{{*/
I. PWD Displays the absolute path (the path represented by/to the start) of the current working directory
Eg:pwd
#选项-P Show link file path
Two. LS Display directory files and directories
Can take parameters, specify directories or files
Displays the current directory without parameters

Eg: View the contents of the Workdir directory in the home directory (~ on behalf of the user home directory)
LS ~/workdir
Ls

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

Eg: (without parameters) view all files and directories in the current directory
Ls-a

#B. Option-L displays property information for a file or directory (only the file size is interpreted, and other details are explained in the File System section)
Eg: Displays attribute information for files and directories in the current directory
Ls-l
Eg: Displays the property information for the specified directory or file, noting that multiple parameters can be taken!
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. Test your results yourself

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


Three. CD switch working directory about the path of the file system. Absolute vs. relative

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

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

Eg: switch to upper level directory
Cd..

Eg: switch to the previous directory
CD-

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

Four. mkdir New Empty Directory
Eg: Create directory under current directory dir
mkdir dir

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

Create a multilayer directory #选项-p recursion
Eg: Create a dir1/dir2/dir3 under ~
mkdir ~/dir1/dir2/dir3-p

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

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

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

Six. MV move the source file or directory to the destination file or directory, can be renamed
Eg: Move the test.txt file under the home directory to the shared directory
VI test.txt
MV Test.txt/mnt/hgfs/share

Eg: rename the videos directory under the home directory to Myvideos
MV Videos Myvideo

Seven. RM deletes one or more files or directories, physically deleted, not recoverable. -rf
Eg: Remove the jeffy-vim-v2.0_bak.zip in the home directory
RM Jeffy-vim-v2.0_bak.zip

#选项-rf to forcibly delete files or directories
Eg: Remove the dir directory from the home directory
Rm-rf dir

Eight. File View files Type
Eg: View the test.c file type in the home directory
File test.c

Eg: View the shared file type in the home directory
File shared

/*}}}*/

Practice:
/*{{{*/
If no directory is available under the/mnt/hgfs/directory, swap the shared directory for ~/videos
Cp
2. Back up the passwd file in the home directory to Passwd-bak
Cp
3. Create a new directory under the user's home directory Test_dir
Mkdir
4. Move the Passwd-bak to the Test_dir directory
Mv
5. Rename the Passwd-bak file in the Test_dir directory to passwd

6. Copy the Test_dir directory to your shared directory

7. Rename the Test_dir directory under the shared directory 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 in the home directory 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 Comparison of two file differences

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

#选项-N Displays line numbers when displaying file contents
Eg: Displays the file test.c in the home directory and displays the 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 this problem.
Use similarities and differences.
Eg: split-screen display of file contents
more/etc/passwd
less/etc/passwd

Same Operation Method
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.
Displays the difference, more displays the current display percentage.

Exit Press the Q key.

13. Head/tail Show file start/end
If you want to see only the beginning of a file or the last few lines, 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 usage is similar
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
Can be viewed through the Echo $LANG
Eg: Sort view file contents
VI test.c
Write characters, numbers, symbols per line
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 Difference

#选项-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 Display file contents, remove adjacent duplicate rows
Eg: repeating adjacent rows displays only one row when displayed.
Uniq Test

16, diff by line comparison of two files, the two files in different content display
Commonly used in the old and new version of the program comparison
Eg: show sort1.c and sort2.c different places
Diff sort1.c Sort2.c
Display results are not intuitive, it is recommended to use VIM sort1.c sort2.c-d instead, intuitive comparison
Display results in a for Append, d for Delete, C for change,
< represents the first file,,>, that belongs to the second file.

/*}}}*/

#查找与搜索 find grep
/*{{{*/
17, find in the current directory recursively find the file display path, that is, including subdirectories
Usage: find-name filename
or find-name ' filename '
or find-name "filename"
Eg: Find the test.c file under the current directory (home directory)
Find-name test.c

Usage: Find path-name filename
Eg: Find the test.c file in the Workdir directory
Find ~/workdir-name test.c

18. grep finds the string in the 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 display the line number
grep "Tim"/etc/passwd

Eg: searches for "main" (not including subdirectories) in all files in the current directory
grep "Main" *

Eg: Search for "main" (including subdirectories) in all files in the current directory
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: Extract the test.zip into the current directory
Unzip Test.zip

20, gzip a file for normal compression, can not be compressed directory
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 high proportion of a file compression, can not be compressed directory
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 directories
#常用选项
-C Create
-X Release

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

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

! usage
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 gzip bzip2 command compression
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 displays a line of text, often used to display environment variable values
Eg: Display string Hello world!
echo "Hello world!"
echo Hello world!
Note 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 command path, source, man manual file location
Whereis ls

30, man search 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 executes a command as Superuser
Eg: to view the contents of the account password file under the system, super User privileges are required.
$cat/etc/shadow
$cat:/etc/shadow:permission denied

$sudo Cat/etc/shadow
$[sudo]password for Tim: <= Enter the TIM user's password, note that the password will not be echoed!

32, su temporary switch user
Eg: switch to the root user
Su
PASSWD: <= Enter the root user password!
Exit the user who can exit the switch

Eg: switch to the root user and switch to the root master directory/root
Su-
PASSWD: <= Enter the root user password

Note If the first switch root user may be prompted with a password error

33. Change Password passwd
Eg: Modify the TIM user password
sudo passwd Tim <= plus sudo is a short password that can be set

Eg: Change the root password
sudo passwd root

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

Eg: Shut down immediately
sudo shutdown-h now

Eg: Restart now
sudo shutdown-r now
sudo reboot

/*}}}*/

#用户管理 AddUser Deluser
/*{{{*/
35, AddUser Add user, will be asked to enter the new user password, full name,
and create a new user home directory under the/a directory
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 directory 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.