簡體   English   中英

linux bash查找幾位數的文件名

[英]linux bash find file name with several digits

我有一個包含文件Job_1,Jobs_2 ... Jobs_77的文件夾。 我用了

find  . -name  "job_[0-9]{1,2}"  -type  f

列出這些文件,但沒有輸出。 問題是什么?

或者,要友好:

find  . -regextype posix-awk -regex  ".*job_[0-9]{1,2}.*"  -type  f

注意:在這種情況下,以posix-extended為例將產生相同的結果。 但是天生就是懶惰,所以我選擇了awk,因為它更短:}

問題中命令的第一個問題:

find .-name "job_[0-9]{1,2}" -type f

...是您正在搜索以字符串jobs_而不是job_開頭的文件,因此上述命令將找不到名為file作業s _1,job s _2 ... job s _77的任何文件。

正確使用的命令是:

find . -regextype posix-awk -regex ".*jobs_[0-9]{1,2}.*" -type f -exec basename {} \;
  • -regextype posix-awk此選項更改-regex理解的正則表達式語法,該語法允許find程序使用posix-awk正則表達式。
  • -exec basename {} \\; -轉換find . -regextype posix-awk -regex ".*jobs_[0-9]{1,2}.*" -type f的結果find . -regextype posix-awk -regex ".*jobs_[0-9]{1,2}.*" -type f find . -regextype posix-awk -regex ".*jobs_[0-9]{1,2}.*" -type f從文件的完整路徑到文件名, find . -regextype posix-awk -regex ".*jobs_[0-9]{1,2}.*" -type f ,以便結果中僅顯示文件名。 這使得結果find -type f-regex類似的結果選項find -type f沒有-regex中只顯示文件名的選項。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM