Linux common skills collection

cattle blog

1. Delete a lot of files

To delete massive files under Linux, hundreds of thousands of files need to be deleted. This is the log written by the previous program, which grows rapidly and is useless. At this time, our commonly used deletion command rm -fr * is not easy to use, because it takes too long to wait. So some extraordinary measures have to be taken. We can use rsync's --delete-before parameter to quickly delete a large number of files. Proceed as follows:

  • Create an empty folder:
mkdir /tmp/test  
  • Delete the target directory with rsync:
rsync --delete-before -a -H -v --progress --stats /tmp/test/ log/

选项说明:
--delete-before 接收者在传输之前进行删除操作
--progress 在传输时显示传输过程
--a 归档模式,表示以递归方式传输文件,并保持所有文件属性
--H 保持硬连接的文件
--v 详细输出模式
--stats 给出某些文件的传输状态

In this way, the log directory we want to delete will be emptied, and the deletion speed will be very fast. rsync actually uses the replacement principle, and it also deletes hundreds of thousands of files in seconds.
Note: Before performing this operation, please confirm whether the files in the corresponding directory are useful! ! !

2. File sorting

  • Increment by file size
#ls-lR|grep^-|sort-nr-k5|more
  • Decrement by file size
#ls-l|grep^-|sort-nr-k5|more

3. Redirect

Input redirection refers to importing files into the command, while output redirection refers to writing the data information originally to be output to the screen into the specified file. In daily study and work, we use output redirection more frequently than input redirection, so we divide output redirection into two different technologies: standard output redirection and error output redirection, and clearing There are two modes of writing and appending:

20180226151961097213544.png

output:

Standard output && Error output Symbol: ">"
20180226151961114587261.png

Enter the symbol: "<"
20180226151961120739037.png

Effect:

20180226151961126618072.png

  • What if we want to write both correct and incorrect information at the same time?
    The answer is: &> (this command saves all correct and incorrect information)

4. Count the memory occupied by a service

  • one sentence script
ps aux |grep -v grep| grep progress_name |sort -nrk4 |awk '{print $4}'|awk '{sum+=$1} END {print "Sum = ", sum}'
  • Using the "/proc" file
#!/bin/bash
#统计进程所占内存
pid=`ps aux |grep -v grep |grep $1 |awk '{print $2}'`
sum=0
for i in $pid;do
  num=`grep Pss /proc/$i/smaps 2>/dev/null |awk '{total+=$2};END {print total}'`
  #echo "pid is $i"
  if [ ! -z $num ];then
    sum=$[sum+$num]
    #测试用
    #echo "num is $num"
    #echo "sum is $sum"
  fi
done
#单位转换——转化为GB(默认为kb)
tot=`echo "$sum" |awk '{printf ("%.2f\n",$1/1024/1024)}'`
echo "tot is $tot"
#注:注释掉的“echo”行为脚本调试时使用。  

Note: How to use this script, such as "sh sum.sh progress_name" (assuming the script name is sum.sh and process_name is the process name).
Division operation (retain decimals): http://www.mamicode.com/info-detail-1187091.html
Added:

5. List of basic skills of Anren Group collaboration

https://github.com/OpenMindClub/Share/wiki/IdxCooperateBasicSkill

6. Check the cron plan - chkcrontab

Used to check whether the task schedule file added to "/etc/cron.d/" is executed correctly.

[root@host3 ~]# pip install chkcrontab  

[root@host3 ~]# chkcrontab /etc/cron.d/sysstat 
Checking correctness of sysstat

Hard disk monitoring shell script

This script is written relying on the megacli tool provided by dell. It has been used for a period of time, but then I have used the hardware monitoring tool of falcon, including motherboards, fans, hard disks, raid cards, etc. It is more convenient than this.

#!/bin/sh
allhosts="host01 "
log_dir=/home/chuyun_sys/disklog/
log_name=_raid_disk_monitor
logtime=$(date +%Y%m%d --date='1 days ago')
fix=.log
for i in $allhosts;do
host=`ssh $i "hostname"`
echo  "Checking RAID status on $host" >> $log_dir$logtime$log_name$fix
echo -e "\033[31m $host \033[0m"
echo "$host" >>$log_dir$logtime$log_name$fix
RAID_Contrller=`ssh $i 'megacli -AdpAllInfo -aALL |grep "Product Name" | cut -d: -f2'`
echo "Controller : $RAID_Contrller" >> $log_dir$logtime$log_name$fix
Online_disk_num=`ssh $i 'megacli  -PDList -aALL | grep Online | wc -l'`
echo "Totall number of Physical disks online : $Online_disk_num" >> $log_dir$logtime$log_name$fix
Degrade_disk=`ssh $i 'megacli -AdpAllInfo -a0 |grep "Degrade"'`
echo  "$Degrade_disk"  >>$log_dir$logtime$log_name$fix
Failed_disk=`ssh $i 'megacli -AdpAllInfo -a0 |grep "Failed Disks"'`
echo "$Failed_disk " >>$log_dir$logtime$log_name$fix
#Failed_disk_num=`ssh $i 'echo $Failed_disk |cut -d " " -f4'`
done

awk match against a column

Usage scenario: When checking the file size, you may need to sort the output to make it clearer. If the file name does not involve G, M and other unit characters, you can directly use grep G after executing the du -h command to filter out files with a file size at G level. , but some self-generated file names are often irregular, which will contain G, M and other characters, so grep is not enough, so use awk to match the output for the specified column, as follows:

#只匹配文件大小在G级别的文件并针对第一列以数字大小顺序排序
du -ch |awk '$1~/G/' |sort -nk1 

20180423152447445668813.png

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324931621&siteId=291194637