20165306 preparatory work 3 Linux installation and learning

Source: Internet
Author: User
Tags chmod create directory first string parent directory rar readable set background file permissions

View a number of tutorials, VirtualBox and Ubuntu have been installed to complete. The following is an experimental report, an experiment, an unsolved problem, and an understanding of the basic basics of Linux learning.

Experiment three user and file Rights Management one, Linux User management (a) View commands 1. Enter the command:

$who am iOr$who mom likes
Output: Shiyanlou pts/0 2018-02-14 14:42 (: 1.0)

To view the user name of the currently logged in user, use WhoAmI directly to

2.who command Other common parameters
    • -A print all that can be printed
    • -D Print dead process
    • -M with AM I,mom likes
    • -Q Print The current number of logged in users and user name
    • -U print current logged in user login information
    • -R print Run level

(ii) Create a user 1. Two prerequisites for using the sudo command:
    • Know the password of the currently logged on user (top of document)

      Note: The Linux password input is not displayed for any content
    • The current user must be in the sudo user group

      2. For example:

      Enter the command:$sudo adduser ariel

ls /home

su -l ariel

(c) User group 1. Meaning:

A collection of users that share some resources and permissions while owning a private resource

2. How do you know which user groups you belong to in Linux? Method one, using the Groups command

Input:$groups shiyanlou
Output: Shiyanlou:shiyanlou a colon before the user, and a colon after the user group that the user belongs to

Method Two, view/etc/group file

Input:$cat /etc/group | sort
Output: The last line is user group information
Input: $cat /etc/group | grep -E "shiyanlou" filter out some of the results you don't want to see
Output: shiyanlou?5000:

    • Cat command: Used to read the contents of the specified file and print the terminal output
    • | Sort: Indicates that the text to be read is sorted in a dictionary and then output
    • Etc/group file Format Description group_name:password:GID:user_list

3. Add another user to the sudo user group

Input:$su shiyanlou
$groups xyh

$sudo usermod -G sudo xyh

$groups xyh

$su -l xyh

$sudo ls

(iv) Deletion of users

Input:$sudo deluser ariel --remove-home

Second, Linux file permissions (a) View file permissions

Input:$ls -l

    • Everything in Linux is file

(ii) Change of file owner

First log in User name Xyh, create a new file named books
Input:$touch books

$ll books
Swap back to Shiyanlou user identity
Input:$cd /home/xyh

$ls books

$sudo chown shiyanlou books

$ll books

(iii) Modify the file permissions method one or two binary digit representation
    • 0: No permissions, 1: Executable x, 2: Writable W, 4: Readable R
    • A total of 10 locations, first if-the non-directory file, if D is the directory

Input:$echo "echo \"hello shiyanlou\"">books

$chmod 600 books

$ll books

$su xyh

$cat books

Method Two, add and subtract assignment operation

U:user, G:group, O:others, a:all, + promotion,-remove
Input:$chmod go-rw books

$ll books

Third, more

What is the difference between AddUser and useradd?

    • Useadd, Userdel This kind of operation is more like a kind of command, the execution is finished to return
    • AddUser is more like a program that requires input, determination and a series of operations
Iv. problems encountered

Ii. (iii) Methods of modifying file permissions in the two plus minus assignment operation, what does the u/g/o follow?
For example: "U-x" Why is "--w--wx-wx"?

Experiment four, Linux directory structure and file base directory one, Linux directory structure 1.FHS standard (file system hierarchy Standard)

2. Directory path Path

CD Switch Directory

. Current directory

.. Top level Directory

-Previous Directory

~ Current user's home directory

PWD Gets the current path

Absolute path

The full path starting with the root "/" directory, ending with the directory you want to go to

Relative path

to the current directory. As the starting point, to the end of the directory you want to go to

For example: Start with the home directory and enter the/usr/local/bin directory

Absolute path: $cd /usr/local/bin$pwd

Relative path: $cd ../../usr/local/bin$pwd

Ii. basic operation of Linux files 1. New
    • Create a new blank file

      $cd ~Switch back to the user's/home/shiyanlou directory

      $touch testCreate a blank file named Test

    • New Catalog
    • Use the mkdir command to create an empty directory, or you can specify permission properties to create a directory
      mkdir mydir
    • Use the-p parameter to create the parent directory at the same time

$mkdir -p father/son/grandson

$cd father/son/grandson

$pwd

2. Copying
    • Copying files

$cp test father/son/grandson

    • Copy Directory

$mkdir familyCreate a Directory

$cp -r father familyPlus-R or-R parameters, indicating recursive replication

3. Delete
    • deleting files

