国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 服務器 > Web服務器 > 正文

101個腳本之建立linux回收站的腳本

2024-09-01 13:48:04
字體:
來源:轉載
供稿:網友

眾所周知,linux是沒有回收站的,一些人很害怕刪錯東西(有經驗的linux管理員極少范這錯誤),個人不建議回收站,而應該是培養個人的安全意識。有點小跑題。
接著回來101個腳本之#15 Archiving Files As They're Removed 就是建立一個linux回收站的腳本

#!/bin/sh# newrm, a replacement for the existing rm command, provides a # rudimentary unremove capability by creating and utilizing a new # directory within the user's home directory. It can handle directories # of content as well as individual files, and if the user specifies # the -f flag files are removed and NOT archived.# Big Important Warning: You'll want a cron job or something similar to keep # the trash directories tamed. Otherwise nothing will ever actually # be deleted from the system and you'll run out of disk space!mydir="$HOME/.deleted-files" realrm="/bin/rm" copy="/bin/cp -R"if [ $# -eq 0 ] ; then # let 'rm' ouptut the usage error exec $realrm # our shell is replaced by /bin/rm fi# Parse all options looking for '-f'flags=""while getopts "dfiPRrvW" opt do case $opt in f) exec $realrm "$@" ;; # exec lets us exit this script directly. *) flags="$flags -$opt" ;; # other flags are for 'rm', not us esac done shift $(($OPTIND - 1))# Make sure that the $mydir existsif [ ! -d $mydir ] ; then if [ ! -w $HOME ] ; then echo "$0 failed: can't create $mydir in $HOME" >&2 exit 1 fi mkdir $mydir chmod 700 $mydir # a little bit of privacy, please fifor arg do newname="$mydir/$(date "+%S.%M.%H.%d.%m").$(basename "$arg")" if [ -f "$arg" ] ; then $copy "$arg" "$newname" elif [ -d "$arg" ] ; then $copy "$arg" "$newname" fi doneexec $realrm $flags "$@" # our shell is replaced by realrm

我們來說下這個腳本的實現思路
將原本的rm命令用我們這個帶有回收站機制的myrm腳本代替(alias別名),腳本將要刪除的文件移動到了home下個人目錄中以.deleted-files 命名的隱藏文件夾。

接著我們看看這個腳本是怎么實現的

while getopts "dfiPRrvW" opt do case $opt in f) exec $realrm "$@" ;; # exec lets us exit this script directly. *) flags="$flags -$opt" ;; # other flags are for 'rm', not us esac done

這一段說明 要是命令用帶 –f 選項的話,則不進回收站,調用原本的rm命令。

for arg do newname="$mydir/$(date "+%S.%M.%H.%d.%m").$(basename "$arg")" if [ -f "$arg" ] ; then $copy "$arg" "$newname" elif [ -d "$arg" ] ; then $copy "$arg" "$newname" fi done

用for循環順序處理參數
newname="$mydir/$(date "+%S.%M.%H.%d.%m").$(basename "$arg")" 回收站里文件命名.

 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 遂溪县| 沙坪坝区| 江阴市| 兰坪| 枞阳县| 芦山县| 河曲县| 苍山县| 阳泉市| 百色市| 临高县| 西昌市| 贵州省| 綦江县| 威海市| 沅江市| 扶风县| 富民县| 上犹县| 松阳县| 西和县| 安宁市| 邵武市| 岐山县| 汾西县| 南昌市| 分宜县| 天门市| 高阳县| 丁青县| 利津县| 南召县| 嘉峪关市| 水城县| 武夷山市| 锡林浩特市| 东至县| 山丹县| 朝阳县| 黄梅县| 阳东县|