Some simple commands for Linux

Source: Internet
Author: User
Tags first string modifier line editor

This is just a list of the more frequently used and can be viewed in more detail through the man command or the command Help.

FILE-related

1: "ls command" ls [option] ... [File] ...

    • -A all lists all files including hidden files [eg ls-a/home]
    • -l list Detailed file information can be abbreviated to LL filename [eg:ls-l/home or Ll/home]
    • -h–human-readable lists the size of the file in bytes
    • -R recursively displays all files in the directory
    • -D displays only the following in this file

More detailed commands can be viewed through the man LS

2: "CD command" CD [option]. [Dir]

eg Cd/home

3:[mkdir command] mkdir [OPTION] ... DIRECTORY ...

    • -P is created when the file is created, when the upper directory of the target directory does not exist, and is not treated as error if the directory already exists; eg:mkdir-p/a/b/c
    • -v–verbose Print Create catalog Yes, details.
    • -m set permissions for eg mkdir-m 700-p-v/a/b/c to set directory permissions at the same time as the directory is created 700

4: "rmdir" Delete empty directory
---P rmdir-p/a/b/c equivalent to rmdir a/b/c A/b if a directory is not empty, then command execution will fail, note hidden files? This command will first be deleted from the deepest level of the directory.

5: "Tree" view directory tree

 这个命令不是系统自带的需要安装 yum install tree 可以通过 tree /home 以树的形式列出 /home目录下的所有文件

6: Creation and deletion of "touch" files

 *touch filename  

7: "Stat" to view file information

8: "RM" Delete file

    • -I: Confirm before deleting files
    • -F do not confirm before deleting file
    • -R Recursive Delete

EG:RM-RF A.txt

Note: In the actual work must be noted that the deletion of files, as far as possible to use this command, the general deletion before the important files first backup

9: "CP" copy copy

    • -I if the file exists, it will be confirmed before overwriting
    • -A archive copy, regular term backup, keep the original properties of the file

You can copy multiple files to a directory at the same time
eg:cp file1 file2 file/tmp
EG2 Cp/etc/{passwd,inittab}/tmp copy the passwd file and initable file below the ETC folder to the/tmp directory

10 "MV" Move move file MV Source file destination Path

eg mv/etc/passwd/tmp move the/etc/passwd file to the/tmp directory
eg2:mv/etc/passed/tmp/pd Move the/etc/passwd file to the/tmp directory and rename it to PD

11 "Cat" Connection display file

*eg cat/etc/passwd*

12 "more | Less "flip screen

13 "Tail | Head "displays the tail or head of the file

  *head -n 5 /etc/passwd* 显示passwd文件的前五行  *tail -5 /etc/passwd* 显示passwd文件的后五行  *tail -f xx.log* 一般用来查看log文件,显示的是文件尾部,不退出命令,只要有新的日志内容,就会显示出来

14 "File Redirection command"

The first thing to understand is three concepts.
The input of standard input stdin keyboard and mouse is called standard input.
Standard output stdout
Standard error Output stderr

Pipe: The standard output of the previous command as standard input for the next command

    • > output redirection, redirect standard input
    • >> Chasing heavier orientation
    • Cat < output redirection
      eg cat a.txt > b.txt redirects content in A.txt to B.txt, and is overwritten if content is originally in B.txt
      eg2:cat a.txt >> b.txt Append to the end of B.txt original content
      eg3:cat > A.txt < b.txt First read from B.txt, then redirect to A.txt
Text Processing

15 "cut" split string

    • -D: Specifies the field separator, which is the default space
    • -F: Specify the fields to display
    • -F 1,3 Show 1 and 3 fields after splitting
    • -F 1-3 shows the 1 to 3 fields after splitting
      eg cut-d:-F 1,7/etc/passwd
[[email protected] test]# cut -d: -f 1,3 /etc/passwdroot:0bin:1daemon:2......

16 "Sort" sort

    • -N Sort by number default is ASCII code
    • -T Field delimiter
    • -R Descending row
    • -U Go Heavy
      • What keyword to sort k

eg:sort-t ":"-k4-n-r/etc/passwd as: delimiter, fourth as a sort of keyword, reversed by number