$rm testIf the delete is enforced $rm -f test .

For example:$touch test

$chmod 444 test

$ll test

$rm test

$rm -f test

    • Delete Directory

$rm -r familyPlus-R or-R parameters

4. Move file and file rename
    • Moving files

"MV Source directory file destination directory"

$mkdir docCreate Directory Doc

$touch fileCreate file

$ls fileOutput file

$mv file docTo move file files to the doc directory

$cd docSwitch Directory doc

$lsOutput file

    • Renaming files

"MV old file name new filename"
$mv file1 file2

    • Batch rename (slightly, using the rename command)
5. View Files
    • viewing files using the cat, TAC commands: Print file contents to standard output (terminal), Cat positive order, TAC reverse, can be added with the-n parameter to display line numbers

$cp /etc/passwd .Copy the passwd file from the/etc directory

$cat passwd

$cat -n passwd

    • View files with the NL command: Add line numbers and print

$nl -b a fileIndicates that the line number (same as "cat-n") is also listed, regardless of whether it is a blank line

$nl -b t fileList only non-blank lines and list (default)

$nl -n ln fileDisplay at the leftmost end of the row number field

$nl -n rn fileDisplay at the right end of the line number field without adding 0

$nl -n rz fileDisplay at the right end of the line number field, plus 0

$nl -w 2 -n rz file-W plus number indicates the number of digits occupied by the line Number field, which defaults to 6 bits

    • Use the more and less commands to page through the file, you can directly use the keyboard operation page

$cp /etc/passwd .

$more passwd

Only one screen is displayed by default, the bottom of the terminal shows the current reading progress,

Enter: Scroll down one line,

Space: Roll down the next screen,

H: Show Help, Q: Exit

    • Viewing files using the head, tail command

$head /etc/passwd10 rows only, default is 10 rows, less than 10 lines show all

$head -n 2 /etc/passwdOnly 2 lines

$tail /etc/passwdLook only after 10 lines

$tail -n 3 /etc/passwdLook only after 3 lines

6. View File types

The type of file in Linux is not judged by the file suffix, typically using the file command to view the type of files
$file /bin/ls

7. Edit the file

See Vim Editor

Vim Quick Start one, mode introduction

6 Basic modes: normal, INSERT, command Line, visual, select, Ex

Two or three common modes of switching
    • Normal → insert press I key or a key
    • Normal → command line press: Key
    • Insert, command line → Normal press the ESC key
Third, enter the VIM1. Use the VIM command to enter the Vim interface
    • "Vim file name" to enter an existing $vim practice_1.txt or new file

    • $vimEnter command line mode after input :e 文件路径 can also open the corresponding file

2. Normal mode downstream label movement

H left, J down, k up, L right, W moves to the next word, B moves to the previous word

Iv. Entering insert mode
    • I edit at the current cursor
    • I Insert at the beginning of the line
    • A inserts at the end of A row
    • A after the cursor is inserted
    • o Insert a new row after the current line
    • O insert a new row before the current line
    • CW replaces characters from the position of the cursor to the end of a word
Five, save the document in command line mode

Normal Mode input: Enter command line mode

    • Enter W return to save the document
    • Enter to save the :w 文件名 document as a different file name or save it to a different path
Vi. exit vim1. Exit vim in command line mode
    • : Wq Save and exit
    • : wq! Force Save and exit
    • : Q exit
    • : q! Force quit, do not save
    • :w< file path > Save As
    • : Save SaveAs file path as
    • : X Save and exit
2. Exiting vim in normal mode
    • Shift+zz Save and exit
Vii. Delete vim text information in normal mode
    • X or delate Delete the character that the cursor contains
    • X Delete the previous character of the cursor
    • DD Delete entire row
    • DW Delete a word (not in Chinese)
    • d$ or D Delete to end of line
    • d^ Delete to the beginning of the line
    • DG deleted at end of document
    • D1G Delete to document header
    • "Number + command" means deleting more than one row at a time, such as 2dd deleting 2 rows at a time
Vim document Edit 1.vim Repeat command 1.1 repeat last command

$cp /etc/protocols .Copy test files to a local directory

$vim protocolsOpen a file for editing

.repeat the last command operation in normal mode

For example: Enter x in normal mode, delete the character of the cursor, the input . will delete one character again, in addition to repeat the delete operation of DD

1.2 Execute the same command in the specified number of times

Input n+命令 , examples are as follows:

10xDelete 10 consecutive characters

3ddDelete 3 lines of text

dwor daw delete a word

dnwDelete N words

2. Quick jump of cursor (normal mode) 2.1 inline jump

