Shell Stuff: Easy File Cleanup

Posted by Ryan Himmelwright on Sat, Jan 5, 2019
Tags linux, shell
World War I Memorial Park, North Attleboro, MA

Applications can leave their junk files all over the place. While I appreciate that all of the .swp, .retry, and "conflict" files are there to help me when things go wrong… sometimes I just want to clean up my file system. So… here is a simple string of commands I often use to declutter my files.

DISCLAIMER: I know there are MANY ways to accomplish this. The method described in this post is to share one efficient solution I use, that might help someone that currently knows zero.

The commands

First, lets quickly meet the commands we will be using:


find is a classic UNIX command that searches for files in a directory hierarchy. By default, it writes out the file path for each file/directory that it finds.


➜  tree
├── dirA
│   ├── file3
│   ├── file4
│   └── file5
├── dirB
│   └── file6
├── file1
└── file2

2 directories, 6 files

➜  find .


Another classic. Basically, grep searches for a pattern in each file provided. In addition to files, it can search text passed through a pipe (this is important for our use, but more on that later).


➜  cat file1
This is a fake file
with a few lines of content.

However, I want search for something
without opening it...

Secret: 12345

I wonder if I will be able to get it...

➜  grep Secret file1
Secret: 12345

Command Substitution

Lastly, command substitution is taking one command, and using it’s output as part of another command. Traditionally, this was done by calling the substitution command `inside backticks`, but it is now preferred to use $(COMMAND) instead of backticks.


➜ echo I am at: `pwd`
I am at: /tmp/demo

or (preferred):

➜ echo I am at: $(pwd)
I am at: /tmp/demo


An unix pipe (|) directs the output of one command, to be used as the input for another command. Pipes can be used to chain together several commands, forming a pipeline.


The output of ls can be fed as input to wc (word count) to create a pipeline command that returns the number of files/directories in the current directory.

➜ ls
dirA  dirB  file1  file2

➜ ls | wc -l

Putting It All Together

Now that we know all the parts, how does it all fit together? One particular shell chain I find convenient is pairing find and grep to recursively get all the paths of a particular file type, and then use it in a command substitution to pass that result on to another command (such as rm).


This is the combination I use to clean up my directories. While working on writing ansible playbooks, I can generate a few *.retry files, as well as some *.swp files from editing in vim.


➜ find . | grep .retry          ## Find *.retry files

➜ rm $(find . | grep .retry)    ## Delete *.retry files

➜ find . | grep .retry          ## Check that they were deleted


That’s it. A small post for a simple but powerful command line set. If you haven’t used this team of commands before, give it a try sometime! Have fun!

Next Post:
Prev Post:

Replacing a Drive in My ZFS Mirror Configuring Pass, the Standard Unix Password Manager
comments powered by Disqus