在添加下划线和索引节点号的同时将文件移动到另一个目录

Moving file to another directory while adding underscore and inode number

我正在努力模仿 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  ] ; then
        echo "is dir"
elif [ ! -e  ] ; then
        echo "does not exist"
else
        mv  $binName
fi

我正在努力在移动文件的末尾添加下划线和 inode 编号,以避免 bin 目录中的重复文件错误。我尝试使用此 link 中的大括号扩展,但它会导致错误。

ls 试试这个:

inode=$(ls -id "" | cut -d " " -f 1)
mv "" "${binName}/_${inode}"

stat:

inode=$(stat --printf %i "")
mv "" "${binName}/_${inode}"

GNU find:

inode=$(find -maxdepth 1 -name "" -printf "%i")
mv "" "${binName}/_${inode}"

在您链接的问题的 accepted answer 中,大括号扩展用于避免重复原始文件名。 (它会作为源名称出现一次,作为目标名称的一部分出现一次。)这在您的情况下毫无意义,因为您已经在短名称 </code>.[=18 的变量中有了名称=] <p>所以你可以</p> <pre><code>mv $binName/_$(${command that gives the inode of} )

例如作为@Cyrus

mv  "${binName}/_"$(find . -maxdepth 1 -name "" -printf "%i")

仍然可以使用大括号扩展来避免“</code>”的重复之一,但这只会使命令更难阅读。</p> <h3>关于创建目录的注意事项:</h3> <p>而不是</p> <pre><code>if [ ! -d $binName ] then mkdir $binName fi

你可以简单地使用

mkdir -p $binName

如果目录已经存在,这将使目录保持不变。如果还缺少任何父目录,它也会根据需要创建这些目录。但是,它当然不能输出 "recycle bin exists",但我猜你只是出于 debugging/demonstration 的原因才输出的。