n shift+g(nG)Cursor moves to nth row

ggCursor moves to the first row

shift+g(G)Cursor moves to the last row

    • If the line number is not displayed by default, enter command mode to : set nu display the line number
    • You can use ctrl+o quick to go back to the previous (before jump) cursor position
2.2 In-line jump

In normal mode, use the following command to jump in a word in a row
wTo the beginning of the next word

eTo the end of the current word

bTo the beginning of the first word

geTo the end of the previous word

0Or ^ to a costume.

$To end of line

~Change the letter of the cursor to uppercase or lowercase

f+字母Search backwards for letters and jump to the first matching position

F+字母Search for letters forward and jump to the first matching position

t+字母Searches backwards for letters and jumps to a letter before the first matching position

T+字母Search for letters forward and jump to a letter before the first matching position

3. Copy and paste and cut 3.1 copy and paste text 1. Use Y to copy in normal mode

yyCopy the entire row of the cursor

3yyCopy 3 rows

y^or y0 copy to the beginning of the line without the character at the cursor

y$Copy to end of line with character at cursor

ywCopy 1 words

y2wCopy 2 words

yGCopy to end of text

y1GCopy to beginning of text

2. Paste with P in normal mode

p(lowercase) after pasting to cursor (bottom)

P(uppercase) Paste to the front of the cursor (top)

3.2 Cutting and pasting

ddpQuick swap the line with the cursor and the line below it

Find replacement 1. Character Substitution and revocation (undo action)

Normal mode,

r、指定字母Press R First, and then the specified letter to replace the cursor with the specified letter

RPress R First, the replace Word appears, enter the insertion mode, can be replaced continuously, until you press ESC, exit and remember to change to lowercase lock

ccReplace the entire row, which deletes the row of the cursor and enters insert mode

cwReplace a word, delete a word, and enter insert mode

C(uppercase) Replace cursor to end of line

~Reverses the case of a cursor in the same letter

uUndo One operation

u{n}Undo n operations??? (See the problems encountered)

UUndo All Current Changes

ctrl+rUndo undo action??? (see the problem encountered)

2. Fast Indent 2.1 Use commands to quickly adjust indent operations (normal mode)

:set nuShow line Numbers

15GJump To line 15th

>>Indent the entire line to the right

<<The entire line is rolled back to the left

2.2shiftwidth command

:set shiftwidth?Gets the current set value

:set shiftwidth=10Set indent to 10 characters

>>See how the Indent changes

2.3 Adjusting the text position

:ce(center) Centers the text of the bank

:ri(right) to make our text on

:le(left) to leave the bank's text on

3. Find 3.1 Quick Find

Enter in normal mode / , then type the string you want to find and press ENTER to find it
/Look down

?Look up

nContinue to find

NReverse Lookup

3.2 Advanced Find

Normal mode,

\*Look backwards (down) for the word where the cursor is located

\#Forward (top) Find the word that the cursor is in

g\*Same as *, but partially conform to the word

g\#The same #, but partially matches the word

4. Problems encountered
    • " u{n} Undo N Operations" How to use this command?
    • ctrl+rWhat is the role? "Already at newest" is displayed after pressing "Ctrl+r", but no other changes are seen.
Getting Started with advanced features 1. Multi-file editing 1.1 using Vim to edit multiple files

Two kinds of forms:

    • The parameters used before entering Vim are multiple files.

    • Enter vim and edit other files

$vim 1.txt 2.txt

Create two new files and edit them simultaneously

:nEdit 2.txt File

:n!Forced switchover, not saved

:NEdit 1.txt File

:N!Forced switchover, not saved

1.2 Open new file after entering vim

:e 3.txtOpen a new file 3.txt

:e#Go back to the previous file

:lsYou can list previously edited documents

:b 2.txtDirect access to file 2.txt editing

:bd 2.txtYou can delete a file item in a previously edited list

:e! 4.txtNew Open File 4.txt, discard the file you are editing

:fDisplay the file name you are editing

:f new.txtChange the file being edited name to New.txt

1.3 Recovering files

$vim -r 1.txt

:ewcover 1.txt

2. Visual mode
    • Normal mode input v (lowercase), enter the character select "VISUAL" mode, you can move the cursor, where the cursor will be selected, press V again will cancel the selection
    • Normal mode input, enter the shift+v line select "VISUAL lines" mode, you can move the cursor up and down to select more rows, press Shift+v again will cancel the selection
    • Normal mode input, enter the ctrl+v area Select "VISUAL BLOCK" mode, you can move the cursor selection area up or down, press CTRL + V to deselect
    • In the visual "visual" Mode to enter d or x delete the selection
    • In the visual "visual" mode, enter y the copy selection
