18.01.2020 Views

Working with Linux

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

This will execute the grep command on all the files returned by find. Let's also search inside the file

using vim, so that we verify the result is correct. As you can see, the text "manager" appears in this

file. You don't have to worry about {} \;, it's just standard -exec syntax.

Moving on with the practical examples, let's say you have a folder where you want to remove all the

files modified in the last 100 days. We can see our default_app folder contains such files. If we

combine find with rm like so:

find default_app -mtime -100 -exec rm -rf {} \;

We can do a quick cleanup. Find can be used for smart backups. For example, if we were to backup

all json files in the project we would combine find with the cpio backup utility using a pipe and a

standard output redirection:

find . -name "*.json" | cpio -o > backup.cpio

We can see that this command has created a backup.cpio file, of type cpio archive.

Now this could probably have been written with -exec also, but it's critical you understand that pipes

can also be used in this type of scenario, together with redirects.

When doing reports, you may have to count the number of lines written:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!