编程知识 cdmana.com

Linux basic command learning

Command learning

Current directory

View the current working directory -pwd( Absolute path )
pwd The command displays the current working directory location of the user , The working directory is the default location for users to operate files or other subdirectories .
As shown in the figure :

Linux Basic command learning _ operating system

Switch the user's working directory (~ Represents the current user's home directory )

  1. cd Target location : Switch to the target location
  2. cd ~ or cd: If the target location is not specified , Switch to the host directory of the current user
  3. cd -: Switch to the last execution cd The directory before the command
  4. Absolute path : The path starting from the root directory , No matter which path you are in, you can accurately switch to the specified directory
  5. Relative paths : The path starting from the current directory

Relative path representation

           form                                     explain                           Example       
Use directory name or file name directly Represents a subdirectory in the current working directory 、 The location of the file cd groub2/
With a dot “.” start Indicates that the current working directory is used as the starting point cd ./grub.conf
With two dots “. .” start Represents the directory above the current directory ( Parent directory ) As a starting point . ./vmlinuz
With “~ user name ” Form begins Indicates starting from the specified user's host directory ~teacher
With “~” Form begins Indicates that starting from the current user's Host Directory ~

The list shows the contents of the catalog -ls

ls Common options for

  • -l
    In long format (Long) Show a list of files and directories , Including authority 、 size 、 Last update time and other details .ll Command and ls-l Function the same
  • -a
    Show all (All) Information about subdirectories and files , Include the name with a point number "." Hidden directories and hidden files at the beginning .
  • -A
    And -a The function of options is basically similar to , But there are two special hidden directories that won't be displayed , That is to say, the ".“ And... Representing the parent directory ". ."
  • -d
    Show directory (Directory) Its own attributes , Instead of showing the contents of the catalog .
  • -h
    To be more human (Human) Displays the size of a directory or file in the same way , The default size is in bytes (B) , Use -h Option will be displayed as KB、MB Equal unit . This option needs to be combined with -l Use options together .
  • -R
    By recursion (Recursive) Displays all the contents of the specified directory and its subdirectories
  • –color
    Color different files in character mode . The default is on .
    Usually , Dark blue means the directory ,, White means general document , Green means executable file , Yellow means device file , Red means compressed file , Sky blue indicates soft connection file
  • -i
    Showing files and directories inode Number

Linux Basic command learning _ operating system _02

Add : use ls -l Command to view a directory will get a 7 A list of fields

  • The first 1 Field : File property word
    The first character represents the type of file
    "-” Indicates that the file is a normal file
    "d” Indicates that the file is a directory
    “l” Indicates that the file is a soft link file . Be similar to windows Next shortcut
    The first 1 After the character 9 A letter indicates the permission bit of the file or directory :
    r The watch is read ( see 、 download )、w Said to write ( add to 、 change 、 Delete 、 Upload )、x Indicates execution ( Run the file 、 Toggle directory )
    The first three represent the file owner ( Belong to ) Authority , The middle three represent the permissions of the group to which the file belongs , The last three represent the permissions of other users .

  • The first 2 Field : Number of file hard links
    If a file is not a directory , This field indicates the number of hard links the file has
    If it's a directory , Is the first 2 Field represents the number of subdirectories in the directory .
    If you create an empty directory , The second field of this directory is 2, Indicates that there are two subdirectories under the directory .

Linux Basic command learning _linux_03

  • The first 3 Field : file \ Directory owner
  • The first 4 Field : file \ The group of the directory owner
  • The first 5 Field : The space occupied by the file ( Soft recognition is in bytes ) If it's a directory , Indicates the size of the directory , Not the total size of the directory and the files under it .Linux Everything in the system is documented , So a directory is also a kind of file .
  • The first 6 Field : file \ Last access or modification time of the directory
  • The first 7 Field : file name , If it's a symbolic link ( Soft link file ), Then there will be a “->” The arrow symbol , The next root is the file name it points to

wildcard
Combining wildcards
“?”: Represents any character , There is one character
“*”: Represents any number of characters , It can be 0 A or 1 One or more
“[]”: Indicates that any character in a character group can be matched [abc]、[a-f]

Linux Basic command learning _shell_04

Set alias -alias

  1. Simplify the common 、 A longer order