3. Windows operation

:newOpen a new Vim window

:sp 1.txtOpen a new horizontal split-screen window to edit 1.txt

:vsp 2.txtOpen a new Vertical split screen window to edit 2.txt

4. Create an encrypted document

$vim -x file1

5. Executing external commands in vim

:! lsUsed to display the contents of the current directory

:! rm FILENAMEUsed to delete a file named filename

:w FILENAMESave the file you are editing in the current vim as a filename file

viewing Help in 6.vim
    • Press F1 in normal mode to open Vim's own preset help document
    • :h shiftwidthOpen a Help file named Shiftwidth
    • :verDisplay version and Parameters
7. Function setting 7.1 Get the current setting

:setOr :se Show all the modified configurations

:set allShow all the set values

:set option?Display the Set value of option

:set nooptionCancel the current set value

Description of the 7.2set function

:set aiSet Auto Indent

:set awSet up AutoArchive, default not open

:set background=back或lightSet the background style

:set bkSet up automatic backup, not open by default

:set cinSet C speech Style indentation

Experiment Five, environment variables and files find one, environment variables 1. Variables

declare tmpTo create a variable named TMP using the DECLARE command

tmp=shiyanlouAssign the variable TMP to Shiyanlou

echo $tmpRead the value of the variable, using ECHO and $

Note: variable names can only be English letters, numbers, or underscores, and cannot begin with a number

2. Environment variables

Three commands for printing environment variable information:

    • Set shows all variables of the current shell

    • ENV displays the environment variables associated with the current user

    • Export shows variables exported from the shell to environment variables

Note: In order to differentiate from normal variables, we are accustomed to setting the environment variable name to uppercase

3. Search path and order of commands

echo $PATHView the contents of the PATH environment variable

4. Add a custom path to the "path" environment variable

PATH=$PATH:/home/shiyanlou/mybinAdd a custom path (note: Absolute path must be used here)

echo "PATH=$PATH:/home/shiyanlou/mybin">>.zshrc

    • >>Indicates that standard output is redirected to a file in Append mode

    • >Represents redirection to a file in a way that overrides

5. Modifying and deleting an existing variable 5.1 variable modification

