[英]Unix - Shell script to find a file from any directory and move it
我目前正在練習一本練習簿,我必須創建一個Shell腳本,該腳本將從任何目錄中查找文件並將其移動。
盡管我遇到了麻煩,因為該文件可以放在任何目錄中(因此我沒有找到它的路徑)。 我已經將find選項與-print標志一起使用了,下一步要使用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
find / -type f -name FILENAME | xargs -I foobar echo mv foobar /tmp
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
find / -type f -name FILENAME | xargs -I foobar foobar is a cool file
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.