Common linux commands

Source: Internet
Author: User
Tags timestamp to date

1. Delete 0-byte files
Find-type f-size 0-exec rm-rf {}\;

2. View Processes
Sort by memory from large to small
Ps-e-o "% C: % p: % z: % a" | sort-k5-nr

3. sort by cpu utilization from large to small
Ps-e-o "% C: % p: % z: % a" | sort-nr

4. Print the URL in the cache
Grep-r-a jpg/data/cache/* | strings | grep "http:" | awk-F 'HTTP: ''{print" http: "$2 ;}'

5. view the number of concurrent http requests and their TCP connection status:
Netstat-n | awk '/^ tcp/{++ S [$ NF]} END {for (a in S) print a, S [a]}'


6. sed-I '/Root/s/no/yes/'/etc/ssh/sshd_config sed: In this text, the Root row matches the Root row and replaces no with yes.


. How to kill the mysql process:
Ps aux | grep mysql | grep-v grep | awk '{print $2}' | xargs kill-9 (learn about the usage of awk)

Killall-TERM mysqld

Kill-9 'cat/usr/local/apache2/logs/httpd. Pi'

8. Run the following three services:
Ls/etc/rc3.d/S * | cut-c 15-(learn the purpose of cut and intercept data)

9. How to display multiple pieces of information in a SHELL, using EOF
Cat <EOF
+ -------------------------------------------------------------- +
| === Welcome to Tunoff services ===|
+ -------------------------------------------------------------- +
EOF

10. Clever Use of for (for example, creating a soft link for mysql)
Cd/usr/local/mysql/bin
For I in *
Do ln/usr/local/mysql/bin/$ I/usr/bin/$ I
Done

11. IP address retrieval:
Ifconfig eth0 | grep "inet addr:" | awk '{print $2}' | cut-c 6-or

Ifconfig | grep 'inet addr: '| grep-V' 127. 0.0.1' | cut-d:-f2 | awk '{print $1 }'

12. Memory size:
Free-m | grep "Mem" | awk '{print $2 }'

13.
Netstat-an-t | grep ": 80" | grep ESTABLISHED | awk '{printf "% s \ n", $5, $6}' | sort

14. view the number of concurrent Apache requests and their TCP connection status:
Netstat-n | awk '/^ tcp/{++ S [$ NF]} END {for (a in S) print a, S [a]}'

15. my colleague wants to count the size of all jpg files under the server and write a shell to count them. it was originally implemented using xargs, but when processing part of it at a time, there are multiple sums ...., the following command can solve the problem.
Find/-name *. jpg-exec wc-c {}\; | awk '{print $1}' | awk '{a + = $1} END {print }'

The more CPUs (multi-core CPUs, cat/proc/cpuinfo | grep-c processor), the lower the system load, the more requests that can be processed per second.

Bytes ---------------------------------------------------------------------------------------
16 CPU load # cat/proc/loadavg
Check whether the first three output values exceed 4 times the system logic CPU.

18 CPU load # mpstat 1 1
Check whether % idle is too low (for example, less than 5%)

19. memory space # free
You can also use # cat/proc/meminfo to check whether the free value is too low.

20 swap space # free
Check whether the value of swap used is too high. If the value of swap used is too high, further check whether swap actions are frequent:
# Vmstat 1 5
Check whether the si and so values are large.

21. Disk Space # df-h
Check whether partition usage (Use %) is too high (for example, more than 90%). If you find that a partition space is close to exhausted, you can enter the mount point of the partition, run the following command to find the most occupied files or directories:
# Du-cks * | sort-rn | head-n 10

22 disk I/O load # iostat-x 1 2
Check if I/O usage (% util) exceeds 100%

23 network load # sar-n DEV
Check whether the network traffic (rxbyt/s, txbyt/s) is too high.

24 network errors # netstat-I
Run the following command to check whether a network error exists (drop fifo colls carrier): # cat/proc/net/dev

25 network connection count # netstat-an | grep-E "^ (tcp)" | cut-c 68-| sort | uniq-c | sort-n

26 processes # ps aux | wc-l
Check whether the number of processes is normal (for example, more than 250)

27. Number of processes that can be run # vmwtat 1 5
The column shows the number of processes that can be run. Check whether the number exceeds 4 times the system logic CPU.

28 process # top-id 1
Check for abnormal Processes

29 check whether the network status can be properly connected, such as DNS and gateway.

30 users # who | wc-l
Run the following command to check whether there are too many login users (for example, more than 50): # uptime

31 system logs # cat/var/log/rflogview/* errors
You can also search for some exception keywords to check whether there are any error records, for example:
# Grep-I error/var/log/messages
# Grep-I fail/var/log/messages

32 core logs # dmesg
Check for abnormal error records

33 system time # date
Check whether the system time is correct

34. Number of opened files # lsof | wc-l
Check whether there are too many open files

35 logs # logwatch-print configuration/etc/log. d/logwatch. conf: Set Mailto to your own email address and start the mail service (sendmail or postfix) so that you can receive the log report every day.
By default, logwatch only reports logs of yesterday. You can use # logwatch-print-range all to obtain all log analysis results.
You can use # logwatch-print-detail high to obtain more specific log analysis results (not just error logs ).

36. Kill the process related to port 80
Lsof-I: 80 | grep-v "PID" | awk '{print "kill-9", $2}' | sh

37. Clear dead processes.
Ps-eal | awk '{if ($2 = "Z") {print $4}' | kill-9

38. tcpdump: capture packets to prevent attacks on port 80.
# Tcpdump-c 10000-I eth0-n dst port 80>/root/pkts

39. Check the number of IP duplicates and sort them in ascending order. Note that there are two spaces in the middle of "-t \ + 0 ".
# Less pkts | awk {'printf $3 "\ n" '} | cut-d. -f 1-4 | sort | uniq-c | awk {'printf $1 "" $2 "\ n" '} | sort-n-t \ + 0

40. view the number of active php-cgi Processes
Netstat-anp | grep php-cgi | grep ^ tcp | wc-l
Chkconfig -- list | awk '{if ($5 = "3: on") print $1 }'

 

Allow non-root users to use "sudo"

Log on to the system as a root user and execute "Login do". Add a new rule as follows (remember that the entered password is the current user password, not the root password)
# Run The sudo command without a password
Hadoop ALL = (ALL) NOPASSWD: ALL

Date

Timestamp to date
Date-d '2017-01-01 UTC 1970 seconds '+ "% Y-% m-% d % T"

# Display current date
Echo 'date-d now + % Y % m % d'

# Display the date of yesterday
Echo 'date-d yesterday + % Y % m % d'

# Addition/subtraction time
Date + % Y % m % d -- date = "-1 day"
Date + % Y % m % d % H -- date = "-1 hour"

View what server curl-s -- head "http://www.baidu.com"

View redhat version cat/etc/redhat-release # Here I show "CentOS release 5.5 (Final)". view the maximum number of files that can be opened in linux and write "ulimit-"
# Follow One of the rows on the screen, and you can see that the maximum number of opened files is 1024.
Open File (-n) 1024

Set the number of opened files:

Echo-ne"
* Soft nofile 65536
* Hard nofile 65536
">>/Etc/security/limits. conf

After launching the terminal, you can see that the modification has taken effect after executing ulimit-.

Zip compressed file zip {dist} {src}
Zip-P {password} {dist} {src}

Only allow root remote login to cd/etc
Touch nologin

Lock the user and check whether the machine is 64-bit or 32-bit uname-m # The following message is displayed: "i686" and "x86_64" cannot be remotely logged on (root only) passwd-l hadoop

Unlock user (root only) passwd-u hadoop

Listing the rpm Installer's file path rpm-ql hadoop-0.20

Check that the system has installed the software rpm-qa | grep hadoop

View the total number of cpu cores of the current server. more/proc/cpuinfo | grep 'processor '| wc-l

# View the number of cores for each CPU
More/proc/cpuinfo | grep 'cpu cores'

# Calculating the current service cpu count
Total number of cores/number of cores per CPU

# Viewing the CPU count
Getconf LONG_BIT

View the installation path of the program whereis java

View the path of the execution program which java

Java code

  1. # Modifying dns ip addresses
  2. Vi/etc/resolv. conf

 

Java code
  1. # View memory
  2. Free

# Check whether the CPU supports the 64-bit operating system. If the result is greater than 0, the 64-Bit mode is supported)
Cat/proc/cpuinfo | grep flags | grep 'lm '| wc-l

# View the content of a line
Sed-n' 100 {p; q} 'filename

# View the user group
Grep "username"/etc/group

# Viewing rpm package files
Rpm2cpio hadoop. rpm | cpio-idmv

# Add a path to the runtime database, with a path in one row
Vi/etc/ld. so. conf
/Usr/local/lib/
(Or use environment variables without the root permission)
Export LD_LIBRARY_PATH =/xxxxx: $ LD_LIBRARY_PATH

# Rename <oldname> <newname> <*. files>
# Change all html extensions to htm extensions
Rename. html. htm *. html
Reference: http://www.cyberciti.biz/tips/renaming-multiple-files-at-a-shell-prompt.html
# You can use music videos to modify the extension.
Mv goodYear. {htm, html}



Sort command #-u remove duplicate lines

#-R descending order (Ascending by default)

#-O output the sorted data. The biggest difference between the data output and the redirection '>' is that the data is output to the original file.
Sort seq.txt-o> seq.txt

#-N sort by value

#-K indicates sorting by a column
Sort num.txt-k2 # sort by the second column (the default Delimiter is space and tab)

#-T specifies the column separator. Only one character is supported.
#-M Merge Sorting

Linux Command Line Parameter reference run command:
./Test. sh 111 222 333 444

Echo running file name: $0
#./Test. sh

Echo first parameter: $1
#111

Echo parameter count: $ #
#4

Last echo: $ _
#444

All echo parameters, which are considered to be strings: $ @
#111 222 333 444

All echo parameters are considered as a string: $ *
#111 222 333 444

Echo program pid: $

Echo exit code: $?

Less command tip1: Press 50 p to enter the location of the 50% file. decimal places are supported, such as 95.5 p.

Write the script to change the password # method 1
Echo 123123 | passwd -- stdin abc
# Method 2
# Echo "root :! @ # $ QW12qw "| chpasswd
# Pwdadm-c root

Http://www.aixchina.net/club/thread-69699-1-1.htmlone-line command creates a user and sets the password useradd-p 'openssl passwd <pwd> '-g <group_name> <username>

Create a user and disable remote logon to useradd-s/sbin/nologin ak47

Query the crontab running Record cat/var/log/cron | grep <username>

Modify the linux user directory usermod-d/home/hdfs-U hdfs disk speed test hdparm-t/dev/sda1

Quick file search (by file name) # The locate command is responsible for searching, and the updatedb command generates a file index (daily)
Locate * qq *. txt

Chkconfig writes chkconfig -- list | more # list all system services

Chkconfig <service_name> off | on # disable and enable system services


The first SSH login does not prompt to configure StrictHostKeyChecking no in the ssh conf file


Top c # Show the complete program path
M # sort by memory
P # sort by CPU

Unzip the bz2 file bunzip2 yy.bz2

View the number of repetitions (shell-implemented group by + count) # Before uniq, you must first sort and uniq parameter-c to print the number of group repetitions.
Cat xx. TXT | sort | uniq-c

Tar command using tar-zcf my. tgz my # compress and Package
Tar-cf my. tgz my # Do not compress and Package


Tar-zxf my. tgz my # decompress the package
Tar-xf my. tgz my # uncompress the package

Tar-tf # view the tar Package Structure find #-o: or condition
# Search for all .htmand .html files in the current directory.
Find. \ (-name "*. htm"-o-name "*. html "\)

#-A: and Condition

Grep writes # search for lines in text that contain boys and girls
Cat my.txt | grep-E 'Boy | gir'

# Find the file name based on the text content, and "-H" is used to output the file name
Find.-name '* city *'-ls-exec zgrep 'null' {}-H \;

Modify hostname Method 1: hostname <new hostname>
Method 2: vi/etc/sysconfig/network

View the number of opened files lsof | wc-l
Lsof-p pid | wc-l

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.