编程知识 cdmana.com

[operation and maintenance] Linux (2)

1.Linux terminal Termial common sense

① Command line prompt

  • l  Alex Represents the name of the current user , stay Linux There can be multiple users in .
  • l  @ In front is the user name , After that is the domain .
  • l  localhost It's a computer. / Host name . That's the host name (hostname).
  • l  ~ The name of the current directory , It will change as users enter different directories ,~ Home directory for the current user .
  • l  $ Indicates the permissions you have .

Linux There are two types of characters for permission in :

1. $: For ordinary users , There are limits of authority

2. #: Represents a superuser ,root user

② Query current user and host name

whoami: Query current user

hostname: Query the current host name

③Linux Command line arguments

Linux A basic command in can have one or more parameters attached to it , Parameters can be in the form of long parameters , It can also be in the form of short parameters .

Short parameters ( A letter )

  •   The most common form of parameter is a dash followed by a letter :command –p
  •   Add several short parameters at a time , Can be separated by spaces :command –p -a -T -c
  •   Multiple short parameters can also be combined :command -paTc
  •   There is a difference between case and letter , uppercase T And lowercase t Usually it means something different

Long parameters ( Multiple letters )

  •   Long parameters start with two dashes . Format :command --parameter
  •   Multiple long parameters cannot be written together like short parameters , It can only be separated by spaces :command --parameter1 --parameter2
  •   You can use a combination of short and long parameters , Format :command -paTc --parameter1 --parameter2
  •   There are some parameters that need to be assigned . Short parameters and long parameters are assigned differently . Short parameter assignment :command -p 10; Long parameter assignment :command --parameter=10

④ Practical shortcut keys

  •  Ctrl + R: Used to find used commands
  •  Ctrl + L: Used to clean up the contents of the terminal , Clear the screen . Same as clear command .
  •  Ctrl + D: Pass it to the terminal EOF(End Of File, End of file )
  •  Ctrl + A: The cursor jumps to the beginning of a line of commands .Home Keys have the same effect .
  •  Ctrl + E: The cursor jumps to the end of a line of commands .End Keys have the same effect .
  •  Ctrl + U: Delete all command characters to the left of the cursor
  •  Ctrl + K: Delete all command characters to the right of the cursor
  •  Ctrl + W: Delete the one to the left of the cursor “ word
  •   Browse text or display the command line interface : Space bar — The next page   Enter key — The next line  q key — Exit list

⑤ Three ways to switch home directory

  •  cd/home/Alex  Alex Can be replaced with your user name
  •  cd ~
  •  cd   Without any parameters

2.Linux Common commands in

2.1. date command

  •  date yes “ date ” It means , Used to display the current time .

2.2. ls command

  •  ls yes list Abbreviation .List yes “ list 、 List ” It means .
  •  ls The command is used to list the directory of files in the current directory .

Common parameter collocation :

  •  ls -a /ls --all: List all files in the directory , Include with . Start with hidden files
  •  ls -A: List all files in the directory , exclude . The current directory and .. Upper level directory
  •  ls -l: List a detailed list of files and directories
  •  ls -h: With Ko,Mo,Go Shows the file size in the form of
  •  ls -t: Sort a file or directory in descending order of last modified time

2.3. pwd command

  •  pwd Is the path to display the current directory ,Print Working Directory(“ Print current working directory ”)
  •   Use which pwd, Can be displayed pwd This executable is located in /usr/bin Directory

2.4.cd command

  •  cd It's English change directory Abbreviation , Express “ Toggle directory ”
  •  cd A table of contents followed by a table of contents , No need to add .
  •  cd .. Go back to the next level

2.5.du command

  •   Display the file size contained in the directory , Let us know how much space the files and directories take up
  •   Compared with the ls – l command ,du The command will drill down through the subdirectories of each directory , Count the size of all files

Common parameter collocation :

  •  du -h: With Ko,Mo,Go Shows the file size in the form of
  •  du -a: By default ,du The command only shows the size of the directory , If you add -a Parameters , The size of the directory and file will be displayed
  •  du -s: Show only total size , In general, and h Continuous use ,du -sh

2.6.cat command

  •  cat yes concatenate Abbreviation , Express “ Connect / Series connection ”
  •   Used to display all the contents of the file at one time , usage : Add the file path you want to display after the command
  •   Suitable for small files

Common parameter collocation :

  •  cat -n file name Display the current file name at one time and identify it with line number
  •  cat -n file name 1 file name 2 Put the filename 1、 file name 2 Merge to display line number output

2.7.less command

  •  less The command is used to paginate the contents of the file , One page at a time , This is the cat The biggest difference between commands
  •   Usage and cat command , Also add the file path after the command
  •  less Command to read the contents of the file , The size of the current screen

Common shortcut keys for reading content :

  •   Space bar : The file content reads the line number of the next terminal screen , It's like going forward one screen ( page ), And the keyboard PageDown( The next page ) The effect is the same
  •   Enter key : The contents of the file are read on the next line , It's equivalent to moving forward , And the keyboard ↓ The effect is the same
  •  d key : Half a page ahead ( Half a screen )
  •  b key : One page back , And on the keyboard PageUp( The previous page ) The effect is the same
  •  y key : Step back a line , The same as the up key on the keyboard
  •  u key : Back half a page ( Half a screen )
  •  q key : Stop reading files , End less command

2.8.head/tail command

  •  head The command is used to display the beginning of the file , Default display file header 10 That's ok

Common parameters :

  •  head -n: Specify the number of lines to display , Show n That's ok
  •  tail The command is used to show the end of the file , The end of the file is displayed by default 10 That's ok

