Learning Linux- File & Text Manipulation

Beginner’s Guide to Learning Linux/Ubuntu

Mariam Manzoor
ILLUMINATION
4 min readOct 10, 2020

--

image courtesy author

While using Linux, one will often have to work in the terminal only. It is therefore important for a user to know some basic Linux Commands for file & text manipulation. In this article, we will look into such commands on Ubuntu distro.

If you are new to Linux, check out my article on Learning Linux- Basic Commands first.

Viewing a Text File

image courtesy author

To look into the contents of a text file, the command used is:

If the file is in the current directory, the following command may be used instead:

image courtesy author

In order to view the start of the file, use the command:

By default, it shows the first 10 lines of the file. To change the number of lines displayed -n flag is used as follows:

To view the end of a file, the command used is:

By default, it shows the last 10 lines of the file. To change the number of lines displayed -n flag is as follows:

Editing a Text File

image courtesy author

To modify the contents of a text file, a text editor is used. The easiest and common one being nano. The command used to open the file is:

Once the file is modified CTRL-O is used to save the changes, followed by CTRL-X to exit from the editor.

Searching a Word in File

image courtesy author

To search a word within a file type in terminal:

In order to search a word with all files in a directory, use the command:

As an example:

Input/Output Redirection

Using Redirection in Linux, users can change the standard input-output devices. Using Output Redirection instead of displaying the contents on the screen, text output can be saved in a file. Similarly, using Input Redirection the contents of a file can be written onto the screen or attached with another application like Email.

image courtesy author

The symbol used for Output Redirection is > ’. To write redirect output from screen to a file, the command used is

This will create a new file if it is already not present. In case the file is present, its contents would be erased and the new message will be overwritten. In order to append the new message into the file, use >> operator as follows:

The symbol used for Input Redirection is ‘ < ’ and can be used as follows:

To redirect errors the operator 2> is used. Example, if a user wants to save error messages in a program my program into the file error.txt, the following command will be used:

Conclusion

These were some basic Linux commands for File & Text manipulation. Practice is the only way a user can learn these commands efficiently. So do try file handling on your Linux machine, and share your learning experience in the comments.

--

--

Mariam Manzoor
ILLUMINATION

Hey Friends! I’m Mariam, a Science and Technology Enthusiast and I’m here to share my Ideas and Explorations in the field of AI and Robotics.