Find a lost file [Solved]

General programming chat and advice for beginners

7 posts
by DavidMS » Mon Jun 04, 2012 7:53 am
Can some help me with an 'understandable' (for a very new Linux user) description of how to find a file I have put somewhere on my SD card.

I suspect I should be using the "find" command but to be frank having read the help text I a basically more confused than when I started. Essentially I just want to perform a search for '*.jpg' files across the whole SD card so I can locate the files location.

Thanks
Post edit Gert: changed the subject text so it is easier to find for others.
Posts: 65
Joined: Fri May 25, 2012 8:48 pm
by spurious » Mon Jun 04, 2012 7:56 am
Code: Select all
apt-get install mlocate

once installed
Code: Select all
updatedb

then you can look for files
Code: Select all
locate myfile.jpg
Posts: 343
Joined: Mon Nov 21, 2011 9:29 pm
by andyl » Mon Jun 04, 2012 9:00 am
The problem with locate is that you are likely to need to run updatedb every time. I guess most people aren't keeping their raspis on all day in a server fashion.

It is better to just use find.

Find all the jpgs anywhere on the system
Code: Select all
 find / -name *.jpg

Find all the jpgs anywhere in the home directory and below
Code: Select all
 find /home -name *.jpg

If you are one of those people who sometimes uses capitals then use -iname

Find all the files owned by the user andyl in /home and below
Code: Select all
find /home -user andyl

Find all the files modified more than 6 days ago
Code: Select all
 find / -mtime +6

Plus find can do a lot more - including running a command against each of these files.
Posts: 265
Joined: Tue Jan 10, 2012 11:05 am
by spurious » Mon Jun 04, 2012 9:03 am
I agree find is more powerful, but I use nether very often and can never remember the syntax for find.. plus I'm lazy... ;)
Posts: 343
Joined: Mon Nov 21, 2011 9:29 pm
by DavidMS » Mon Jun 04, 2012 9:07 am
Thanks for this - have found the lost cat picture thanks - daughter happy
Posts: 65
Joined: Fri May 25, 2012 8:48 pm
by Gert van Loo » Sun Jun 17, 2012 9:16 pm
I thought you always have to put quotes around the wild cards:
find / -name "*.jpg" -print
(I know , the -print is no longer required. It is an old left-over from my Unix days.
But I have typed that command soooo many time it just comes out automatically).
User avatar
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1988
Joined: Tue Aug 02, 2011 7:27 am
by andyl » Sun Jun 17, 2012 9:51 pm
Gert van Loo wrote:I thought you always have to put quotes around the wild cards:
find / -name "*.jpg" -print
(I know , the -print is no longer required. It is an old left-over from my Unix days.
But I have typed that command soooo many time it just comes out automatically).


Well to be absolutely correct then yes you are right - you will need to escape the glob pattern.

However if (and only if) the current directory doesn't contain anything matching the glob pattern then the entire pattern gets passed to find.

I guess I should really have added the quotes (or escaping) in my previous answer.
Posts: 265
Joined: Tue Jan 10, 2012 11:05 am