Find and Replace with sed in Directory and Sub Directories in Linux
Last Updated :
02 Jan, 2023
sed stands for stream editor. It is a basic utility command in Linux that has basic functionality like inserting, deleting, finding, replacing, and searching text strings in a file. But sed is most commonly used for its substitution function i.e find and replace.
Syntax
sed OPTIONS... [script] [input_file]
e.g.
sed "s/abc/ABC/g" abc.txt
Consider the above example,
- "s/abc/ABC/g" - It is the [script] which specifies what actions are to be performed.
- 's/..' ->> tells that it is the substitution operation.
- 's/abc/..' ->> "abc" will be the word after the substitution operation.
- 's/abc/ABC/..' ->> "ABC" is the string to be substituted with "abc".
- 's/abc/ABC/g' ->> "g" means global. It specifies that all the matching occurrences should be substituted on the given line rather than the first occurrence only.
2. abc.txt - It is the [input_file], where the name of the file which is needed to be searched is specified.
Output
Find and Replace with sed in Directory and Sub Directories in Linux
Scenario
To demonstrate the example, we created a directory namely, sed_article, which contains a .txt file (file1.txt) and two subdirectories, subdir1 and subdir2, each of which contains one .txt file (file2.txt and file3.txt).
Also, the content of all the .txt files i.e. file1.txt, file2.txt, and file3.txt is kept same for the simplicity.
However, when we need to Find and Replace within directories and subdirectories, we cannot use the sed command alone. It needs to be accompanied by a few other commands, which would include grep, xargs, and the pipe (|) delimiter command.
- grep -It is a basic Linux utility command which searches for a matching string and then returns the entire line if a match is found.
- xargs - It is abbreviated as "extended arguments". It builds and executes commands from the standard inputs.
- pipe (|) delimiter - It is a utility command which is used to link to output and input of two other commands. It will provide the output generated from the previous command to the next command as input.
The command for Find and Replace with sed in Directory and Sub Directories in Linux is as below:
grep -rl 'bugsyy' "sed_article/" | xargs sed -i 's/bugsyy/BUGSYY/g'
Explanation :
Basically, -r with grep is used to search for the string recursively, and -l is tag is used for listing the files. But here both the tags will be used together as we need a list of files as output which will be further used as input to the sed command using pipe ( | ) delimiter.
Thus, the grep command with -rl tag search for the string "bugsyy" recursively into the specified directory and then lists the file in which the match is found.
The output for just grep command with tag -rl is as follows :
grep -rl 'bugsyy' "sed_article/"
Now the output from the above command i.e. the list of files that includes the string will be given as input to the sed command using pipe ( | ) delimiter, and the xargs will convert it to the standard input values so as to successfully execute the command.
In the above terminal, we first displayed/checked the contents of file1.txt before running the command for finding and replacing the string. And, after the execution, we displayed/checked the contents of file1.txt once more to confirm the changes. So the changes were successful
Recursively, the string "bugsyy" was successfully changed to "BUGSYY" in other subdirectories where the match was found.
Conclusion :
So this is how we can find and replace with sed in Directory and Sub Directories in Linux. In the above methodology, we used the sed command along with grep, xargs, and pipe delimiter ( | ).
Similar Reads
How to Recursively Grep all Directories and Subdirectories in Linux
The grep command is one of the most frequently used utilities in Linux for searching through text files. It allows users to search for specific strings within files and directories, offering a wide range of options to customize the search process. In this article, weâll explore how to use the recurs
5 min read
Finding Files With SUID and SGID Permissions in Linux
SUID(Set-user Identification) and SGID(Set-group identification) are two special permissions that can be set on executable files, and These permissions allow the file being executed to be executed with the privileges of the owner or the group. SUID: It is special file permission for executable files
3 min read
Shell Script To Show Names of All Sub-Directories Present in Current Directory
In this given program, we are required to write a shell script to print the list of all the sub-directories present in the current directory. Currently, when we are running this program, the terminal is opened at the root directory location and hence, we are getting a list of sub-directories present
2 min read
mindepth and maxdepth in Linux find() command for limiting search to a specific directory.
How to limit search a specified directory in Linux? There is a command in Linux to search for files in a directory hierarchy known as 'find'. It searches the directory tree rooted at each given starting-point by evaluating the given expression from left to right, according to the rules of precedence
5 min read
How to Find Hidden Web Directories with Dirsearch
Dirsearch tool is a Python language-based tool, which is command-line only. Dirsearch lights when it comes to recursive scanning, so for every directory it identifies, it will go back through and crawl the directory for some additional directories. Dirsearch tool is an advanced command-line tool des
3 min read
How to Exclude Certain Paths With the find Command in Linux
The find command in Linux is a very useful command line utility that helps us in finding files and directories. In our daily life, there are many tasks in which we require files that may be located deep inside the system, finding them manually would be a tedious task. Thus, the find command comes in
3 min read
How to Count Files in Directory Recursively in Linux
When exploring directories on your server, you may have come across folders with a large number of files in them. You might wish to know how many files exist in a certain directory or across many directories at times. To put it another way, you wish to count the number of files saved in a directory
5 min read
Find all the Files in a Directory with .txt Extension in Python
Directory traversal is a common operation performed by file locator in Operating Systems. The operating system offers elaborate methods for streamlining the search process and the choice to search for a selected filename/extension. This article will teach you how to find all the files in a directory
5 min read
Renaming All Files and Directory Names to Lowercase in Linux
Here we will see how to rename all files and directory names to lowercase in Linux. We can easily do it with some basic commands in Linux. The below command can be used to view the contents of your directory. $ find [directory_name] -depth Here 'priyanshu' directory which has some sub-files/folders,
1 min read
How to Copy a File to Multiple Directories in Linux
In this article, we will discuss how to copy a file to multiple directories in Linux. xargs, find, tee, shell loop, and parallel are the commands that can help you to copy a File to Multiple Directories. Suppose we have a file with the name "xyz.txt" and we want to copy it into 3 directories with th
5 min read