DEV Community

Cover image for Exploring File Contents with Common Commands in Linux
Bello Osagie
Bello Osagie

Posted on • Updated on • Originally published at blog.techstackmedia.com

Exploring File Contents with Common Commands in Linux

Exploring File Contents with Common Commands in Linux Cover

In Linux, there are several commands you can use to examine the contents of text files. Let's take a closer look at these commands, their functionalities, and how they can be used effectively.

You can use the following commands to look at the contents of text files:

  • head
  • tail
  • cat
  • less
  • echo
  • wc
  • grep

head

The head command displays the first part of a file. By default, it shows the first 10 lines, but you can specify a different number using the -n flag.

head <filename>
Enter fullscreen mode Exit fullscreen mode
head README.md
Enter fullscreen mode Exit fullscreen mode

If you want to specify a different number -n of lines to show, run the code in the format.

head -n <number of lines> <filename>
Enter fullscreen mode Exit fullscreen mode
head -n 6 README.md
Enter fullscreen mode Exit fullscreen mode

tail

The tail command prints the last 10 lines of a file. You can also customize the number of lines to be displayed by using the -n flag. Additionally, the -f flag allows you to continuously monitor changes made to a file, which is useful for log files.

tail <filename>
Enter fullscreen mode Exit fullscreen mode
tail README.md
Enter fullscreen mode Exit fullscreen mode

If you want to specify a different number -n of lines to show, run the code in the format

head -n <number of lines> <filename>
Enter fullscreen mode Exit fullscreen mode
tail -n 5 README.md
Enter fullscreen mode Exit fullscreen mode

cat

The cat command is versatile and can be used for multiple purposes. Primarily, it prints the content of a file to the standard output. It can also be used to concatenate files or create new ones.

Printing the content of a file:

cat <filename>
Enter fullscreen mode Exit fullscreen mode
cat README.md
Enter fullscreen mode Exit fullscreen mode

Concatenating multiple files into a new file:

cat doc.txt contribute.txt > combine.txt
Enter fullscreen mode Exit fullscreen mode

Creating a new file:

cat > newfile.txt
Enter fullscreen mode Exit fullscreen mode

less

The less command provides an interactive and user-friendly way to view file content. It displays file contents in a paginated manner, allowing you to scroll, search, and navigate easily.

less README.md
Enter fullscreen mode Exit fullscreen mode

Useful commands within less:

  • b: Scroll up one page
  • G: Go to the end of the file
  • g: Go to the beginning of the file
  • q: Quit the command

echo

The echo command prints the argument passed to it to the output.

echo 'Hello world'
Enter fullscreen mode Exit fullscreen mode

wc

The wc command, short for word count, provides information about a given input, such as the number of lines, words, and bytes in the content.

Options for wc:

-l: Prints the newline count
-m: Prints the character count
-c: Prints the byte count
-w: Prints the word count

For example:

wc -l README.md
Enter fullscreen mode Exit fullscreen mode

grep

The grep command is a powerful text manipulation tool that allows you to filter the content of a file based on a specific pattern. It displays only the lines that match the given pattern.

For instance, if you want to see all lines in a file that include the word "command," you can use grep in combination with cat:

cat README.md | grep command
Enter fullscreen mode Exit fullscreen mode

In this command, the output of cat README.md is piped (|) to grep command for further filtering.

These commands provide a solid foundation for examining and manipulating file content in Linux. With their flexibility and varied functionalities, you can efficiently navigate and process text files according to your needs.

Please note that the examples provided here are simplified, and there are many more options and use cases for each command. Further exploration and practice will enhance your understanding and proficiency in working with these commands.

Let me know if you have any questions or need further assistance!


References


Top comments (0)