`less` "search backward" command line switch?

I know less +/pattern filename will open the file and navigate to the first occurrence of the pattern. Is there a way to tell less to search from the end backwards? Does it even accept more than one + switch?

Do not work:

less +?pattern filename
less +G +?pattern filename

2 Answers

The man page for less states the syntax is less +?pattern filename

?pattern Search backward in the file for the N-th line containing the pattern. The search starts at the last line displayed (but see the -a and -j options, which change this).

Press n to continue to search for the pattern backwards. Press N to search for the pattern forwards.

Example

For testfile.txt of

apple
banana
carrot
apple
banana
carrot
apple
banana
carrot

less +?banana testfile.txt shows the following:

banana
carrot
8

In general it is better to escape ? and the pattern from the shell like this:

less '+?pattern' filename

In your case you might not get what you expect when e.g. there is a file matching +?pattern or you have set bash option nullglob (use shopt nullglob to check it).

In doubt, you can use echo to look what your shell changed:

echo less +?pattern filename

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like