Find command to exclude directories and setup alias or script?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Find command to exclude directories and setup alias or script?
# 1  
Old 06-24-2011
Question Find command to exclude directories and setup alias or script?

Hi,

Firstly - sorry for the duplicate my other post looked like i was posting a how to for people. But i am wanting some help :P

I want to search from / to find files and exclude my mounted ntfs drives.

I have found this thread (Which I can't post the URL to until i have 5 posts) it's from this site https://www.unix.com/unix-dummies-que...nd-syntax.html
QUESTION 1:

Is this still the best way to do it (the post is over 5 years old)?

QUESTION 2:

IF the above post is still the best way to do it I end up with the following command:

Code:
find / \( -wholename /mnt/C -prune \) -o \( -wholename /mnt/D  -prune \) -o \( -wholename /mnt/E -prune \) -o -name "TEST"  -print

Which I have configured the following alias for:

Code:
 alias findx='find / \( -wholename /mnt/C -prune \) -o \(  -wholename /mnt/D -prune \) -o \( -wholename /mnt/E -prune \)  -o'

Running

findx -name "TEST" -print

Works perfectly however, is there anyway I can have the alias setup to accept input like a script can or should I just put it in a script so it can accept parameters

for example

Code:
alias findx='find / \( -wholename /mnt/C -prune \) -o \(  -wholename /mnt/D -prune \) -o \( -wholename /mnt/E -prune \) -o -name  "%1" -print'

so I would be able to just run:

findx TEST

instead of having to put in -name "TEST" -print

If that's a no what would the easiest way to put this in a script, so i could run it that way?

QUESTION 3:

Again IF this is still the best way of doing it does this command

Code:
find / \( -wholename /mnt/C -prune \) -o \( -wholename /mnt/D  -prune \) -o \( -wholename /mnt/E -prune \) -o -name "TEST"  -print

Completely bypass the pruned directories or is it simply not printing anything from within those directories it finds? Because it seems to take an awful long time to search STILL even with those 3 drives excluded....

Thanks in advance for any assistance with this Smilie

Last edited by pludi; 06-24-2011 at 05:10 AM..
# 2  
Old 06-24-2011
maybe you can use -fstype for the prune?

you can script like:

test.sh:
Code:
#!/bin/bash
findx()
{
        find / \( -fstype proc -prune \) -o -name "$1" -print
}

echo "Find $1"
findx "$1"

---------- Post updated at 04:08 AM ---------- Previous update was at 03:52 AM ----------

oh you wanted an alias. you can in fact put functions in your .bashrc, aliases don't accept parameters.

Last edited by neutronscott; 06-24-2011 at 05:04 AM.. Reason: quotations around arguments
# 3  
Old 06-24-2011
Computer

Quote:
Originally Posted by neutronscott
maybe you can use -fstype for the prune?

you can script like:

test.sh:
Code:
#!/bin/bash
findx()
{
        find / \( -fstype proc -prune \) -o -name "$1" -print
}

echo "Find $1"
findx "$1"

---------- Post updated at 04:08 AM ---------- Previous update was at 03:52 AM ----------

oh you wanted an alias. you can in fact put functions in your .bashrc, aliases don't accept parameters.
Wow that's a brilliant idea i didn't even think of that Smilie I'll have a look at the fstype options now.

So you mean I could just paste this whole code into my bashrc and I could then run

findx TEST and it would work?

---------- Post updated at 03:44 AM ---------- Previous update was at 03:30 AM ----------

Quote:
Originally Posted by neutronscott
maybe you can use -fstype for the prune?
oh you wanted an alias. you can in fact put functions in your .bashrc, aliases don't accept parameters.

Lol man thanks for the hint i went hunting through find for filesystem and I found this option -mount - it skips mounted filesystems with this option.

so my function now (thanks to you) in bashrc looks like this:

Code:
findx()
{
        find / -mount -name "$1"
}

How much simpler does that look Smilie
# 4  
Old 06-24-2011
Quote:
Originally Posted by mightymouse2045
Lol man thanks for the hint i went hunting through find for filesystem and I found this option -mount - it skips mounted filesystems with this option.
I thought that would be what you wanted, but you did say "ntfs mounts" Smilie

while you can't change the arguments around with an alias, now your arguments are all at the end so you can use alias again if you'd like.

Code:
alias findx='find / -mount -name'

If you want speed you may look into 'locate'/'updatedb'
# 5  
Old 06-25-2011
Quote:
Originally Posted by neutronscott
I thought that would be what you wanted, but you did say "ntfs mounts" Smilie

while you can't change the arguments around with an alias, now your arguments are all at the end so you can use alias again if you'd like.

Code:
alias findx='find / -mount -name'

If you want speed you may look into 'locate'/'updatedb'
Thanks for that I am looking at those now also
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need Tar Ball command to exclude directories

Hi, uname -a SunOS mymac 5.11 11.2 sun4u sparc SUNW,SPARC-Enterprise I need to tar a folder /tmp/moht but do not want these three folders to be included in the tar file -> savejpg, bmpsave and imgsave I tried --exclude, -path, -not options but it says bad option Can you help me with... (3 Replies)
Discussion started by: mohtashims
3 Replies

2. Shell Programming and Scripting

Exclude directories in FIND command

Can you please help tweak the below command to exclude all directories with the name "logs" and "tmp" find . -type f \( ! -name "*.tar*" ! -name "*.bkp*" \) -exec /usr/xpg4/bin/grep -i "user_1" /dev/null {} + >result.out bash-3.2$ uname -a SunOS mymac 5.10 Generic_150400-26 sun4v sparc sun4v... (9 Replies)
Discussion started by: mohtashims
9 Replies

3. AIX

Exclude Directories in my tar command

Hi, im having some issues after i execute the next command: tar -cvf /varios/restore/test.tar -X /jfma/test1/excludefile /jfma | gzip -c > /varios/restore/test.tar.gz this creates the desired "test.tar.gz" file, but whe i try to open it it says "tar: 0511-164 There is a media read or write... (6 Replies)
Discussion started by: blacksteel1988
6 Replies

4. UNIX for Dummies Questions & Answers

Using grep command to find the pattern of text in all directories and sub-directories.

Hi all, Using grep command, i want to find the pattern of text in all directories and sub-directories. e.g: if i want to search for a pattern named "parmeter", i used the command grep -i "param" ../* is this correct? (1 Reply)
Discussion started by: vinothrajan55
1 Replies

5. Shell Programming and Scripting

Perl exclude directories in command line

Hi, I use find command to list all the files in a directory and its sub-directories, but the problem is to exclude certain directories during search. Can i give the directory names in command line to skip them and search rest of the directories? For example i have directories: test ../test1... (1 Reply)
Discussion started by: nmattam
1 Replies

6. Shell Programming and Scripting

Help - Find command to exclude sub-directories

Hi Forum. I'm trying to write a script that finds and deletes files that are older than 300 days. The script will read a table that contains the following 3 columns: 1st col: Y means sub-directory scan; "N" means no subdirectory scan 2nd col: sub-directory location 3rd col: File prefix... (7 Replies)
Discussion started by: pchang
7 Replies

7. UNIX for Dummies Questions & Answers

How to Exclude multiple directories from find command?

Hi, Can some one help me how to exclude multiple directories using find command.. I have the directory structure below. /a/a1/b1 /a/c1/c2 /a/d1/d2/d3 I want to exlcude a1,c2and d3 from the above using find,can some one suggest pls.. thanks in advance... Use code tags... (1 Reply)
Discussion started by: jagadish_gaddam
1 Replies

8. Shell Programming and Scripting

Find but exclude directories

Hello, I have a line in my script to find the files changed in the last 24 hours. It is as below: find /home/hary -type f -mtime -1 I now want to exclude a directory named "/home/hary/temp/cache" from the above find command. How do I add it to my script? Any help is appreciated. ... (9 Replies)
Discussion started by: tadi18
9 Replies

9. UNIX for Dummies Questions & Answers

find command to exclude directories

Howdy I have this directory structure ... eep eepaptest eepfatest eepgltest eep.old eeppoptest ehf ehfaptest ehfgltest ehp ehpgltest I want to find files in these directories, but I want to exclude eep, ehf & ehp. Cany anyone help with the correct command ?? (1 Reply)
Discussion started by: SmurfGGM
1 Replies
Login or Register to Ask a Question