在主目錄創建垃圾箱文件夾 $HOME/Trash. 下載: #git clone https://github.com/lagerspetz/linux-stuff # mv linux-stuff/scripts/saferm.sh /bin/saferm # rm -rf linux-stuff 在 .bashrc 文件中設置別名: # vim .bashrc alias rm=saferm # source ~/.bashrc 立即生效。 說明: rm -rf 刪除文件時會自動將文件移動到/root/Trash中。
編寫定期刪除/root/Trash文件夾下的文件: # cat crondelete.sh #!/bin/bash # write scripts:2018-10-02_22-41 DATE=$(date '+%Y-%m-%d_%H-%M') #判斷是否有該目錄 [[ ! -d /root/Trash ]] && mkdir /root/Trash #創建定時刪除的日誌 DELETESIZE=$(ls -lh /var/log/crondelete.log | cut -f 5 -d ' ') TRASH=$(ls -l /root/Trash/ | wc -l) echo $TRASH >> /var/log/crondelete.log [[ -e /root/Trash ]] || mkdir /root/Trashs && echo yes if [ $TRASH -ge 10 ];then echo y | rm -rf /root/Trash/* fi #定時計劃任務: # crontab -e 21 21 * * * /usr/bin/crondelete.sh & #賦予執行權限: # chmod +x /usr/bin/crondelete.sh
八種在 Linux 上生成隨機密碼的方法:
1. 使用mkpasswd實用程序生成密碼: 安裝: #yum -y install expect #apt-get install whois 獲得密碼(可通過參數來控制長度): RHEL : #mkpasswd Ubuntu : #mkpasswd string 2. 使用openssl命令生成密碼: # openssl rand -base64 30 說明: base64編碼隨機函數, 30表示長度。 3. 使用urandom生成密碼: 設備文件:/dev/urandom 從一個文件中輸出可打印的字符串: # strings /dev/urandom | grep -o '[[:alnum:]]' | head -n 20 | tr -d '\n' 只輸出結果的前32個字符: # < /dev/urandom tr -dc _A-Z-a-z-0-9 |head -c${1:-32} # < /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c32 # </dev/urandom tr -dc '12345!@#$%qwertQWERTasdfgASDFGzxcvbZXCVB' | head -c32 # head -c 6 /dev/urandom | od -An -t x | tr -d ' ' 或者: # tr -cd '[:alnum:]' < /dev/urandom | fold -w32 | head -n1 4.使用SHA算法來加密日期,並輸出結果的前32個字符: # date +%s |sha256sum |base64 |head -c 32 5. 使用dd命令生成隨機密碼: # dd if=/dev/urandom bs=1 count=32 2>/dev/null | base64 -w 0 | rev | cut -b 2- | rev 6. 使用md5sum生成隨機密碼的方法: # date | md5sum # ifconfig | md5sum 7. 使用base64編碼函數生成隨機密碼: # date | base64 # ifconfig | base64 | head -c 10 8. 使用pwgen生成密碼: # apt-get install pwgen 生成長度16,含有數字,含有大小寫字母,含有特殊字符的密碼8個: # pwgen -ncy1 16 8 9. 使用gpg工具生成密碼: GPG是一個遵循OpenPGP標準的加密和簽名工具。 # gpg --gen-random --armor 1 12 說明: --gen-random 生成隨機字節序列選項。 質量爲1,次數12 --armor保證以base64編碼輸出。 10. 使用xkcdpass生成密碼: # python3 -m pip install xkcdpass # xkcdpass | base64 | head -c 20 還有其他第三方工具: Diceware, KeePassX, Revelation, PasswordMaker 等來生成強隨機密碼。