- The topics in this article are all from the Internet
2020-10-29
- Please follow this date format (xxxx-xx-xx) to generate a file every day, for example, the file generated today is 2020-10-29.log, and write the disk usage to this file, (don’t consider cron , Just write the script)!
#!/bin/bash
file_name=`date -d "today" "+%Y-%m-%d"`
touch ${file_name}.log
df -h > ${file_name}.log
- Statistics log
- There is log 1.log, the content is as follows:
112.111.12.248 - [25/Sep/2013:16:08:31 +0800]formula-x.haotui.com "/seccode.php?update=0.5593110133088248" 200"http://formula-x.haotui.com/registerbbs.php" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1;)"
61.147.76.51 - [25/Sep/2013:16:08:31 +0800]xyzdiy.5d6d.com "/attachment.php?aid=4554&k=9ce51e2c376bc861603c7689d97c04a1&t=1334564048&fid=9&sid=zgohwYoLZq2qPW233ZIRsJiUeu22XqE8f49jY9mouRSoE71" 301"http://xyzdiy.5d6d.com/thread-1435-1-23.html" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"
- Requirements: How many visits are counted for each IP?
cat 1.log | awk '{print $1}' | sort -n | uniq -c
- Statistics memory usage
- Write a script to calculate the sum of memory occupied by all processes in the linux system. (Prompt, use ps or top command)
ps aux | grep -v "RSS" |awk '{ ((sum=sum+$6))}END{print sum}'
or
#!/bin/bash
sum=0
for mem in `ps aux | grep -v "RSS" | awk '{print $6 }'`
do
sum=$(($sum+$mem))
done
echo "The total memory is $sum k"
- Batch change file name
- Find all files with the extension .txt in the /123 directory,
- Batch modify .txt to .txt.bak
- Pack all .bak files into 123.tar.gz
- Restore file names in batches, that is, delete the added .bak
#!/bin/bash
##找到.txt文件
find /mnt/files -type f -name "*.txt" > new_files
#遍历 重命名
for file in `cat new_files`
do
mv $file $file.bak
done
#创建新目录 为压缩做准备
dir_name=`date "+%Y%m%d"`
mkdir $dir_name
for file in `cat new_files`
do
cp $file.bak ./$dir_name
done
##压缩
tar czf $dir_name.tar.gz $dir_name/
#还原
for file in `cat new_files`
do
mv $file.bak $file
done
2020-10-31
- Find the prime numbers within 100. (Via shell script)
#!/bin/bash
echo 2
for i in `seq 3 2 100`
do
for j in `seq 2 $i`
do
if [ $(($i%$j)) -ne 0 ]
then
echo $i
break
fi
done
done
- Write a shell script to transfer files larger than 10K in the current directory to the /tmp directory
#!/bin/bash
file_name=/mnt/myr
cd /mnt/myr
echo `ls $file_name` > newfiles
for i in `cat newfiles`
do
file_memory=`du -b $i`| awk '{print $1}'
if [[ $file_memory -lt 10 ]]
then
#echo yes
mv $i /tmp
fi
done