Remember to think

I’m not a fan of mindlessly copy-pasting stuff. I understand sometimes there is time-pressure and you just want your stuff to work. I think however, that learning how to use documentation is probably faster than searching the internet for other people’s solutions and mindlessly copy pasting them.

I’m not saying the internet is a bad place, some people actually take their time to be very complete in their answers. But let’s be fair, you don’t visit these solutions to fully read them do you? You just want that piece of code or that single option you need.

How to actually learn something

If you really want to learn, copy pasting should be the last thing on your mind. It’s a bit like troubleshooting. It’s better to start at the lowest level and work your way up in complexity.

Let’s look at an example

The problem

You want to find the files in a linux directory that are older than one week.

The not so good solution

A quick google search later you are looking at some stackexchange answer. You quickly scan it and find the solution you were looking for:

find /my/folder/ -type f -mtime +15

What did you really learn from this? Do you now know how the find command works? Do you know what the -type flag does? What about -mtime +15?

The truth is, depending on your interpretation the answer might not even be entirely correct.

The better solution

Let’s look at the alternative. Google does not yet exist, or you are lacking an internet connection. How would you solve the problem? Let’s say you haven’t heard of the find command.

apropos (man -k) to the rescue!

$ man -k "search for file"
find (1)             - search for files in a directory hierarchy

The other answers you need are in the man page for find. In the man pages you can search in by using /<search term>. Searching for the word day quickly reveals the daystart option:

-daystart
  Measure  times  (for  -amin,  -atime,  -cmin, -ctime, -mmin, and -mtime) from the beginning of today rather than  from  24  hours ago. This  option only affects tests which appear later on the command line.

When you take a better look at the options listed near daystart it looks like -mtime is what you need:

-mtime n
  File's data was last modified n * 24 hours ago.  See the  comments for -atime to understand how rounding affects the interpretation of file modification times.

Searching for delete (/delete) also reveals (depending on the version of find you have) the delete option: -delete

The SYNOPSIS section quickly shows you how to use the find command:

SYNOPSIS
    find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]

In man-pages it’s also never a bad idea to check the EXAMPLES section:

EXAMPLES
       find /tmp -name core -type f -print | xargs /bin/rm -f

       Find  files  named core in or below the directory /tmp and delete them.
       Note that this will work incorrectly if there are  any  filenames  con‐
       taining newlines, single or double quotes, or spaces.

       find /tmp -name core -type f -print0 | xargs -0 /bin/rm -f

       Find  files  named core in or below the directory /tmp and delete them,
       processing filenames in such a way that file or  directory  names  con‐
       taining  single or double quotes, spaces or newlines are correctly han‐
       dled.  The -name test comes before the -type test  in  order  to  avoid
       having to call stat(2) on every file.

       find . -type f -exec file '{}' \;

       Runs  `file'  on  every file in or below the current directory.  Notice
       that the braces are enclosed in single quote marks to protect them from
       interpretation as shell script punctuation.  The semicolon is similarly
       protected by the use of a backslash, though single  quotes  could  have
       been used in that case also.

Now we can combine everything we learned!

# It's always a good idea to check before taking action ;-)
find /tmp -type f -daystart -mtime +8 -print
# Option 1
find /tmp -type f -daystart -mtime +8 -exec /bin/rm -f '{}' \;
# Option 2
find /tmp -type f -daystart -mtime +8 -print | xargs /bin/rm -f
# Option 3 (best option for speed if you find version supports it)
find /tmp -type f -daystart -mtime +8 -delete

Read the documentation if you want to learn

Don’t be lazy, read documentation, you will learn from it!