Linux look 命令

Linux look Command Tutorial for Beginners (with Examples)

On this page

  1. Linux look command
  2. Q1. How to use look command?
  3. Q2. How to narrow down search within given string?
  4. Q3. Is look search case sensitive?
  5. Q4. What else look can be used for?
  6. Conclusion

Although the Linux find command does a fabulous job for searching on the command line, there may be situations where a dedicated tool may be more convinient. One such case is to find lines in a file that start with a particular word. There exists a command - dubbed look - that does this for you.

 

In this tutorial, we will discuss this command using some easy to understand examples. But before we do that, it's worth mentioning that all examples in the article have been tested on an Ubuntu 18.04 LTS machine.

Linux look command

The look command in Linux displays lines beginning with a given string. Following is its syntax:

look [-bdf] [-t termchar] string [file ...]

And here's what the man page says about the tool:

     The look utility displays any lines in file which contain string as a
     prefix.

     If file is not specified, the file /usr/share/dict/words is used, only
     alphanumeric characters are compared and the case of alphabetic charac?
     ters is ignored.

Following are some Q&A-styled examples that should give you a good idea on how the look command works.

Q1. How to use look command?

Simple, just provide the word you want to search and the file in which you want to search as inputs to the look command.

Here's an example:

look The test.txt

The above command searches for lines beginning with 'The' in the file 'test.txt'.

For reference, here's the test.txt file:

How to use look command

And here's the output produced:

look command result

Q2. How to narrow down search within given string?

The look command also allows you to narrow down your search by allowing you to provide a terminating character. Here's how the man page explains the -t option:

-t, --terminate termchar
             Specify a string termination character, i.e., only the characters
             in string up to and including the first occurrence of termchar
             are compared.

 

To test this option, I updated test.txt with a few more lines. Take a look:

test text

And then executed the following command:

look -t n Fund test.txt

So we are basically asking look to search for lines beginning with the word 'Fund' but also include those that start with 'Fun'. And here's the output:

How to narrow down search within given string

Q3. Is look search case sensitive?

Yes, it is. For example, if you run the following command (which is same as the one we used in last Q&A, save for the lower case of the first character in the word 'fund'):

look -t n fund test.txt

No output will be produced.

However, you can force look to ignore case by using the -f option.

Is look search case sensitive

Q4. What else look can be used for?

You can use the look command to search for all words beginning with a set of characters. For example, to see what all words start with the character sequence 'love', just use the look command in the following way:

look love

Here's a sample output:

What else look can be used for

Conclusion

So all in all, look is a useful little command line utility that you should be at least aware of, for you never know when it may turn out to be a saviour for you. It doesn't offer many command line options, and majority we've already discussed here. To learn more about look, head to its man page.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值