Unix - 用于从任何目录中查找文件并移动它的Shell脚本

时间:2013-09-10 21:51:03

标签: shell unix

我目前正在编写一本练习册,我必须创建一个shell脚本,它将从任何目录中找到一个文件并将其移动。

虽然我遇到困难,因为文件可能在任何目录中(所以我没有找到它的路径)。我使用了-print标志的find选项,使用mv命令移动它的下一步是什么?

我的代码到目前为止读入一个变量,检测是否已输入文件,文件或目录是否存在,或者是否存在。

上面提到的下一个阶段是找到该文件,然后将其移动到“测试”文件中。

如果有人有任何建议,我们将不胜感激。

#!/bin/bash

bin="deleted"

if [ !  -e bin ] ; then
    mkdir $bin
fi

file=$1

#error to display that no file has been entered
if [[ ! $file ]]; then
    echo "no file has been entered"
fi

#file does not exist, display error message
if [[ !  -f $file ]]; then
    echo "$file does not exsist!"
fi

#check to see if the input is a directory
if [[ -d $file ]]; then
    echo "$file is a directory!"

if [[ -e $file ]]; then *** move to test folder
****This is where I am having the problems

1 个答案:

答案 0 :(得分:3)

find / -type f -name FILENAME | xargs -I foobar echo mv foobar /tmp(删除echo以使命令实际工作..我把它放在那里只是为了避免意外移动文件只是为了尝试命令)

请注意,-I foobar表示在mv foobar /tmp中将foobar字符串替换为找到的文件的完整路径。

例如,尝试:find / -type f -name FILENAME | xargs -I foobar foobar is a cool file

相关问题