alias  Command alias =‘ command ’   ###“=” There must be no spaces on either side 

     
  • 1.
  1. Remove alias
unalias  Command alias 

     
  • 1.
 Example 
[root@localhost ~]# alias myls='ls -alh'

     
  • 1.
  • 2.

Linux Basic command learning _ operating system _05

Statistics directory and file space occupancy -du

  1. Count the specified directory ( Or document ) The amount of disk space occupied
 du   [ Options ]    [ File or directory …]

     
  • 1.
  1. Common options
  • -a : Include all files when calculating disk space usage , It's not just catalogues
  • -h : In a more humane way ( Default to KB Count , But it doesn't show units ) Show the statistics
  • -s : Only count the total size of the occupied space , Instead of counting every subdirectory 、 File size

Linux Basic command learning _shell_06

  • a key :–max-depth=n —— Count all less than or equal to n The total size of the directory of the layer
    In the production environment, the system disk is occupied. To find the cause of the exception, you need to find the directory layer by layer
    for example : According to the statistics /var/log The size of the space occupied by the directory
 ​du -h --max-depth=1/var/log/

     
  • 1.

Linux Basic command learning _ operating system _07

notes : Options -a and -s Do not use at the same time

Create a new directory mkdir

  1. Create a new empty directory
mkdir   [ Options ]    Directory location and name 

     
  • 1.
  • Add multiple directories at a time and directly add (mkdir abc 123)
  • -p: Create nested multiple directories at once

for example : Create a file in the current directory called public_html A subdirectory

 mkdir public_html

     
  • 1.

for example : Create directory /aa, stay /aa Create subdirectories under the directory bb, stay /aa/bb Create subdirectories under the directory cc

 mkdir -p aa/bb/cc

     
  • 1.

Linux Basic command learning _linux_08

Create an empty file touch

  1. Update the time stamp of the file
  2. Often used to create multiple new empty files
    touch file …
    for example :
    Create two empty files
touch aa bb

     
  • 1.

The file names are aa.txt and bb.txt:

touch {aa,bb}.txt

     
  • 1.

Create two different suffixes with the same file name

touch aa {.jpg,.png}

     
  • 1.

Linux Basic command learning _linux_09

Create link file In

  1. Create link file -In
    Create a linked file for a file or directory , Be similar to Windows System shortcuts
    Link file type
  • Soft link ( Also known as symbolic links ln -s)
    When establishing a soft connection, the source file needs to be represented by an absolute path . The source file location remains unchanged , Soft link file locations are useful wherever they are , Changing the file location relative to the path will invalidate
    example :ln -s Source file or directory … Linked file or target location
 ln -s /root/bb.txt   /opt/BB.txt

     
  • 1.
  • Hard links ( Only to understand )
    ln Source file or directory … Linked file or target location
    The source file is represented as an absolute path , Hard links are relative to creating an alias file , The source file is still useful after deletion

Linux Basic command learning _ operating system _10

Linux Basic command learning _linux_11

Copy files or directories cp

Files or directories that will need to be copied ( Source ) Rebuild a , And save it as a new file or directory

  1. cp [ Options ]… Source file or directory … Target file or directory …
  2. cp Common options
  • -f: Do not remind when overwriting a file or directory with the same name as the target , And direct forced replication
  • -i: Remind the user to confirm when overwriting a file or directory with the same name as the target
  • -p: Permission to save files when copying 、 Attributes such as owner and time stamp remain unchanged
  • -r: This option must be used when copying directories , Means to copy all files and subdirectories recursively
    notes : When copying multiple files or directories , The target location must be a directory , And the target directory must already exist .
cp letc/host.conf /etc/host.conf.bak
cp host.conf{ ,.bak}
mkdir yum_bak
cp -r /etc/ yum.repos.d /etc/ yum.conf yum_bak/

     
  • 1.
  • 2.
  • 3.
  • 4.

Delete files or directories rm

  1. rm [ Options ] Files or directories to delete …
  2. Common options
  • -f: Do not remind when deleting files or directories , And directly force deletion .
  • -i: Remind users to confirm when deleting files or directories .(y Said to delete ,n Means not to delete )
  • -r: This option must be used when deleting directories , Means to delete the entire directory tree recursively ( Use with caution ).
    notes : Do not directly delete existing directories or configuration files in the system , To avoid unexpected failures
 rm -rf /etc/yum.repos.d/*

     
  • 1.

When deleting files in the directory, it is recommended to switch to the directory first and then execute rm -rf command

 cd /etc/ yum.repos.d/
 rm -rf/*( Use caution )

     
  • 1.
  • 2.

Move files or directories mv

  1. Specify file or directory transfer location
  2. If the destination location is the same as the source location , It is equivalent to performing rename operation
mv     [ Options ]    … Source file or directory … Target file or directory 

     
  • 1.
 example :mv /aa /opt   ### Move to the root directory aa Directory to opt Under the table of contents 

     
  • 1.
  1. mv The command can only rename a single file , You can use rename Command to batch modify the file name
 example : rename jpg gif *.jpg
   renaem   Old name   New name   Target file 

     
  • 1.
  • 2.

Batch modify file name :rename

mv The command can only rename a single file , You can use rename Batch modify file name

rename    Old characters     New character     Target file 

     
  • 1.
 Example :rename jpg gif *.jpg   ### Put all the jpg The format file is changed to gif Format 

     
  • 1.

Find the directory where the command file is stored which

which    command | The program name    // By default, when the first target is found, it will not continue to find 
 or 
which   -a    command | The program name    // Find... In all search paths 

     
  • 1.
  • 2.
  • 3.
 Example :which ls
      which history

     
  • 1.
  • 2.

Use which When looking for internal commands , The corresponding program will not be found

Find a file or directory find

  1. Recursively , According to the name of the target 、 type 、 Size and other attributes for fine search
find   [ Find the range ]   [ Find the condition type ]

     
  • 1.
  1. Common types of search criteria
Find type keyword                       explain                      
Search by name -name Search by the name of the target file ( Name and “ ” Double quotes , Allow to use “*” And “?” wildcard
Check by file size -size Search based on the size of the target file , In general use “+”、“-” Number is set to be greater than or less than the specified size as the search condition , Common units of capacity include kB( Be careful k It's lowercase )、MB、GB
Search by file owner -user Search according to whether the file belongs to the target user
Find by file type -type Find according to the type of file , File types include ordinary files (f)、 Catalog (d)、 Block device file (b): A device that reads data in chunks ( Hard disk , Memory , CD drive ) Character device file (c) A device that reads data as a single character ( keyboard , mouse )
  1. Use find Command to implement multiple search conditions
  • Use logical operators between expressions
  1. “-a” And (and) ### It means that you need to meet
  2. “-o” Express or ( or) ### Indicates that only one of the
 Example :find /boot -size +1024k -a -name "vmlinuz"
     find /boot -size +1024k -o -name "vmlinuz"

     
  • 1.
  • 2.

The experiment is shown in the figure below :

Linux Basic command learning _linux_12

Linux Basic command learning _shell_13

  1. find And exec usage
  • -exec Parameters are followed by Linux command , It's a semicolon ";" For the end sign , Because semicolons have different meanings in different systems , So add the backslash escape character before the semicolon “\”.
    {} For the front find Find out the file name .
  • find The command matches all the normal files in the current directory , And in -exec Use... In the options ls-l The command lists them
find /boot -name "vm*" -a -type f -exec ls -l {} \;

     
  • 1.

Linux Basic command learning _linux_14

Priority of executing commands

  • First priority
    Command to specify the path . Absolute path /root/pwd.sh Or relative path ./pwd.sh
  • Second priority
    Alias specified command alias pwd=/root/pwd.sh
  • Third priority
    Internal orders
  • Fourth priority
    hash command .
    Linux There will be one under the system hash surface , When you first turned it on, this hash Table is empty , Every time you execute an order , hash The table records the path of the command , It's like caching . For the first time shell By default, the interpreter will start from PATH Path to find the path of the command , The second time you use this command ,shell The interpreter first looks at hash surface , Without that order, I would go PATH Find... Under the path .hash Tables increase the speed of command calls .
  • Fifth priority
    adopt PATH Defined find order find
  • If none of the above can be found , Will report “ Command not found …” Error of

版权声明
本文为[Maybe no more good night]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/11/20211109092935738b.html

Scroll to Top