我的代码有问题,如下:
#!/bin/bash
#Removing Files into the Recycle Bin(Deleted Directory)
filemove=$1 #Saving the first argument as "filemove"
mkdir -p ~/deleted #Create the deleted directory if it doesn't exist
mv $filemove ~/deleted #Moves the file
我需要回收站中的文件遵循以下格式:filename_inode
。
答案1
- 使用
stat
工具获取 inode 号。 - 直接使用
mv
. - 引用文件名 (!!),例如
"$filemove"
, never$filemove
。 - 在搬家之前添加一些安全检查
[ ! -e "$target" ] && mv ...
set -euo pipefail
在脚本开头使用,因此出现任何错误都会失败。- 使用
for f in "$@"; do ... done
循环允许多个文件作为参数。 - 再次:引用文件名(!!)。
- 您最好使用一些现成的解决方案,例如参见:
#!/bin/bash
# Removing Files into the Recycle Bin (Deleted Directory)
set -euo pipefail #make script exit on any error
mkdir -p "$HOME/deleted"
dest="$HOME/deleted/${1}_$(stat --format %i "$1")"
# check if file exists, and if not, do the move!
[ -e "$dest" ] && echo "Target exists, not moving: $1" || mv "$1" "$dest"
使用类似trash file1
或 trash "file with spaces"
(假设trash
是脚本名称...)
或者能够一次删除多个文件:
#!/bin/bash
# Removing Files into the Recycle Bin (Deleted Directory)
set -euo pipefail #make script exit on any error
mkdir -p "$HOME/deleted"
for f in "$@"; do
dest="$HOME/deleted/${f}_$(stat --format %i "$f")"
# check if file exists, and if not, do the move!
[ -e "$dest" ] && echo "Target exists, skipped moving: $f" || mv "$f" "$dest"
done
使用类似trash file1 file2 "file with spaces"