txt3rob
Posts: 365
Joined: Sat Aug 11, 2012 3:45 pm
Contact: Website

bash: if file

Tue Sep 03, 2013 9:25 am

Is there any way i could use following with locate instead? or any way of finding files if the exsist's in sub dirs

i need a way to scan over an external hard drive and do actions based if the file is found or not.

Code: Select all

if [ -f $file ];
then
echo file found
else
file not found
The Raspberry Pi Hell Guy - Random Ramblings to assist me and others.
http://raspberrypihell.blogspot.com
My Github - http:/www.github.com/random-robbie/
http://www.smspi.co.uk - send free uk sms via your raspberry pi from here

User avatar
PeterO
Posts: 5357
Joined: Sun Jul 22, 2012 4:14 pm

Re: bash: if file

Tue Sep 03, 2013 9:37 am

The "find" command may help. "man find" and look at the -exec option.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
redhawk
Posts: 3465
Joined: Sun Mar 04, 2012 2:13 pm
Location: ::1

Re: bash: if file

Tue Sep 03, 2013 10:11 am

Providing you don't have folders / directories with the same name as your search pattern then the following should work.
#!/bin/bash
path="/"
pattern="[email protected]"
result=`find "$path" | awk '{ gsub(/\x2f/,"\n"); print }' | grep -x "$pattern"`
if [ -z "$result" ]
then
echo not found
else
echo found
fi
The variable path is the starting point for recursive file and directory scanning.
The variable pattern contains the search pattern (currently set to [email protected] the command line string)
The awk stuff replaces the / symbol with a new line this is required for grep to perform an exact string comparison.

Richard S.

txt3rob
Posts: 365
Joined: Sat Aug 11, 2012 3:45 pm
Contact: Website

Re: bash: if file

Tue Sep 03, 2013 10:25 am

that's the thing the files and folders will have the same name.

so i am after a pdf or word doc or a folder with the name of 10-1234

normally on the ext hdd they could be stored in a location like below.

/var/www/nasdrive/10/100-199/10-1234/
or they could be
/var/www/nasdrive/random-folder/another-random-folder/10-1234.doc

unfortunately they are not stored in the best format
The Raspberry Pi Hell Guy - Random Ramblings to assist me and others.
http://raspberrypihell.blogspot.com
My Github - http:/www.github.com/random-robbie/
http://www.smspi.co.uk - send free uk sms via your raspberry pi from here

User avatar
redhawk
Posts: 3465
Joined: Sun Mar 04, 2012 2:13 pm
Location: ::1

Re: bash: if file

Tue Sep 03, 2013 10:51 am

Well not quite the same they do end in .doc and .pdf the folder doesn't have a file extension like i.e. /mnt/media/hdd/stuff.doc/work/work_01.doc so there shouldn't be a problem.
A better method would involve saving your search results to temporary file, and then reading this back line by line and tested for file status.

Richard S.

User avatar
PeterO
Posts: 5357
Joined: Sun Jul 22, 2012 4:14 pm

Re: bash: if file

Tue Sep 03, 2013 3:47 pm

Using "find" can select files and ignore directories even if they have the same name....

find . -type f -name 10-1234* <<< finds files
find . -type d -name 10-1234* <<<< finds directories

But your example suggests that files have .doc or .pdf extensions so there is no name clashes.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
jojopi
Posts: 3115
Joined: Tue Oct 11, 2011 8:38 pm

Re: bash: if file

Tue Sep 03, 2013 4:00 pm

redhawk wrote:Providing you don't have folders / directories with the same name as your search pattern then the following should work.
The only reason for that caveat is that you intentionally split the output on slashes, destroying the context. It also means that, even if you get a match, you have no idea what its complete pathname was.

I do not see any situation where the method has advantage over the more obvious:

Code: Select all

find "$path" -name "$pattern"

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: bash: if file

Tue Sep 03, 2013 4:19 pm

...or if you wanted to get really fancy you could do:

Code: Select all

find /some/path \( \( -type d -name "10-1234" \) -o \( -type f -a \( -name "10-1234.doc" -o -name "10-1234.pdf" \) \) \) -print
which will only display folders named "10-1234" or files named "10-1234.doc" or "10-1234.pdf" :ugeek:
(yes, all the back-slashes and brackets are necessary!)

txt3rob
Posts: 365
Joined: Sat Aug 11, 2012 3:45 pm
Contact: Website

Re: bash: if file

Wed Sep 04, 2013 7:50 am

right once updatedb command has finished im going to look on the drive for a file hidden away and then see which i can use to find the files i need.
The Raspberry Pi Hell Guy - Random Ramblings to assist me and others.
http://raspberrypihell.blogspot.com
My Github - http:/www.github.com/random-robbie/
http://www.smspi.co.uk - send free uk sms via your raspberry pi from here

txt3rob
Posts: 365
Joined: Sat Aug 11, 2012 3:45 pm
Contact: Website

Re: bash: if file

Wed Sep 04, 2013 8:02 am

ok so running updatedb has caused the exthdd to drop off yay!
time to reboot and use the web end to see whats where.
The Raspberry Pi Hell Guy - Random Ramblings to assist me and others.
http://raspberrypihell.blogspot.com
My Github - http:/www.github.com/random-robbie/
http://www.smspi.co.uk - send free uk sms via your raspberry pi from here

