2013-09-10 41 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

相關問題