Basic Linux commands

Source: Internet
Author: User
Tags unpack zip extension

Basic Linux commands


# Directory and file operations pwd ls cd mkdir cp mv rm file
/*{{{*/
1. pwd: displays the absolute path of the current working directory (the path expressed as the/starting point)
Eg: pwd
# Option-P: display the link file path
Ii. ls display files and directories under the Directory
You can specify a directory or file with parameters.
Displays the current directory without Parameters

Eg: view the contents of the workdir directory in the main directory (~ Represents the user's main directory)
Ls ~ /Workdir
Ls

# A. Option-a: display all files, including hidden files (Files starting)
Example: (with parameters: specify a directory or file) view all files under the workdir directory in the main directory
Ls-a/home/tim/workdir

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

# B. Option-l display the attribute information of a file or directory. (Only file size is explained. Other details are explained in the file system section)
Eg: displays the attributes of files and directories in the current directory.
Ls-l
Eg: displays the attribute information of the specified directory or file. Note that multiple parameters can be included!
Ls-l/home/tim examples. desktop

# C. The option-h is used to display the file size in the unit of K, M, and G. It is used with-l.

Note: In bash, l, la, and ll are named by alias. Test the effect.

Note: several special directories .../~ -


3. Switch the path of the file system in the working directory of cd. Absolute and relative

Eg: returns the Home Directory of the current user without options and parameters.
Cd press ENTER

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

Eg: switch to the upper directory
Cd ..

Eg: switch to the previous directory
Cd-

Eg: switch to the upper directory of the upper directory, and switch from the main directory to the root directory.
Cd ~
Cd ../..

4. Create an empty directory using mkdir
Eg: Create the directory dir in the current directory
Mkdir dir

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

# Option-p recursively create multi-level Directories
Eg: In ~ Create dir1/dir2/dir3
Mkdir ~ /Dir1/dir2/dir3-p

5. Copy the source file or directory to the target file or directory by using cp to implement backup.
Eg: copy the files in the shared directory to the home directory.
Cp/mnt/hgfs/share/jeffy-vim-v2.0.zip ~

For example, set jeff-vim-v2.0.zip under the main directory to jeffy-vim-v2.0_bak.zip.
Cp jeff-vim-v2.0.zip jeffy-vim-v2.0_bak.zip

# Option-a: copy the contents of the entire directory
Copy the directory jeffy-vim-v2.0 to the Desktop directory
Cp jeffy-vim-v2.0 ~ /Desktop-

6. You can rename a music video file or directory by moving it to the target file or directory.
Eg: Move the test.txt file under the main directory to the shared directory.
Vi test.txt
Mv test.txt/mnt/hgfs/share

Eg: Rename the Videos directory in the main directory to MyVideos
Mv Videos MyVideo

7. rm deletes one or more files or directories. It is physically deleted and cannot be recovered. -Rf
Eg: delete jeffy-vim-v2.0_bak.zip from the main directory.
Rm jeffy-vim-v2.0_bak.zip

# Option-rf force delete a file or directory
Eg: Delete the dir directory in the main directory
Rm-rf dir

8. View file Types
Eg: view the test. c file type in the main directory
File test. c

Eg: view the shared file type in the main directory
File shared

/*}}}*/

Exercise:
/*{{{*/
If no directory is available under the/mnt/hgfs/directory, replace the shared directory ~ /Videos
Cp
2. Back up the passwd file in your home directory as passwd-bak
Cp
3. Create the directory test_dir in the user's home directory.
Mkdir
4. Move passwd-bak to the test_dir directory.
Mv
5. Rename the passwd-bak file under 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
/*}}}*/

# Edit and view the file content touch vi/vim cat more/less head/tail sort uniq diff
/*{{{*/

9. touch can be used to create a new file
Eg: Create the file test_touch in the main directory
Touch test_touch

10. vi/vim create or edit
TIPS:
Vi file + line number open file time mark direct positioning to the specified line
Vi file1 file2-d compares the differences between the two files

11. cat displays file content in standard output (Display)
Eg: displays the file test. c In the main directory.
Cat test. c

# Option-n displays the row number when the file content is displayed
Eg: displays the file test. c In the main directory and the row number.
Cat-n test. c

12. more/less split screen display file
Cat displays all files on the screen at a time. It is inconvenient to view the previous content,
The more/less command can solve this problem.
Similarities and Differences
Eg: split-screen display of File Content
More/etc/passwd
Less/etc/passwd

Same Operation Method
Press d to flip the page, and press B to flip the page.
Press enter to flip the line, and press a space to flip the page.

Different operations: less supports up and down keys, pagedown keys, and pageup keys.
Display difference. more shows the percentage of currently displayed content.

Press q to exit.

13. head/tail: display the beginning/end of a file
If you only want to view the beginning or last few lines of the file, you can use the head/tail command. The default value is 10 lines.
Eg: view the content starting with 10 in the/etc/passwd file
Head/etc/passwd
Eg: View 10 contents at the end of the/etc/passwd file
Tail/etc/passwd

# Option-num: specify the number of lines displayed
Eg: view the first 20 rows of content in the/etc/passwd file
Head-20/etc/passwd
Similar to tail
Head and tail are often used in combination with pipelines


14. sort displays the file content in a small to large order by row. Note that the sorting result does not affect the original file.
The display result is related to the language family. To use the ASCII code value as the result, use LANG = C
You can use echo $ LANG to view
Eg: sort and View File Content
Vi test. c
Write characters, numbers, and symbols in each line
Sort test. c

# Option-sort and display r values in ascending order
Eg: View File Content in reverse order
Sort-r test. c

# Option-n sorts numbers by numerical results instead of ASCII values.
Eg: sort by numeric values
Vi test
Write numbers in each row
Sort test
Sort-n test
Display differences

# Option-o specify the sorting result to be output to the file
Eg: Write the test. c sorting result back to test. c.
Sort test. c-o test. c

15. uniq: display the file content and remove the adjacent duplicate rows.
Eg: duplicate adjacent rows show only one row.
Uniq test

16th and diff compare the differences between the two files by line, and display the different contents of the two files
Comparison of old and new versions of Programs
Eg: show the differences between sort1.c and sort2.c
Diff sort1.c sort2.c
The display result is not intuitive. We recommend that you use vim sort1.c sort2.c-d instead for intuitive comparison.
In the displayed result, a indicates appending, d Indicates deleting, and c indicates modifying,
<Indicates to belong to the first file,> indicates to belong to the second file.

/*}}}*/

# Find and search for find grep
/*{{{*/
17. find recursively searches for the file display path in the current directory, that is, including subdirectories
Usage: find-name filename
Or find-name 'filename'
Or find-name "filename"
Eg: Search for the test. c file in the current directory (main directory)
Find-name test. c

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

18. Search for strings in the file using grep
Eg: search for "tim" in the/etc/passwd file"
Grep "tim"/etc/passwd

# Option-n: display the row number of the string
Eg: search for "tim" in the/etc/passwd file and display the row number
Grep "tim"/etc/passwd

Eg: search for "main" (excluding 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

# String Matching Method ^ $
Eg: search for all strings containing tim
Copy/etc/passwd to the Master Directory
Cp/etc/passwd ~
Grep "tim" passwd

Eg: Search for strings starting with tim
Grep "^ tim" passwd

Eg: Search for strings ending with tim
Grep "tim $" passwd

Eg: Search for strings containing only "tim"
Grep "^ tim $" passwd

/*}}}*/

# Package directory and compressed file (combined with ppt 3 66 pages) zip unzip gzip gunzip bzip2 bunzip2 tar
/*{{{*/

19. Zip: compress multiple files, generate a. Zip extension, and unzip the files.
! Usage: zip compressed package name. zip file 1 file 2 ......
Eg: compress test. c test1.c test2.c to the test.zip file.
Zip test.zip test. c test1.c test2.c

Eg: Decompress test.zip to the current directory.
Unzip test.zip

20. gzip compresses a file normally and does not compress directories.
Eg: compress test.cinto test.c.gz
Gzip test. c

21. decompress the gz compressed file using gunzip
Eg: Decompress test.c.gz
Gunzip test.c.gz

22. bzip2 compresses a file in a high proportion and does not compress directories.
Eg: compress the test.c file to test.c.bz2.
Bzip2 test. c

23. bunzip2 decompress the bz2 compressed file
Eg: Decompress test.c.bz2
Bunzip2 test.c.bz2

Twenty-four, tar Package Multiple files or directories
# Common options
-C create
-X release

-Z processes the gzip compressed tar package
-J: handle the tar package compressed by bzip2

-V: Show Details
-F indicates the file to be packaged or unwrapped, which must be placed at the end of the option

! Usage
Package
Eg: Pack jeffy-vim-v2.0to generate jeffy-vim-v2.0.tar.
Tar-cvf jeffy-vim-v2.0.tar jeffy-vim-v2.0

Unpack
Eg: unpack jeffy-vim-v2.0.tar
Tar-xvf jeffy-vim-v2.0.tar

First package, 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 <= generate jeffy-vim-v2.0.tar.gz
Or bzip2 jeffy-vim-v2.0.tar <= generate jeffy-vim-v2.0.tar.bz2

Directly package and compress
Use gzip to compress -czvfwith the 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

Compress With bzip2-cjvf, suffixed with .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

Decompress and decompress the package-xvf, which is effective for files processed by gzip and bzip2.
Tar-xvf jeffy-vim-v2.0.tar.bz2
Tar-xvf jeffy-vim-v2.0.tbz2
Tar-xvf jeffy-vim-v2.0.tgz

/*}}}*/

# Other commands clear date echo which whereis man
/*{{{*/

25. clear Screen
Shortcut Key ctrl + l

26. view date and time on date
Eg: date

27th. echo Displays a line of text, which is 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, multiple strings are output.

Eg: The result shows that the environment changes to the LANG value. Note that you need to add $ to view the environment variable.
Echo $ LANG

Twenty-eight, which: Find the command path
Eg: Find the path of ls
Which ls

19th. whereis: Find the path of the command, the source, and the location of the man manual file.
Whereis ls

Thirty man help manual
Eg: ASCII code table
Man ascii

Eg: query the whereis Command help page
Man whereis

Eg: query function strcmp

/*}}}*/

# System maintenance command sudo su passwd shutdown
/*{{{*/
On November 31, sudo executed a command as a superuser.
Eg: Super User permission is required to view the contents of the account and password files stored in the system.
$ 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 the password is not displayed!

. Su temporary switch user
Eg: switch to the root user
Su
Passwd: <= enter the root user password!
Users who exit the switch

Eg: switch to the root user and switch to the root directory/root
Su-
Passwd: <= enter the root user password.

Note: If the root user is switched for the first time, a Password error may be prompted.

33. Change passwd Password
Eg: Change the tim User Password
Sudo passwd tim <= short password can be set with sudo

Eg: Change the root password
Sudo passwd root

34. shutdown command
Eg: shutdown in 5 minutes
Sudo shutdown + 5

Eg: Shut down immediately
Sudo shutdown-h now

Eg: restart now
Sudo shutdown-r now
Sudo reboot

/*}}}*/

# Adduser deluser Management
/*{{{*/
35. When adduser is added, the new user password, full name, will be required,
Create a new user's home directory under the/home Directory
Eg: sudo adduser max <= enter the current user password

36. deluser: delete a user
Eg: delete user max
Sudo deluser max

# Option -- remove-home: delete a user and delete the user's home directory
Eg: sudo deluser -- remove-home max
/*}}}*/

Remaining commands
Du df mount umount ln "ls-l" chmod chown chgrp ps kill

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.