[Root@node01Test# sort-t:-k4-nr/etc/passwdTtuser5:x:520:520::/home/ttuser5:/bin/bashTtuser4:x:519:519::/home/ttuser4:/bin/bashTtuser3:x:518:518::/home/ttuser3:/bin/bashTtuser2:x:517:517::/home/ttuser2:/bin/bash......

eg2:cut-d:-f7/etc/passwd | sort-u

[[email protected] test]# cut -d: -f7 /etc/passwd | sort -u/bin/bash/bin/nologin/bin/sync......

17 "WC" Text statistics

    • -L: Count rows
    • -W: Count the number of words
    • -C: Statistics of bytes
    • -L: Print the longest line length

Eg1:wc-l/etc/passwd

[root@node01 test]# wc -l /etc/passwd44 /etc/passwd

eg2:cut-d:-f7 passwd | Sort-u | Wc-l

[root@node01 test]# cut -d: -f7 /etc/passwd | sort -u | wc -l6

18 "sed" line editor, not editing source files by default

    • -N does not display to the screen, also does not change the source file, basically does not have the use
    • -e executes multiple text simultaneously
    • -P Display Qualifying rows
    • -I modify source file
    • s/pattern/string/modifier: Find and Replace, default replaces only the first string in each line that is matched by the pattern, modifier g: Global substitution, I: ignore character case
    • -R using extended regular expressions

Regular expressions are used here, and if you are not familiar with regular expressions, teach yourself regular expressions
eg: remove whitespace from the beginning of the/etc/inittab file
Sed-r ' [e-mail protected]^[[:space:]][email protected]@g '/etc/inittab:-R indicates that s in quotation marks using an extension means find and replace, and the @ character can be customized, Indicates that the following is followed by a regular expression: ^[[:space:]]+ is the use of one or more spaces beginning with the @@ 之间 the character substitution, here @@ 之间 is the same as deleting the beginning of the blank character; the last G represents a global substitution
EG2: Replace the number in the "Id:3:initdefault:" in the/etc/inittab file with 5, in order to see that the TAIL-1 is used to show the last line

[[email protected] test]# sed -r ‘[email protected](id:)[0-9](:initdefault:)@\15\[email protected]‘ /etc/inittab | tail -1id:5:initdefault:

EG3: Delete empty lines in C.txt

[[email protected] test]# cat c.txt  PASSWD:  only one user name is specified. PASSWD:  Only one user name is specified. PASSWD:  Only one user name is specified. PASSWD:  Only one user name can be specified.//the above file has two blank lines and blank lines that are blank lines that are lines that have spaces, while blank lines are rows that are generated directly by the Enter//file  blank line, line consisting of spaces, 4 , 7  line is the empty row [[email  Protected] Test]# sed-r '/^$/d ' c.txt  PASSWD:  only one user name is specified. PASSWD:  Only one user name is specified. PASSWD:  Only one user name is specified. PASSWD:  Only one user name is specified. 

EG4: Delete blank lines in C.txt "Note is not a blank line"

[[email protected] test]# cat c.txt  PASSWD:  only one user name is specified. PASSWD:  Only one user name is specified. PASSWD:  Only one user name is specified. PASSWD:  Only one user name is specified. [[email protected] test] # sed-r '/^[[:space:]]+$/d ' c.txt  PASSWD:  Only one user name is specified. PASSWD:  Only one user name is specified. PASSWD:  Only one user name is specified. PASSWD:  Only one user name is specified. PASSWD:  Only one user name can be specified. The result of the execution is the deletion of the third row of red and white lines while 5 , 7  behavior is blank, and the  is not deleted.

EG5: Delete empty lines in c.txt, and Blank lines

[[email protected] test]# cat c.txt passwd: Only one user name may be specified.passwd: Only one user name may be specified.passwd: Only one user name may be specified.passwd: Only one user name may be specified.[[email protected] test]# sed -r ‘/^[[:space:]]*$/d‘ c.txtpasswd: Only one user name may be specified.passwd: Only one user name may be specified.passwd: Only one user name may be specified.passwd: Only one user name may be specified.

19 "awk" is a huge text processing tool, self-made system that can be programmed

    • -F Format Separator
    • Built-in variables
      • -NF the number of fields in the current record
      • NR number of records read
      • OFS output Field delimiter
        Awk-f "Format Separator" ' {Specific action performed} '

Eg: statistics on the number of accounts in/etc/passwd

END {pring "user count is ", count}‘ /etc/passwd

EG2: Show/ETC/PASSWD's account

[[email protected] test]# awk -F: ‘BEGIN {count=0;OFS="XXX"}{name[count]= $1;count++};END {for (i=0;i< NR;i++)print i,name[i]}‘ /etc/passwd;i++)print i,name[i]}‘ /etc/passwd0XXXroot1XXXbin2XXXdaemon......

EG3: Using If Else

# awk ' BEGIN {count=0; fs=":"} {if(nr==6) Ofs=ofs"|";ElseOfs=ofs"^"; Name[count] = $1;p rint count,name[count++]} '/etc/passwd# awk ' BEGIN {count=0; fs=":"} {if(nr%2==0) Ofs=ofs"|";ElseOfs=ofs"^"; Name[count] = $1;p rint count,name[count++]} '/etc/passwd# awk ' BEGIN {count=0; fs=":"} {if(nr%2) Ofs=ofs"|";ElseOfs=ofs"^"; Name[count] = $1;p rint count,name[count++]} '/etc/passwd

other commands in Linux
View process Commands
Ps-aux
Netstat-ntpl
Ss-tanlp
Yum install lrzsz a command to support file uploads and downloads between Fast windons and Linux hosts

Some simple 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.