Let's find a lost file.

Yesterday, after a beer and some hours spent with a friend, I decided to transform this blog in a Linux tutorial one. But it won't be your usual "How to" Linux site.

I'm selfish. Everybody is in some kind of way. My way is making my problems dissappear and then sharing (or bragging) how I managed to resolve them.
That being said, let's see what this is about.

A few days ago, someone commented on a video that I had uploaded on YouTube a few years ago. In 2010, exactly. In that time, YouTube didn't allow me to upload more than 10 minutes of footage, and I remembered that that video was aproximatively 15 minutes long. I also thought that it was recorded with my iPod, so naturally, I manually searched through the folder in which I stored my iPod videos. There were only 12 of them and none was the one that I looked for. I almost gave up, but then something struck my mind: let's use Linux to find that video if it exists! And so did I.

I knew nothing about that lost file, except the date it was uploaded on YouTube. No title, the filename wasn't helping me, etc. I then clearly remembered that I recorded it in the same day in which I uploaded it. So let's use that single infomation (the date) to search what files were created on 20 february 2010.
How would I do that? I told myself. Let's use ls -Rl to recursively list all files in the folder where I keep my pictures and videos. Then let's grep "feb.*20.*2010" through the list to find them. Also, let's restrict that search only to videos, not only to the specified date. Since the file wasn't recorded with my iPod, I thought of all the devices that I had and could record a video, to find the file format. At that time I had only 2 devices that could film. All of them were recording in mp4, so of course, I restricted the search to *mp4 files.
All put together, the command looked like this:

ls -Rl /path/to/folder | grep "month.\*day.\*year.\*file_extension"

And then I thought: Could one use find to search through his files only knowing the creation/modification date? The answer is... (of course) yes.
Let's see and explain how can you use find to find files by a given date and extension.
The command you want to use looks like this:

find -iname "*file_extension" -newermt YYYY-MM-DD ! -newermt YYYY-MM-DD

It's obviously that the find command searches through files.
find -iname "filename" searches through files, with a given name, and the search is case-insensitive. For example find -iname "*mp4" will return all files that end in mp4, mP4, Mp4 or MP4, or are named mp4, mP4, Mp4 or MP4. "*" matches 0 or more characters. For now on will refer to them as "video files".
find -iname "*mp4" -newermt 2010-02-20 returns all the video files that were modified and/or created on 20 february 2010, until the time of running the command.
So we are very close to find that lost file. But I don't want find to list all the files newer than 20 february 2010. This translates in find -iname "*mp4" -newermt 2010-02-20 ! -newermt 2010-02-21. Basically, the exclamation mark (!) tells find to negate the argument -newermt 2010-02-21 which means show all the files from 21 february 2010 until today.
Put together:

find -iname "*mp4" -newermt 2010-02-20 ! -newermt 2010-02-21

So what are you telling find is this:

Dude, find me all the files that are named or end in mp4, mP4, Mp4 or MP4, are newer than 20 february 2010, but not newer than 21 february 2010.

This is pretty damn cool, isn't it?
This is my kind of selfish. I'll firstly do the things for me and then post them on this blog. Bumberry Pi will transform in a blog where I will post all my problems that I encounter while using Linux and how I got rid of that problem. Well, they're not really problems, but you get the point.

In the next post I will be teaching you how to use make to make yourself a sandwich.

Comments !

blogroll

social