Saturday, August 23, 2008

ls tips - 1

Sometimes it becomes necessary to search for all files that end in a particular character, like, for example a digit. The following command will list out all files that have filenames ending in a digit -
$ ls | grep '[0-9]$'

It is essential that the $ symbol is placed after the paranthesis. This $ symbol will cause grep to look for the digits from 0 to 9 at the end of the filename.
A better way would be -
$ ls | grep '[[:digit:]]$'

Similarly, if you want to search for filenames that begin with a digit, here is the command -
$ ls | grep '^[0-9]'

or
$ ls | grep '^[[:digit:]]'

This will list all filenames that begin with a character.

No comments: