LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   Need help with find command (http://www.linuxquestions.org/questions/linux-software-2/need-help-with-find-command-924743/)

dth4h 01-20-2012 02:51 AM

Need help with find command
 
I want to use the find command to well, find stuff (this much is obvious lol)
Let's say I have a directory named Folder, (when I run the find command I will be in that directory) and in that directory are a bunch of sub-directories. In those sub-directories are a whole bunch of stuff, but I just want to see all the files named VIDEO.
This will do that:
Code:

find -name VIDEO
But I have a problem, some of the VIDEO files I do not want to see. All the VIDEO files I do not want to see also have this file in the same sub-directory as it named DONOTLIST.

So how do I use find to find all the files named VIDEO under the Folder directory, but tell it not to look in any sub-directories that have the file DONOTLIST in it?

barunparichha 01-20-2012 03:13 AM

try this :

find . | grep VIDEO

deep27ak 01-20-2012 03:58 AM

try this

Code:

#find /folder -t file -name VIDEO
this will search inside folder for all files with name VIDEO

vinilvijayan 01-20-2012 07:30 AM

1. Get the list of directories with "DONOTLIST". Using "find . |grep DONOTLIST"
2. Give the directory names from point:1, <excludedir1>,excludedir2>,... with -prune switch and search for "VIDEO" keyword in the end.

find . -path './<excludedir1>' -prune -o -path './<excludedir2>' -prune -o \( -name VIDEO -print \)

Better use a small shell script to do this.

Cheers,
vinil

dth4h 01-20-2012 03:17 PM

Thanks vinilvijayan, that's what I wanted.

Nominal Animal 01-20-2012 03:22 PM

Because the DONOTLIST file is in the same directory as the VIDEO files you want to see, I don't know if there is a find option for this case. (Specifically, I don't think -prune helps here, except if all directories containing DONOTLIST is also supplied like vinilvijayan suggested.)

I do know that
Code:

find . -type d -print0 | bash -c 'while read -rd "" DIR ; do [ -f "$DIR/VIDEO" ] && [ ! -f "$DIR/DONOTLIST" ] && printf "%s\\n" "$DIR/VIDEO" ; done'
does work. It's probably not the shortest or most efficient way to go about it, though.

The idea is that find just searches for all directories. It uses the ASCII NUL, zero byte, as the separator, so that all file names are handled correctly. The Bash snippet reads in the directories. If the directory contains file VIDEO but not file DONOTLIST, then it prints the path to the VIDEO file.

If you want the DONOTLIST file to affect both the current directory and subdirectories, I'd use GNU awk, since it handles RS="\0";FS="/+" separators correctly. I'd construct an array of directories that contain the VIDEO file, and another of directories that contain the DONOTLIST file, both with trailing slashes, then remove the entries in the former array that begin with any entry in the latter. Finally, print the ones left over (adding the VIDEO to the directory path).

dth4h 01-20-2012 03:41 PM

Nominal Animal, you are a genius! This is exactly what I wanted! THANK YOU so much!!!

Rory Glenn Pascua 01-20-2012 03:44 PM

Find Command
 
I usually just do this:

find / -name <file you're looking for>

ex:

find / -name firefox


All times are GMT -5. The time now is 10:08 PM.