Common parameter collocation :

  •  tail -n: Specify the number of lines to display , Show n That's ok
  •  tail -f: Real time tracking of file updates , have access to Ctrl + C To terminate ; Default tail -f Every time 1 Check the file for new content , You can specify the number of seconds that interval checks , use -s Parameters :tail -f -s 4 XXX

2.9.touch command

  •   Used to create a blank file
  •   If touch The file name followed by the command is a nonexistent file , It will create a new one

2.10.mkdir command

  •   Used to create a directory

Common parameter collocation :

  •  mkdir -p: Recursively create directory structure ,mkdir -p one/two/three

2.11.cp command

  •   Used to copy files or directories
  •   Usage mode :

For copying files

  • Copy files created before :copy new_file new_file_copy, The first new_file Is an existing file , the second new_file_copy It's a file that needs to be created
  • Copy files to other directories ( The file name does not change ):cp new_file Directory name /, take new_file Copy the file to the directory , And the file name doesn't change
  • Copy files to other directories ( File name change ):cp new_file Directory name /new_file_copy, take new_file Copy the file to the directory , And rename to new_file_copy

For copying directories

  • cp -r/cp -R: replica catalog , Everything in the catalog ( Subdirectories and files ) Will be copied
  • cp *.txt folder: Put all the... In the current directory txt File copy to folder Directory
  • cp ha* folder: Put the current directory as ha All the files are copied to the beginning folder Directory

2.12.mv command

  •  mv It's English move Abbreviation , Express “ Move ”.
  •  mv Command has two functions : Moving files ( Or directory )、 Rename file ( Or directory )
  •  mv And cp Command usage is similar to , But not like mv Command to copy the current file . and mv The command is simply moving , They don't make copies
  •   Usage mode

For moving files / Catalog

  • mv new_file one: take new_file Move this file to one Catalog
  • mv folder one: take folder Catalog ( Including subdirectories and files ) Move to one Catalog
  • mv The command also supports wildcard operations

Used to rename files

  • mv new_file renamed_file: take new_file Rename it to renamed_file

2.13.rm command

  •  rm The command is used to delete files and directories
  •  rm new_file_copy: Delete the file name in the current directory new_file_copy This document of
  •   You can delete multiple files at the same time , Just space each file , Such as :rm file1 file2 file3

Common parameter collocation :

  •  rm -i: Confirm to the user whether to delete , So for every file to be deleted , The terminal will ask if we want to delete it
  •  rm -f: Use with caution , You don't ask if you want to delete , Mandatory deletion
  •  rm -r: Recursively delete files , Contains subdirectories and all files in the directory
  •  rm -rf/rm -rf/*: Extremely dangerous , When using Administrators root The user goes to execute this command

2.14.sudo command

  •  sudo command : For the time being root user , have access to sudo For the time being, this order becomes root, It can be executed with normal user rights root User only commands
  •  sudo su command : Has always been root user

2.15.locate command

  •  locate The command is used to quickly find , This command is used to locate the file you want to find , And it's a quick order
  •   usage : The name of the file you want to search for , You can also use regular expressions
  •  locate The function of the command is to search all files and directories containing keywords
  •  locate The command does not look up your actual entire hard disk , It's looking up records in a file database , For newly created files , Not included in the file database ,locate Command cannot find its index , No results are returned
  •   If you want to force tracking to find the most recently created file , Need to use :sudo updatedb Update the database

2.16.find command

  •  find Command is a powerful tool for finding files , It allows us to do subsequent operations on every file we find
  •   And locate Different commands ,find The command does not look for records of files in the file database , It's going to traverse the files on your actual hard disk
  •  find Command usage :find 《 where 》 《 What 》 《 What do you do 》,《 What 》 Must be specified , That's what we're looking for

Parameters, :

  •  《 where 》: Specifies in which directory to look for , All subdirectories of this directory will also be looked up ,find Command can limit the search directory . Default if not given 《 where 》 Parameters , It will be found in the current directory and its subdirectories by default
  •  《 What 》: Find the file , According to the file name 、 size 、 Recent visit time to find
  •  《 What do you do 》: Yes find Find the file to do a certain operation . By default , If you don't specify this parameter ,find The command displays only the files found

Common parameter collocation :

Find by filename

  •  find -name “new_file”: Use -name Parameter specifies the file name , yes new_file
  •  find /var/log -name “syslog”: among /var/log It's the file path , It means that /var/log In the directory syslog file

Find by file size

  •  find /var -size + 10M: Use -size Parameter specifies the file size , The one that follows +10M Greater than 10M byte ; If you use ”-” Say less than ; If there's no plus or minus sign , To find a file whose size is equal to the specified value
  •  find -name “*.txt” -atime -7: Search according to the latest access time of the file , Use -atime Parameters ;-atime Parameters are followed by -7 Express 7 Within days , The minus sign means less than

Find by file type

  •  -type Parameter to specify the type of file to look for , among -type d Indicates to look up only directory types ,d yes directory The first letter of , Express “ Catalog ”;-type f Indicates that only file types are found ,f yes file The first letter of , Express “ file ”; If not used -type Parameter specified type ,find The default command is to find directories and files
  •  -printf Parameter is used to format and print the found content , such as :find -name “.*txt” -printf “%p - %u\n”, among %p File name ,- It's a short horizontal ,%u It's the owner of the file
  •  -delete Parameter is used to delete the found file , such as find -name “*.jpg” -delete, Will delete the current directory and all its subdirectories with .jpg A file with a suffix , There will be no confirmation
  •  -exec Parameter can be followed by a command , Operate on each file found

版权声明
本文为[If you are nameless, you can concentrate on practicing sword]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201224124746845P.html

Scroll to Top