简体   繁体   中英

List only files based on their middle part using bash ls command

I am a noob in bash and have a very basic question about bash. I have files like:

a_lsst_z1.5_000.txt
a_lsst_z1.5_001.txt
a_lsst90_z1.5_001.txt
a_lsst_mono_z1.5_000.txt
a_lsst_mono_z1.5_001.txt
a_lsst_mono90_z1.5_000.txt
a_lsst_mono90_z1.5_001.txt
and so on

I would like to list ONLY files having lsst not ( lsst90 or lsst_mono or lsst_mono90 .

I have tried:

ls a_lsst_*.txt # but it gives all files

Required output:

a_lsst_z1.5_000.txt
a_lsst_z1.5_001.txt

How to get only lsst files?

Maybe just match the first character after _ as a number?

echo a_lsst_[0-9]*.txt

After your edit, you could just match the z1.5 part:

echo a_lsst_z1.5_*.txt

尝试这个

ls -ltr a_lsst_z1.5_*.txt

If you want to use ls and exclude certain character, you can try:

ls a_lsst[^9m]*.txt

This will exclude lsst90 and lsst_mono etc files.

find . -iname "a_lsst_*.txt" -type f -printf %P\\n 2>/dev/null

gives:

a_lsst_mono90_z1.5_001.txt
a_lsst_z1.5_000.txt
a_lsst_z1.5_001.txt
a_lsst_mono_z1.5_000.txt
a_lsst_mono_z1.5_001.txt
a_lsst_mono90_z1.5_000.txt

and

find . -iname "a_lsst_z1*.txt" -type f -printf %P\\n 2>/dev/null

gives:

a_lsst_z1.5_000.txt
a_lsst_z1.5_001.txt

with find command in the current dir . and with -iname you'll get expected results when using the pattern a_lsst_*.txt or a_lsst_z1*.txt .

Use -type f to get only match on files (not dirs).

Use -printf with %P to get paths without ./ at the beginning and \\\\n to have them ended with a new line char.

2>/dev/null prevents from displaying any error including very common Permission denied when using find command.

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