我正在尝试使用类似于回收站的附加功能来模仿rm行为。这是我的代码到目前为止的样子:
#!/bin/bash
# tests if bin does not exist and if true, create it
binName=$HOME/deleted
if [ ! -d $binName ]
then
mkdir $binName
else
echo "recycle bin exists"
fi
if [ $# -eq 0 ] ; then
echo "No argument passed"
elif [ -d $1 ] ; then
echo "is dir"
elif [ ! -e $1 ] ; then
echo "does not exist"
else
mv $1 $binName
fi
我正在努力在移动文件的末尾添加一个得分和inode编号,以避免bin目录中出现重复文件错误。我尝试使用此link中的大括号扩展,但它会导致错误。
答案 0 :(得分:2)
使用ls
:
inode=$(ls -id "${1}" | cut -d " " -f 1)
mv "$1" "${binName}/${1}_${inode}"
或stat
:
inode=$(stat --printf %i "${1}")
mv "$1" "${binName}/${1}_${inode}"
或GNU find
:
inode=$(find -maxdepth 1 -name "${1}" -printf "%i")
mv "$1" "${binName}/${1}_${inode}"
答案 1 :(得分:1)
在您链接的问题的accepted answer中,使用大括号扩展来避免重复原始文件名。 (它作为源名称出现一次,作为目标名称的一部分出现。)在您的情况下,这将是毫无意义的,因为您已经在名称为$1
的变量中使用了名称。 / p>
所以你可以
mv $1 $binName/${1}_$(${command that gives the inode of} $1)
e.g。作为@Cyrus suggested
mv $1 "${binName}/${1}_"$(find . -maxdepth 1 -name "${1}" -printf "%i")
仍然可以使用大括号扩展来避免重复#34; $1
",但这只会使命令难以阅读。
而不是
if [ ! -d $binName ]
then
mkdir $binName
fi
你可能只是使用
mkdir -p $binName
如果目录已经存在,这将使目录保持不变。如果还缺少任何父目录,它也会根据需要创建这些目录。但是,它当然不能输出"回收站存在",但我想你只是出于调试/演示的原因,无论如何。