User avatar
RaTTuS
Posts: 10519
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: bash: if file

Wed Sep 04, 2013 8:12 am

txt3rob wrote:ok so running updatedb has caused the exthdd to drop off yay!
time to reboot and use the web end to see whats where.
you have got a powered usb hub for the HD ?
or the HD is powered via a plug
?
what does
dmesg
show when it drops off
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

txt3rob
Posts: 365
Joined: Sat Aug 11, 2012 3:45 pm
Contact: Website

Re: bash: if file

Wed Sep 04, 2013 8:23 am

Code: Select all

find "$path" -name "$pattern"
does not rapidly bring up anything as i have 3 folders with a set name and it seems to be taking ages....although that could be just crappy nas and pi low cpu
The Raspberry Pi Hell Guy - Random Ramblings to assist me and others.
http://raspberrypihell.blogspot.com
My Github - http:/www.github.com/random-robbie/
http://www.smspi.co.uk - send free uk sms via your raspberry pi from here

scrapheap
Posts: 20
Joined: Wed Feb 13, 2013 5:13 pm

Re: bash: if file

Wed Sep 04, 2013 10:44 am

Find doesn't use locate's database of files, it will actually hit the filesystem. This gives you the advantage of it looking at the current state of the files and directories, but it does so at the cost of time (especially when checking a large file system over a USB connection).

If you want get close to the speed of locate but still want to be sure that the entry currently exists,then you could use locate with its -e option to get it to check it exists (though it will still miss any files created since its database was last updated). You can also use the -b option to get locate to only check the base name of the entries (Still will pick up matching directories but only once rather than for every entry under that directory). You can also use the --regex option of locate to match a regular expression and so limit it to only those entries that end with your pattern, which included the .pdf or .doc file extensions.

Code: Select all

locate --regex -be "10-1234\.(pdf|doc)$"

txt3rob
Posts: 365
Joined: Sat Aug 11, 2012 3:45 pm
Contact: Website

Re: bash: if file

Wed Sep 04, 2013 11:52 am

i've ran find to delete all empty dirs i will try your suggestion shortly hopefully that works wonders
The Raspberry Pi Hell Guy - Random Ramblings to assist me and others.
http://raspberrypihell.blogspot.com
My Github - http:/www.github.com/random-robbie/
http://www.smspi.co.uk - send free uk sms via your raspberry pi from here

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: bash: if file

Wed Sep 04, 2013 12:29 pm

txt3rob wrote:i've ran find to delete all empty dirs i will try your suggestion shortly hopefully that works wonders
It should go without saying, but you need to be very careful when using 'find' to delete things - one small slip-up and you could easily delete everything!

txt3rob
Posts: 365
Joined: Sat Aug 11, 2012 3:45 pm
Contact: Website

Re: bash: if file

Wed Sep 04, 2013 12:38 pm

Code: Select all

find . -empty -type d -delete
The Raspberry Pi Hell Guy - Random Ramblings to assist me and others.
http://raspberrypihell.blogspot.com
My Github - http:/www.github.com/random-robbie/
http://www.smspi.co.uk - send free uk sms via your raspberry pi from here

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: bash: if file

Fri Sep 06, 2013 1:42 am

txt3rob wrote:

Code: Select all

find . -empty -type d -delete
Ah, cool :) There's so many options to 'find' it feels like I learn something new nearly every time I use it :D

I was about to suggest using -depth to search depth-first, but I see the man-page says

Code: Select all

       -depth Process each directory's contents before the  directory  itself.
              The -delete action also implies -depth.


txt3rob
Posts: 365
Joined: Sat Aug 11, 2012 3:45 pm
Contact: Website

Re: bash: if file

Fri Sep 06, 2013 12:27 pm

i currently have the drive connected to my pc at work at the mo

got a program to remove all empty dirs it's found...... 240k empty dirs!
The Raspberry Pi Hell Guy - Random Ramblings to assist me and others.
http://raspberrypihell.blogspot.com
My Github - http:/www.github.com/random-robbie/
http://www.smspi.co.uk - send free uk sms via your raspberry pi from here

txt3rob
Posts: 365
Joined: Sat Aug 11, 2012 3:45 pm
Contact: Website

Re: bash: if file

Mon Sep 09, 2013 10:30 am

redhawk wrote:Providing you don't have folders / directories with the same name as your search pattern then the following should work.
#!/bin/bash
path="/"
pattern="[email protected]"
result=`find "$path" | awk '{ gsub(/\x2f/,"\n"); print }' | grep -x "$pattern"`
if [ -z "$result" ]
then
echo not found
else
echo found
fi
The variable path is the starting point for recursive file and directory scanning.
The variable pattern contains the search pattern (currently set to [email protected] the command line string)
The awk stuff replaces the / symbol with a new line this is required for grep to perform an exact string comparison.

Richard S.
tried this with just a text file didn't find the results but if i did sudo find / -name 10-123123.txt it returned what i needed
The Raspberry Pi Hell Guy - Random Ramblings to assist me and others.
http://raspberrypihell.blogspot.com
My Github - http:/www.github.com/random-robbie/
http://www.smspi.co.uk - send free uk sms via your raspberry pi from here

Return to “Other programming languages”