- 本文中的題目均來自于互聯網
2020-10-29
- 請按照這樣的日期格式(xxxx-xx-xx)每日生成一個檔案,例如今天生成的檔案為2020-10-29.log, 并且把磁盤的使用情況寫到到這個檔案中,(不用考慮cron,僅僅寫腳本即可)!
#!/bin/bash
file_name=`date -d "today" "+%Y-%m-%d"`
touch ${file_name}.log
df -h > ${file_name}.log
- 統計日志
- 有日志1.log,內容如下:
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)"
- 要求: 統計出每個IP的訪問量有多少?
cat 1.log | awk '{print $1}' | sort -n | uniq -c
- 統計記憶體使用
- 寫一個腳本計算一下linux系統所有行程占用記憶體大小的和,(提示,使用ps或者top命令)
ps aux | grep -v "RSS" |awk '{ ((sum=sum+$6))}END{print sum}'
或者
#!/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"
- 批量更改檔案名
- 找到/123目錄下所有后綴名為.txt的檔案,
- 批量修改.txt為.txt.bak
- 把所有.bak檔案打包壓縮為123.tar.gz
- 批量還原檔案的名字,即把增加的.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
- 求100內的質數,(通過shell 腳本)
#!/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
- 撰寫個shell腳本將當前目錄下大于10K的檔案轉移到/tmp目錄下
#!/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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/197148.html
標籤:其他