${变量名#匹配字串}From head to back, shortest

${变量名##匹配字串}From top to back, longest

${变量名%匹配字串}From the back forward, the shortest

${变量名%%匹配字串}From the back forward, the longest

${变量名/旧的字串/新的字串}Replace the first string that matches the old string with a new string

${变量名//旧的字串/新的字串}Replace all strings that match the old string with a new string

For example:

path=$PATH

echo $path

path=${path%/home/shiyanlou/mybin}

path=${path%*/mybin}

5.2 Variable Deletion

$unset temp

6. How to make environment variables effective immediately

$. ./.zshrcYou must specify a complete absolute or relative path name

$source .zshrcDon't need

Second, search file 1.whereis simple fast 2.locate fast and full 3.which small and fine 4.find fine and fine

Time-related command parameters:
-atimeLast Access time

-ctimeLast time the file content was modified

-mtimeLast time the file properties were modified

For example:

-mtime n: Modified files within one day before n days

-mtime +n: Files that were modified before n days (not including the Nth Day itself)

-mtime -n: Files that have been modified within n days (containing the nth day itself)

-newer file: File is an existing document, listing the new file name

For example:

$find ~ -mtime 0List files that have changed in the home directory for the day (within 24 hours)

$find ~ -newer /home/shiyanlou/CodeList the new files in the user directory than the code folder

Challenge: Finding Files

Goal:

    • Locate Sources.list File
    • Modify the file owner to yourself (Shiyanlou)
    • Modify the permissions to only be readable and writable

Experiment six, file packaging and decompression one, zip Compression Packager 1. Use the Zip Package folder

zip -r -q -o shiyanlou.zip /home/shiyanlou

    • -rIndicates that a recursive package contains all the contents of a subdirectory
    • -qExpressed as quiet mode, that is, does not output information to the screen
    • -oIndicates the output file, followed by packaging the output filename immediately thereafter

$du -h shiyanlou.zipView the size of a packaged file

$file shiyanlou.zip

2. Set the compression level to 9 and 1 (9 max, 1 min), repack

$zip -r -9 -q -o shiyanlou_9.zip /home/shiyanlou -x ~/*.zip

$zip -r -1 -q -o shiyanlou_1.zip /home/shiyanlou -x ~/*.zip

    • 1 means the fastest compression but the volume is large, 9 means the smallest volume but the longest time
    • -xTo exclude the zip file we created last time.
    • Note: Only absolute paths can be used here, otherwise it will not work

$du -h -d 0 *.zip ~ | sort

    • H:--human-readable
    • D:--max-depth

3. Create an encrypted ZIP package

$zip -r -e -o shiyanlou_encryption.zip /home/shiyanlouUse the-e parameter to create an encrypted compressed package

$zip -r -l -o shiyanlou.zip /home/shiyanlouPlus the-l parameter converts LF (line break in LUnix) to CR+LF (line wrapping in Windows)

Second, unzip the zip file using the unzip command

$unzip shiyanlou.zipExtract the Shiyanlou.zip to the current directory

$unzip -q shiyanlou.zip -d zipestUse Quiet mode to extract files to the specified directory

$unzip -l shiyanlou.zipDo not want to unzip only want to see the contents of the compressed package with the-l parameter

$unzip -O(大写)GBK 中文压缩文件.zipSpecifying the encoding type using the-o parameter

Iii. rar Packaging Compression command

$sudo apt-get update

$sudo apt-get install rar unrarInstalling RAR and Unrar tools

$rm *.rar

$rar a shiyanlou.rar .Create a compressed package or add a file to a compressed package from a specified file or directory

    • Use the A parameter to add a directory ~ to an archive, if the file does not exist it will be automatically created
    • RAR command Parameters No-, if added will error

$rar d shiyanlou.rar .zshrcTo delete a file from the specified compressed package file

$rar 1 shiyanlou.rarView unresolved files

$unrar x shiyanlou.rarFull path decompression

$mkdir tmp

$unrar e shiyanlou.rar tmp/Remove Path Decompression

Iv. Tar Packaging tools

$tar -cf shiyanlou.tar ~

    • -cRepresents creating a Tar package file
    • -fUsed to specify the file name to be created, immediately following the file name
    • -vVisual
    • -pPreserve absolute path characters

$mkdir tardir

$tar -xf shiyanlou.tar -c tardirUnpack a file (-X) to the existing directory of the specified path (-c)

$tar -tf shiyanlou.tarView only unresolved package file-T parameter

$tar -cphf etc.tar /etcKeep file attributes (-p) and follow links, backup links to source files instead of link itself (-h)

$tar -czf shiyanlou.tar.gz ~

Compressed file format and corresponding parameters:

    • *.tar.gzCorresponding-z

    • *.tar.xzCorresponding-J

    • *.tar.bz2Corresponding-j

Experiment VII, File system operation and Disk Management I. BASIC operations 1. Use the DF command to view the capacity of a disk

$df

$df -hEasier to read

2. Use the du command to view the directory's capacity

$du

$du -hEasier to read

$du -h -d 0 ~View information for Level 1 directories only

$du -h -d 1 ~View Level 2

    • -dparameter specifies the depth of the view directory
    • -aparameter displays the size of all files in the directory
    • -sShow totals only, list the last plus total values only
Second, create a virtual disk

I can't read it.

Experiment VIII, Linux Help command One, built-in commands and external commands
    • Built-in command: "Natural and innate skills" such as: History, CD, Exit
    • External command: "Acquired additional skills", e.g. LS, vi

Input$type xxx

If output:

xxx is a shell builtinDescription is built-in command

xxx is usr/sbin/xxxDescription is an external command

xxx is an alias for xx - xxxDescription is the name set for the command alias

Second, the use of the Help command 1.help command

$bash

$ help exitThe help command can only be used to display information about the built-in commands

If the external command is$ls --help

2.man command

$man ls

Man command has no distinction between built-in and external commands

Man Handbook: Press PgUp and PgDn up and down, press Q to exit the current page

LS(1)LS represents the manual name, (1) indicates that the manual is in the first chapter

3.info command

Regular bash will bring your own

info ls

Summarize

The study in the experimental building stimulated my interest in learning, especially the "relax" at the end of each experiment, which really surprised me and made me feel fulfilled. However, there are still many unresolved issues, such as:

    • Experiment two operation part toilet, figlet how to use?
    • Experiment three methods of modifying file permissions in the two plus minus assignment operation, what does the u/g/o follow?
      For example: "U-x" Why is "--w--wx-wx"?
    • Vim Find and Replace experiment, " u{n} Undo N Operations" How to use this command?
    • What is the role of vim in finding and replacing experiments ctrl+r ? "Already at newest" is displayed after pressing "Ctrl+r", but no other changes are seen.
    • Experiment seven creating a virtual disk is not read

I hope I can solve these problems in the future study.

20165306 preparatory work 3 Linux installation and learning

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.