DEV Community

Cover image for Essential Linux Command For Developers
joshua_jebaraj
joshua_jebaraj

Posted on

Essential Linux Command For Developers

Introduction

In this tutorial, I am going to share basic Linux commands that we can use to edit the text file. One can ask why we should use the Linux tools where we can use IDE which is more powerful than Linux tools.

  • In servers, you don't have GUI in that case you have to work with the CLI
  • You can with CLI text editor like vim or nano but working with CLI tools makes you more efficient
  • Makes you look cool in front of your friends

Outcomes

By end of the article you will understand the basic linux cli tools to work with the text file

Printing the content

There are ton of tools to print the content of the file lets see some of the common and popular cli tools

Cat

cat command used to print the content of the file

Usage

$ cat file-name 
Enter fullscreen mode Exit fullscreen mode

There are some more flag that you can use along with cat to make cat more powerful

$ cat -n file-name
Enter fullscreen mode Exit fullscreen mode

The above command will print the content of the file with line number

tac

tac command is the same as a cat but prints the content in the reverse order

Usage

$ tac file-name
Enter fullscreen mode Exit fullscreen mode

head

Imagine you have 100 lines of code you want to see the beginning of the file In this case head command comes in hand

Usage

$ head <file-name>
Enter fullscreen mode Exit fullscreen mode

If you want to print up to a particular number of lines you can use the head command with n flag

$ head -n 5 file-name
Enter fullscreen mode Exit fullscreen mode

The above command prints. the first 5 line of the file

Tail

Tail command used to print the last part of the file

$ tail file-name
Enter fullscreen mode Exit fullscreen mode

Similar to head you can combine with the n flag

$ tail -n 5 file-name
Enter fullscreen mode Exit fullscreen mode

prints the last 5 lines of the file

more

more command is similar to the cat

Usage

$ more <file-name>
Enter fullscreen mode Exit fullscreen mode

To show only 10 lines per page you can use the below command

$ more -10 <file-name>
Enter fullscreen mode Exit fullscreen mode

The main disadvantage of the more command is you can't scroll up

less

less is similar to the more.But it's more efficient because because it doesn't load the whole file in the go

Usage

$ less <file-name>
Enter fullscreen mode Exit fullscreen mode

Searching

To search for the particular string in the file we can use grep

Usage

$ grep <string> <file-name>
Enter fullscreen mode Exit fullscreen mode

We can combine with the -i flag to ignore the case

$ grep -i <string> <file>
Enter fullscreen mode Exit fullscreen mode

Find and Replace

To find and replace the string in the file we can use sed command

sed -i '/s/<text-to-be-searched>/<text-to-be-replaced>/' filename
Enter fullscreen mode Exit fullscreen mode

To replace all occurrences

sed -i '/s/<text-to-be-searched>/<text-to-be-replaced>/g' filename
Enter fullscreen mode Exit fullscreen mode

Conclusion

In this article, I have covered basic Linux CLI tools that you can use to edit the text file . If you have any more feedback or have any question let me know via Twitter

Top comments (0)