DEV Community

Cover image for Don't fear the command line:Inspecting and Interacting with Files
Dionysia Lemonaki
Dionysia Lemonaki

Posted on

Don't fear the command line:Inspecting and Interacting with Files

There is so much more power in the command line than just navigating through folders , creating new files and then renaming or deleting them.
There is the opportunity to actually inspect the contents of files in great detail , and search through them when we want to find something specific. The most fascinating part is the fact that we are able to interact with files from the web and actually download them!

surpise

In my last post I wrote about the cat command that literally dumps the contents of files to the screen. If you fancy having a read about that you can check it out here

When it comes to cat to examine file contents we can be quite restricted and that command doesn't work particularly well with longer files that contain hundreds of words, as it goes through the file really fast and just leaves us at the end.That way, the only option to search through the file is to use the scrollbar to navigate which can be quite tiring if the file is really long. It's more useful when used for files a few lines long.There are other commands that are more helpful to use when it comes to inspecting files that their contents are too long to fit into a single screen.

Heads or Tails?

Two complimentary commands useful for when we know we just want to inspect a particular part of the file or specifically the beginning or end of a file is head and tail.

Tail

This command by default outputs the last 10 lines of a file.However ,if we want to be more specific and change the number of lines shown, that can be done with the -n option that controls the count :

tail -n 3 textfile.txt
Enter fullscreen mode Exit fullscreen mode

This way, only the last three lines will be shown in the terminal.

One of the most useful ways to use tail is to view a file that is actively changing.For that we use the -f option which stands for follow.

One example for that could be to monitor files used to log activity of web servers or otherwise a practice called tailing the log file.To achieve that ,in one terminal tab we simulate the creation of a log file by pinging a server ,where the ping command pings a server to see if it's working , and we then redirect that output to a log file.

ping google.com > google.log
Enter fullscreen mode Exit fullscreen mode

In a second tab(CMD + T) we type the command to tail the log file

tail -f google.log
Enter fullscreen mode Exit fullscreen mode

We then see different data on that tab,that keeps on updating.
To stop this process we hit control + c on both tabs.

On that note, something similar that can be done with tail -f is to view in real time what is being added to a file of ours, anything being added would show up immediately.Similar to what I described above, in one tab we type our commands with what we want to add to a file:

echo 'I am adding this line' > textfile.txt
Enter fullscreen mode Exit fullscreen mode
echo 'I am also adding one more line' >> textfile.txt
Enter fullscreen mode Exit fullscreen mode

In a second tab ,the tail tab, we view in real time the changes we are making.

tail -f textfile.txt
Enter fullscreen mode Exit fullscreen mode

Switching between the two tabs shows us tail -f in action and how it works.

ping

Head

The head command works exactly the same way as the tail command, only in this case it outputs the first 10 lines of a file.To change that number we can use the -n option as we did in tail above.

Less is more

The less command is the most useful one in my opinion to search through long files.The variety of shortcuts available make it easy to navigate a file and look for something very specific if you wish for it.
Originally the less program was designed as an improvement to the more program because it lacked features and had limitations.With less it's easy to view long text documents in a page by page manner, as it allows us to move through pages both forwards and backwards.

Why view a file in the first place?

One reason for viewing files, especially files located on our system, is that many of them are configuration files meaning they contain system settings.Some of the programs our system uses are stored in these files and have human readable plain text format.Reading them could give us some kind of understanding as to how things work.

How to use it

Less is used just for reading and not writing.

less textfile.txt
Enter fullscreen mode Exit fullscreen mode

throws us straight into reader mode

Alt Text

Less is an interactive program and let's us navigate through files in several useful ways.Below is a table with some of the most useful keyboard commands.

command What it does
Space bar/ ^+F/F move forward one page
^+B/B move back one page
G move to the end of the file
1G/g move to the beginning of the file
nG(where n an integer) move to that number of line specified
/ string search for a string of words
n move to the next search result
N move to the previous search result
H display help
q quit

If you want to read further on less you can check the wikipedia page.

Interaction

The curl command allows us to interact with URLs in order to transfer data to and from a server, make HTTP requests and download files from the web.It grabs data from or to a server and we can use it to grab public domain text.It will pretty much take any URL on the web.It stands for C (SEE) URL which basically means to see a web address.
If we typed

curl google.com
Enter fullscreen mode Exit fullscreen mode

we would get this in return
Alt Text
which is not that helpful.Without using any options it displays the specified resource to the standard output.The URL is loaded and the contents are printed out.

curl -s google.com
Enter fullscreen mode Exit fullscreen mode

When using the -s option ,the action is silent or muted, as a progress meter is not shown, neither are error messages displayed.
If for some reason we just wanted to view the source code(the way we can view it through the browser developer tools), we need to follow redirects with the -L option:

curl -L google.com
Enter fullscreen mode Exit fullscreen mode

To get curl to write the data to a file rather than just displaying it to the terminal, we can actually save it to one, which is a helpful feature.

The lowercase option -o downloads the file and saves it to the name we have defined and would like to save it under(in this case google.html).

curl -o -L  google.html google.com
Enter fullscreen mode Exit fullscreen mode

When using the -I option it includes header information such as HTTP headers on status codes for the address

curl -I -L google.com
Enter fullscreen mode Exit fullscreen mode

Alt Text

The basic pattern that curl uses is to start with the command(curl),followed by some options, followed by the objects we want to actually operate on.

fascinating

Those are just some of the ways we can inspect in detail and interact with files both our own and from the web.

Thanks for reading!😃

Top comments (0)