Home Linux Commandline Tips How to Remove Line of Text from Multiple Files in Linux

How to Remove Line of Text from Multiple Files in Linux

The Linux operating system never disappoints when it comes to file management tweaks. Think of any hurdle associated with file management and Linux will solve it. Consider this scenario.

Supposing you are a web/system admin or user that is handling various web-based projects simultaneously. Scanning through these project files, you might notice inconsistencies dues to unwanted line entries.

The direct approach to deleting these unwanted lines from the project files or log files will be to open them one by one and then delete them. However, what if you are dealing with hundreds of files that are all victims of these illegal line entries?

This article is here to investigate a possible solution to this problem.

Problem Statement

To make this article more practical and relatable, we are going to create some files (3 text files) and add a similar line of text in these files and afterward attempt to remove the line entries.

$ nano file1.txt
$ nano file2.txt
$ nano file3.txt
Create Files in Linux
Create Files in Linux

From these three created text files, we will seek to remove the line of text entry I should not hear present in all of them.

Combining and Using find, xargs, and sed Commands

The find command helps us a query and search for the possible existence of targeted files within a directory hierarchy. In this case, we will point the find command to the directory containing our grouped files and use it to retrieve all files with .txt extension.

The find command is then piped to the xargs command. This command will build and execute the find command results based on set parameters. In this case, the xargs command takes/mimics a file list of the .txt files retrieved by the find command.

Finally, the xargs command output is passed to the sed command where the retrieved text files are edited, filtered, and transformed into copies without the highlighted line of text.

The reference syntax for combining the above three commands looks like the following:

$ find /Directory/Path/To/Files -name "*.file_extension" -type f | xargs sed -i -e '/line of text to be removed/d'    

Our final command implementation for using these three terminal approaches is as follows:

$ find . -name "*.txt" -type f | xargs sed -i -e '/i should not be hear/d'

Let us use the cat command to confirm if the line of text highlighted in the above command execution was removed.

$ cat file1.txt
$ cat file2.txt
$ cat file3.txt
Confirm Text Removed from Files
Confirm Text Removed from Files

We have successfully removed the unwanted line of text from the three files. This approach is effective when dealing with a countless number of files with a common line entry that needs immediate deletion.

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.