国产精品天干天干,亚洲毛片在线,日韩gay小鲜肉啪啪18禁,女同Gay自慰喷水

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

linux 替換rm命令為移動到固定文件夾

2023-03-07 18:55 作者:不妙脆角  | 我要投稿

痛定思痛之后,寫個教程教大家直接把rm給換成mv,這樣就不會徹底刪除找不到了。


首先把rm的意義重定向。

> vi ~/.bashrc

#加入這么一行在bashrc中

alias rm='/home/rm_replace.sh'

#退出~/.bashrc后更新

> source?~/.bashrc

#檢查是否已經(jīng)重定向

> which rm

如果顯示了這行說明就對了。

擬實現(xiàn)的代碼目的和功能:

  1. 當rm時,需要輸入y確認

  2. 輸入y確認刪除后,也只是將文件或目錄移動到指定垃圾箱目錄下,假裝刪除,并保存移動的log文件到垃圾箱目錄中。

  3. 當指定目錄已有同名文件,將新移入的文件加上時間后綴。

  4. 無論是刪除目錄還是文件,都只需要rm,而不需要rm -rf。

注意:如果你完全看不懂下面的代碼,說明你不適合做這項操作。別搞。

定義rm_replace.sh

#!/bin/bash

# Define trash directory

TRASH_DIR="$HOME/Trash_for_rm"


# Create trash directory if it does not exist

if [[ ! -d "$TRASH_DIR" ]]; then

? ? mkdir -p "$TRASH_DIR"

fi


# Create log file in trash directory

LOG_FILE="$TRASH_DIR/trash_log_$(date +%Y%m%d_%H%M%S).txt"

touch "$LOG_FILE"


# Confirm deletion with user

echo "This is a custom rm command. It moves files and directories to the Trash instead of deleting them."

echo "Are you sure you want to continue? [y/n]"

read response



if [[ "$response" =~ ^[Yy]$ ]]; then


for file in "$@"; do

? ? # 獲取文件名和路徑

? ? filename=$(basename "$file")

? ? filepath=$(dirname "$file")


? ? # 判斷目標文件是否存在

? ? if [[ -e "$TRASH_DIR/$filename" ]]; then

? ? ? ? # 給文件名加上時間戳,避免文件名沖突

? ? ? ? timestamp=$(date +%Y%m%d%H%M%S)

? ? ? ? new_filename="${filename}_${timestamp}"

? ? ? ? echo "WARNING: file '$filename' already exists in Trash_for_rm, renaming to '$new_filename'."

? ? ? ? mv "$file" "$TRASH_DIR/$new_filename"

? ? ? ? ? ? echo "[$(date +%Y%m%d_%H%M%S)] Directory $file moved to trash." >> "$LOG_FILE"


? ? else

? ? ? ? mv "$file" "$TRASH_DIR"

? ? ? ? ? ? echo "[$(date +%Y%m%d_%H%M%S)] File $file moved to trash." >> "$LOG_FILE"


? ? fi

done


echo "Files and directories moved to Trash_for_rm."


else

? ? echo "Aborted."

fi

實戰(zhàn)效果如圖所示。


我現(xiàn)在已經(jīng)想開了。

linux 替換rm命令為移動到固定文件夾的評論 (共 條)

分享到微博請遵守國家法律
突泉县| 津南区| 景德镇市| 清水河县| 延安市| 尉犁县| 林芝县| 太和县| 上林县| 鄂尔多斯市| 嘉荫县| 涞水县| 阜新市| 泗水县| 南岸区| 邵阳县| 水富县| 洛南县| 武邑县| 长汀县| 文山县| 称多县| 黄大仙区| 巴楚县| 华蓥市| 仲巴县| 格尔木市| 晴隆县| 茌平县| 宁乡县| 黑山县| 大足县| 马尔康县| 孙吴县| 嘉义县| 汽车| 汉沽区| 天全县| 获嘉县| 仁寿县| 红安县|