Hands-On File Operations

Hands-On File Operations

The Command Line Essentials for File Operations

Β·

6 min read

Introduction

Welcome everyone! πŸ‘‹πŸ»

You learned about Linux file permissions and ownership in the previous part of this series. In this part, you'll learn how to work with files in the command line.

So without further ado, let's get started! πŸš€

Viewing files

There are several commands you can use to look inside files without opening them in a text editor. Here are a few of the most common commands:

  • cat - The cat command is a versatile command that can be used to print the contents of a file to the terminal, or to concatenate the contents of multiple files into a single file. It will print the entire file to the terminal, regardless of its size. This can be useful for small files, but it can be overwhelming for large files.

  • less - The less command is a more powerful alternative to cat command, as it allows you to scroll through the contents of a file one page at a time, and to search for specific text patterns.

  • head - The head command prints the first few lines of a file to the terminal. This can be useful for quickly viewing the contents of a large file, or for getting a sense of what a file is about. By default, it will print the first 10 lines of a file. However, you can specify a different number of lines to print.

  • tail - The tail command prints the last few lines of a file to the terminal. This can be useful for seeing the most recent changes to a file or for troubleshooting problems. By default, it will print the last 10 lines of a file. However, you can specify a different number of lines to print.

Let's see all of these in action!

Using cat

  • Viewing the content of kubernetes.yaml file.

As you can see above the whole content of the kubernetes.yaml file has been printed to the console, regardless of its size.

  • Printing the content of multiple files.

  • Concatenating content of multiple files.

The first command, cat greet.sh hello.sh > concatenated.sh, will concatenate the contents of the files greet.sh and hello.sh and save the result to a new file called concatenated.sh. The > symbol is used to redirect the output of the cat command to the file concatenated.sh. You don't need to worry about the > symbol for now, we'll cover it in more detail in the next part.

You can do a lot more with the cat command check man pages.

Using less

The less command allows you to view a text file one page at a time. To search for a word, type / followed by the word and press Enter. The word will be highlighted in the text. This can be seen in the below image.

Using head

  • Viewing the first few lines of the kubernetes.yaml file.

As you can see, the command only printed the first 10 lines of the file. You can verify this by counting the lines yourself.

  • To view lines other than the default 10 lines, use the head command with the -n option followed by the number of lines you want to view. For example, you can see below I asked head to print the first 5 and 12 lines of the kubernetes.yaml file respectively.

TIP: You can specify the number of lines to print by using the -<number_of_lines> option, without the -n option.

Using tail

As you know the tail command is the opposite of head, so the syntax for both commands is similar, but with the tail command, you can monitor a file as it grows. See the below examples.

  • Viewing a specific number of lines.

  • Monitoring a file using tail -f command, can be seen in the below image.

I ran the hello.sh script in the background and saved its output to the hello file using the > operator. This allows me to show you the usage of the tail -f command without blocking the terminal.

Using bat (Bonus)

bat is described as "A cat(1) clone with syntax highlighting and Git integration."

The bat command is a modern replacement for the cat command. It has all of the features of cat, plus syntax highlighting and Git integration.

To install it on your system follow their instructions here.

As you can see above the output of the bat command is looking much better. Try it yourself, you won't be disappointed.

Searching for data

Have you ever had to search for a specific line, word, or piece of text in a large file? If so, you know how tedious and time-consuming it can be to read through the file line by line.

That's where the grep command comes in. It is a powerful command-line tool that can be used to search for a specific pattern of text in a file. It's a lifesaver when you need to find something quickly and easily.

Let's see it in action!

  • Searching a word Deployment in a file kubernetes.yaml that has 85 lines.

As you can see above, it's easy to search for a word in a file using the grep command. But did you know that by default, grep only matches the word if it's spelt exactly the same way, including capitalization? This is fine if you know the word exactly, but what if you're not sure if it's capitalized correctly?

That's where the -i option comes in. The -i option tells grep to ignore the case when matching words. So, if you're searching for the word "hello", grep will also match the words "Hello" and "HELLO". This can be a very helpful option if you're not sure how a word is capitalized.

  • Sometimes, you may want to know the line number of the word that matches. To do this, you can use the -n option. As you can see below.

  • To count the number of occurrences of a word, use the -c option.

  • To search for the exact word, you can use the -w option which stands for "word match." This option tells grep to only match the exact word that you specify, and not any other words that contain that same sequence of letters.

For example, I just want to search image in a file called kubernetes.yaml as you can see below.

Let's see what happens if we try to search word image without using the -w option.

As you expect, this will match all words starting with image.

This is just the tip of the iceberg. Explore all of these commands on your own to see what they can do. You may be surprised at how much you can accomplish with them.

I didn't explain writing to files in this post because most people use a text editor like Vim, Nano, Emacs or a graphical editor to do this. I will discuss writing to files in a separate blog post.

I hope you found this article informative and helpful. If you have any feedback, please feel free to share it in the comments below. Thank you for reading!

Stay tuned for the next part of the Master Linux series!

Β