This post is a lose collection of useful applications of sed.


The following commands are equivalent:

The -n flag makes sed print only the matching lines.

Replacing Pathnames

The following command replaces the placeholder CURRENT_WORKING_DIR with the current working directory inside file.txt:

 Replacing in a whole Directory Tree

We can use find to filter all files in a directory (tree) and then hand these files to sed. The following command prints out all true files (not directories) and lets sed replace the term et cetera with etc.

While we could also use the -exec flag of find this separation of concerns makes it easy to dry-run the modifications by exchanging the sed command. The flag -n reduces the output to the matches only and the command p triggers the printing of matching lines.


  • [1] Colorful tutorial by grymoire

A list of useful commands involving the find tool.

Empty directories

Identify empty directories below the current directory

Identify empty directories that have do not start with “.git”:

See also: here