我正在運行find命令來獲取具有特定大小的文件列表,然後將輸出保存在文件中,現在我逐個遍歷該文件並詢問用戶哪一個他想刪除。我想要做一些事情,比如在列表中的每個文件旁邊添加一個數字,以便用戶可以直接輸入與此文件關聯的數字並刪除,而不必遍歷整個文件。請幫忙。將數字添加到由find命令返回的文件列表中
1
A
回答
1
select f in $(find . -name '*.txt'); do
if [ -n "$f" ]; then
# put your command here
echo "rm $f"
fi
done
0
find . -size 5k -okdir rm {} ";"
要求您爲每個文件,是否要執行的操作與否,沒有中間文件。
-okdir
是一個Gnu擴展找到,並不適用於所有的實現。
另外,精益的方法是使用select
:
select fno in $(find . -size 5k);
do
echo rm $fno
done
這是一個bashism,也許不是在你的shell存在。
help select
顯示其用法。不幸的是,它也不像查找解決方案一樣允許一次選擇多個條目,但是您可以重複選擇一些內容,直到您點擊Ctrl + D,這很安靜舒適。
select: select NAME [in WORDS ... ;] do COMMANDS; done
Select words from a list and execute commands.
The WORDS are expanded, generating a list of words. The
set of expanded words is printed on the standard error, each
preceded by a number. If `in WORDS' is not present, `in "[email protected]"'
is assumed. The PS3 prompt is then displayed and a line read
from the standard input. If the line consists of the number
corresponding to one of the displayed words, then NAME is set
to that word. If the line is empty, WORDS and the prompt are
redisplayed. If EOF is read, the command completes. Any other
value read causes NAME to be set to null. The line read is saved
in the variable REPLY. COMMANDS are executed after each selection
until a break command is executed.
Exit Status:
Returns the status of the last command executed.
這是什麼樣子:
select fno in *scala ; do echo "fno: " $fno; done
1) Cartesian.scala 6) MWzufall.scala
2) Haeufigkeit.scala 7) Shuffle.scala
3) HelloChain.scala 8) eHWCChain.scala
4) Lychrel.scala 9) equilibrum.scala
5) M.scala 10) scala
#? 3
fno: HelloChain.scala
#? 3 4
fno:
#?
注意單詞用空格隔開,所以你要照顧在第二個例子,如果你在文件名中有空格的工作。
相關問題
- 1. FIND命令返回空值
- 2. 將XeLaTeX添加到命令列表
- 3. 列出具有日期命令和返回字數的文件
- 4. 使用Perl將命令添加到Tk中的列表框中
- 5. 從列表中添加數字(例如,asdf125dkf將返回8)
- 6. 無法將路由命令添加到WPF中的CheckBox中
- 7. 將文件添加到列表中時的回調
- 8. 由蘭德命令,不返回數據
- 9. CakePHP的find命令警告,儘管返回的數據正確
- 10. 如何批量添加到由rxjs observables返回的數組/列表中?
- 11. 「find」命令無法檢測到執行期間添加的文件
- 12. 將文本文件中的數據添加到列表框
- 13. 將返回的元組添加到列表中
- 14. Cmake將命令行參數添加到二進制文件
- 15. 將文件名添加到java中的數組列表中
- 16. sed命令將文本行添加到文件的最末尾
- 17. Linux:將命令添加到bashrc中的另一個命令中
- 18. 從命令行添加文件到MSI?
- 19. 遞歸添加到列表返回null
- 20. 將命令行命令添加到文件重命名批處理
- 21. 通過命令行將文件添加到redmine中
- 22. 列表中的數字返回列表
- 23. 將文件添加到啓動列表
- 24. 如何將方法返回值添加到python列表中?
- 25. 將加載命令添加到mach-o二進制文件
- 26. 使用python將文件列表添加到字典中
- 27. 將.my.cnf添加到mysql命令行中
- 28. 將新屬性添加到列表中的對象並返回新列表
- 29. 使用python將列表中的數字添加到現有文件中
- 30. 如何將我的返回類的特定字符串的txt文件添加到列表中?
爲什麼不循環'find'的輸出並詢問用戶?如果我理解正確,爲什麼不使用'find'..'exec' combo&use'rm -i'這是交互式刪除,它會在刪除文件之前詢問用戶[y/n]查詢。除非將文件用於除清單文件以外的其他內容,否則不需要將輸出保存到文件中 – 2012-02-24 11:33:17