How to test for existence of a file matching a pattern?
This really sounds simple but I can't think of a neat way of doing it, I want to test (in bash) if there's any file with a specific suffix in a particular directory. This means I can't use "[ -f <filename> ]" because I don't know the filename and I don't see how I can use "[ *.sfx ]" because if there are no *.sfx files the string is still not empty. I suppose I could say something like "[ *.sfx = '*.sfx' ]" but it's a quoting nightmare. Any better ideas? -- Chris Green
On 9 July 2014 17:11, Chris Green <cl@isbd.net> wrote:
This really sounds simple but I can't think of a neat way of doing it, I want to test (in bash) if there's any file with a specific suffix in a particular directory.
Print the filenames in the directory and grep for the suffix at the end of the line? Or write a C program to do it? That way, you could re-use that whenever you need to write a similar script. Regards, Srdjan
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA256 On 09/07, Chris Green wrote:
This really sounds simple but I can't think of a neat way of doing it, I want to test (in bash) if there's any file with a specific suffix in a particular directory.
I suppose you could use ls ls gl*b &>/dev/null && echo Yes || echo No seems to work Steve -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iQIcBAEBCAAGBQJTvWyMAAoJEL/3HArzwYbRnSIP/js/4ME/u/eoQ2/5ACQZgbgQ 17cTK6v8ao6ZokPa5zJ2LxkuVd2hcJkiajFHE6JOu/HKYJpKj9r1KpqW0/2hfAmO xzst38LPJBopPz6aEL9tbc4sxAjDcZrXxr2du2YzPHKV1LVIxlIC1eemF3MWAxcy paleeQNh8WUahY4Hw/vdVhu/XiXmdZ6eI1CoYXwfPljZIjZCP6AmWtLrnnLZhuut DuSFrnGRXI1MM0LhygoOad9JXlLkjq9MMIqf9C5NSzOu6vpAAaBoBNB6txFJfC9R NSRriuW87zhaCFEpOBPJ7gTJ5DJkkxEAd99e2NdEDqaPucIM62I7eBnFNckAJEza 0ql0USEGLbrPIYTlm0RQ0mYbUvyFMCRcfKWPQbvGiUEIYTluK9WJTx0lxOt/Mzm4 1Nwt1BVadIz0VsySWyP3PVKbeLPx6SsEZQaeq71yk5UgnW5yhm0ozU/y6MP8QB9i JrejR3cCCjr7LxVQZcEbD75COVe/o9YYT4TI5O8cGoHnIZrbVTHldOloUBSgb3Xu CR20XzDChlWn8Rt3L//VNB0F8UvZj4Kv+p2PAUiS+BOIUtCIS6iYB6Z00snAxwFI TMQZj1vI4sIskVZmLWv4P2TpVmhq+x/ni5DpXhElgKucpwQoRkqKY+SBXzIbe4i+ nz1L6e6YYVGpsHUFueyF =Ew/p -----END PGP SIGNATURE-----
Use Python import os import fnmatch for f in fnmatch.filter(os.listdir(os.curdir),'*.sfx'): print f On 9 July 2014 17:11, Chris Green <cl@isbd.net> wrote:
This really sounds simple but I can't think of a neat way of doing it, I want to test (in bash) if there's any file with a specific suffix in a particular directory.
This means I can't use "[ -f <filename> ]" because I don't know the filename and I don't see how I can use "[ *.sfx ]" because if there are no *.sfx files the string is still not empty.
I suppose I could say something like "[ *.sfx = '*.sfx' ]" but it's a quoting nightmare.
Any better ideas?
-- Chris Green
_______________________________________________ main@lists.alug.org.uk http://www.alug.org.uk/ http://lists.alug.org.uk/mailman/listinfo/main Unsubscribe? See message headers or the web site above!
participants (4)
-
Chris Green -
Ewan Slater -
Srdjan Todorovic -
Steve Engledow