2013-09-10 43 views
1

我目前正在通过练习册工作,并且必须创建一个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 

回答

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

+0

所以我最后的if语句S /是:如果[[-e $文件]];然后找到/ -name FILENAME | xargs mv测试? – user2141414

+0

也许你还应该加上'-not -wholename'/ tmp/*''或'/ path/to/test_dir/*'这样的选项。 – konsolebox

+0

@ user2141414它应该是:'find/-name FILENAME | xargs -I foobar mv foobar/tmp' ...要小心! – necromancer

相关问题