Displaying a Line in Linux

Here’s an interesting thing. Let’s say you have a problem like I did, above. An error pops up in a big project, referring to a specific line number of a generic filename. How generic? In Linux, we can see exactly how many of those files with that name exist with this command:

vagrant@homestead:~/$ find . -name Client.php
./app/Contracts/News/Client.php
./vendor/guzzlehttp/guzzle/src/Client.php
./vendor/nexmo/client/src/Account/Client.php
./vendor/nexmo/client/src/Application/Client.php
./vendor/nexmo/client/src/Client.php
./vendor/nexmo/client/src/Conversion/Client.php
./vendor/nexmo/client/src/Insights/Client.php
./vendor/nexmo/client/src/Message/Client.php
./vendor/nexmo/client/src/Numbers/Client.php
./vendor/nexmo/client/src/Redact/Client.php
./vendor/nexmo/client/src/Verify/Client.php
./vendor/php-http/guzzle6-adapter/src/Client.php
./vendor/predis/predis/src/Client.php
./vendor/sentry/sentry/lib/Raven/Client.php
./vendor/symfony/http-kernel/Client.php

OOh. Quite a few! One option is that I open each of them up in my favourite text editor, and scroll down (or jump) to that line: 131. Sounds OK for a small number of them, but I can show you another way which may work out easier.

To start, how do we show a given line in a text file? For that, we can use a utility like sed.

vagrant@homestead:~/$ sed -n '131,131p' ./vendor/guzzlehttp/guzzle/src/Client.php
return $this->requestAsync($method, $uri, $options)->wait();

Using this tool, we are asking it to print out from line 131, to line 131, of the file: ./vendor/guzzlehttp/guzzle/src/Client.php. In effect, we are just printing out one line.

That’s helpful and a little easier than opening up the editor, I guess, but there is a way to combine both commands; in effect, find me all the files named Client.php AND show that line number. We can do that with this:

vagrant@homestead:~/$ find . -name Client.php -exec sed -n '131,131p' {} \;
return $this->requestAsync($method, $uri, $options)->wait();

public function delete($application)

} else {
if ($number !== null) {

$connection = $initializer($parameters, $options);
protected $_shutdown_function_has_been_set;
/**

All the magic happens with the -exec command line option and this part: {} \; which replaces each filename , at that position of the executing command.

Did that solve my problem? No, but it might yours :-)


Hi! Did you find this useful or interesting? I have an email list coming soon, but in the meantime, if you ready anything you fancy chatting about, I would love to hear from you. You can contact me here or at stephen ‘at’ logicalmoon.com