简体   繁体   中英

Shell filename substitution for zero or one occurrence

Having following list of files:

-rw-r----- 1 mysql install 80550 Dec 11 16:40 TEST30042.log
-rw-r----- 1 mysql install 74006 Dec 11 17:39 TEST30032.log
-rw-r----- 1 mysql install 74566 Dec 11 17:39 TEST30012.log
-rw-r----- 1 mysql install 88435 Dec 11 17:39 TEST32.log
-rw-r----- 1 mysql install 79540 Dec 11 17:39 TEST30022.log
-rw-r----- 1 mysql install 17276 Dec 11 21:07 TEST30021.log
-rw-r----- 1 mysql install 15623 Dec 11 21:07 TEST31.log
-rw-r----- 1 mysql install 17280 Dec 11 21:07 TEST30031.log
-rw-r----- 1 mysql install 16302 Dec 11 21:07 TEST30011.log
-rw-r----- 1 mysql install 16002 Dec 11 21:07 TEST30041.log
-rw-r----- 1 mysql install 29035 Dec 12 14:00 TEST30010.log
-rw-r----- 1 mysql install 16926 Dec 12 14:00 TEST30020.log
-rw-r----- 1 mysql install 14381 Dec 12 14:00 TEST30040.log
-rw-r----- 1 mysql install 29157 Dec 12 14:00 TEST30030.log
-rw-r----- 1 mysql install 21761 Dec 12 14:00 TEST30.log
-rw-r----- 1 mysql install  5977 Dec 12 15:02 TEST3002.log
-rw-r----- 1 mysql install  6670 Dec 12 15:03 TEST3001.log
-rw-r----- 1 mysql install  6670 Dec 12 15:03 TEST3003.log
-rw-r----- 1 mysql install  6526 Dec 12 15:03 TEST3004.log
-rw-r----- 1 mysql install  7167 Dec 12 19:29 TEST3.log

Trying to list only the files with the name TEST3.log, TEST30.log, TEST31.log, TEST32.log using following command. But the not getting desired output.

$ ls -ltr TEST3?.log
-rw-r----- 1 mysql install 88435 Dec 11 17:39 TEST32.log
-rw-r----- 1 mysql install 15623 Dec 11 21:07 TEST31.log
-rw-r----- 1 mysql install 21761 Dec 12 14:00 TEST30.log

To my understanding, "?" is notation for zero or single occurrence. Couldn't figure out why TEST3.log file is being left out..

Expected Output

-rw-r----- 1 mysql install 88435 Dec 11 17:39 TEST32.log
-rw-r----- 1 mysql install 15623 Dec 11 21:07 TEST31.log
-rw-r----- 1 mysql install 21761 Dec 12 14:00 TEST30.log
-rw-r----- 1 mysql install 21761 Dec 12 14:00 TEST3.log

Figured one way to do this is by doing

ls -ltr TEST3?.log TEST3.log
-rw-r----- 1 mysql install 88435 Dec 11 17:39 TEST32.log
-rw-r----- 1 mysql install 15623 Dec 11 21:07 TEST31.log
-rw-r----- 1 mysql install 21761 Dec 12 14:00 TEST30.log
-rw-r----- 1 mysql install 21761 Dec 12 14:00 TEST3.log

Wondering if there is a better simplest way to do it.

Thanks in advance.

The ? operator represents exactly one wildcard character. There is no operator for zero or one characters. You would have to use a loop or pipe ls through another tool.

I assume you are using Linux based on the directory listing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM