find command with wildcard directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find command with wildcard directory
# 1  
Old 06-22-2011
find command with wildcard directory

I want to look if there is any file inside a specific directory which was modified before 2 days.

I wrote the find command, but the problem is there is one directory and that is a random directory generated by unix, so not sure on how to code for that on the find command.
Code:
find /Production/ST/st*/Outbound/Prod/PROD-*/bcfa0dbbcf17f28c768d1d34da6b48a4/PGP -name '*.*' -mtime +2
find /Production/ST/st*/Outbound/Prod/PROD-*/63acf2caf91bc136cb9bcce8a85c7fa8/PGP -name '*.*' -mtime +2

the folders bcfa0dbbcf17f28c768d1d34da6b48a4 and 63acf2caf91bc136cb9bcce8a85c7fa8 are random and generated by unix.

there are several hundreds of folders like this, so I want a generic soultion to code for the random directory

any help would be appreciated as this is urgent.

Last edited by Franklin52; 06-22-2011 at 05:29 PM.. Reason: Please use code tags
# 2  
Old 06-22-2011
I doubt UNIX generates them, I think some application's probably doing so.

"*.*" is a DOS thing, what it amounts to for find isn't "find all files" but "find all files or folders with . in their name". If you want to find all files, leave off -name and try -type f (to limit it to files, by default it prints files and folders alike)

What is your system?

How about:
Code:
# Find all directories in .../PROD-* which are exactly 32 chars long
find /Production/ST/st*/Outbound/Prod/PROD-* -type d -name '????????????????????????????????' -print -name '*' -prune
while read DIR
do
        find "${DIR}/PGP" -type f -mtime +2
done

The -name '*' prune tells it to not recurse deeper into those folders, which is the job of the second find.

Last edited by Corona688; 06-22-2011 at 05:41 PM.. Reason: fixed typos
# 3  
Old 06-22-2011
Quote:
Originally Posted by Corona688
I doubt UNIX generates them, I think some application's probably doing so.

"*.*" is a DOS thing, what it amounts to for find isn't "find all files" but "find all files or folders with . in their name". If you want to find all files, leave off -name and try -type f (to limit it to files, by default it prints files and folders alike)

What is your system?

How about:
Code:
# Find all directories in .../PROD-* which are exactly 32 chars long
find /Production/ST/st*/Outbound/Prod/PROD-* -type d -name '????????????????????????????????' -print -name '*' -prune
while read DIR
do
        find "${DIR}/PGP" -type f -mtime +2
done

The -name '*' prune tells it to not recurse deeper into those folders, which is the job of the second find.
yes yours should work and I also found an other way and changed the code as suggested by you.

Code:
find /Production/ST/st*/Outbound/Prod/PROD-*/[abcdefghijklmnopqrstuvwxyz]*/PGP -type f -mtime +2 -exec rm -f '{}' \;

find /Production/ST/st*/Outbound/Prod/PROD-*/[0123456789]*/PGP -type f -mtime +2 -exec rm -f '{}' \;

so the above code will delete the files under folder PGP.

after deleting the files inside PGP I also want is to delete the PGP folder then remove the folder ??????????????????????????????? also.

I can delete the PGP folder using rmdir PGP

I want to know how to handle the 32char folder?
# 4  
Old 06-22-2011
Quote:
find /Production/ST/st*/Outbound/Prod/PROD-*/[abcdefghijklmnopqrstuvwxyz]*/PGP
If you'd only said "dozens" I'd have have suggested this in the first place, but this is dangerous when you have lots of files and folders. There's a limit to how many things one glob can find -- in some shells, no more than a page or two worth.

The version with two finds has no limit at all.

Quote:
after deleting the files inside PGP I also want is to delete the PGP folder then remove the folder ??????????????????????????????? also.
You're only deleting some of the files. If they're not empty, I doubt you really want them deleted.

You'll need to use the two-find version to do this anyway, since it'd be torturous to get the right directory in one find and use it only once. I'll use 0-9, a-f if that worked for you.

Code:
# Find all directories in .../PROD-* beginning with [0-9a-f]
find /Production/ST/st*/Outbound/Prod/PROD-* -type d -name '[0-9a-f]*' -print -name '*' -prune
while read DIR
do
        # Find files in "${DIR}/PGP", delete them if old enough
        find "${DIR}/PGP" -type f -mtime +2 | xargs -d '\n' echo rm

        # Remove these directories only if they're empty
        rmdir "${DIR}/PGP" && rmdir "${DIR}"
# the rmdir's will cause some error messages when they fail, redirect that to /dev/null
done 2> /dev/null

find | xargs rm will run 'rm file1 file2 file3 ...' where find -exec rm would run 'rm file1; rm file2 ; rm file3 ...' so xargs makes it much faster. The -d '\n' is to tell xargs to consider anything but newlines as part of the filename.

The 'echo' is just a test, to print filenames instead of deleting as a test. Remove it once you're sure it's doing what you wanted.

Last edited by Corona688; 06-22-2011 at 06:23 PM..
# 5  
Old 06-22-2011
Quote:
Originally Posted by Corona688
If you'd only said "dozens" I'd have have suggested this in the first place, but this is dangerous when you have lots of files and folders. There's a limit to how many things one glob can find -- in some shells, no more than a page or two worth.

The version with two finds has no limit at all.

You're only deleting some of the files. If they're not empty, I doubt you really want them deleted.

You'll need to use the two-find version to do this anyway, since it'd be torturous to get the right directory in one find and use it only once. I'll use 0-9, a-f if that worked for you.

Code:
# Find all directories in .../PROD-* beginning with [0-9a-f]
find /Production/ST/st*/Outbound/Prod/PROD-* -type d -name '[0-9a-f]*' -print -name '*' -prune
while read DIR
do
        # Find files in "${DIR}/PGP", delete them if old enough
        find "${DIR}/PGP" -type f -mtime +2 | xargs -d '\n' echo rm
 
        # Remove these directories only if they're empty
        rmdir "${DIR}/PGP" && rmdir "${DIR}"
# the rmdir's will cause some error messages when they fail, redirect that to /dev/null
done 2> /dev/null

find | xargs rm will run 'rm file1 file2 file3 ...' where find -exec rm would run 'rm file1; rm file2 ; rm file3 ...' so xargs makes it much faster. The -d '\n' is to tell xargs to consider anything but newlines as part of the filename.

The 'echo' is just a test, to print filenames instead of deleting as a test. Remove it once you're sure it's doing what you wanted.
can you tell me a bit more about two finds?

so in my case do I have to give it as 0-9,a-z as it could be anything between 0-9 and a-z as the first letter of the folder. is there any restriction on the # of directories I can search using this wildcard?

also won't the rmdir "${DIR}" delete the full structure? I just need only till the 32 char folder deleted and the rest should be intact.

P.S:
I just confirmed that it will be 32 chars and it will be consistent? so can i use the ??? approach? would that be better?
# 6  
Old 06-22-2011
Quote:
Originally Posted by srini0603
can you tell me a bit more about two finds?
I think I forgot a pipe. No wonder it was puzzling you.

Code:
# Find and print all directories in .../PROD-* beginning with [0-9a-f]
find /Production/ST/st*/Outbound/Prod/PROD-* -type d -name '[0-9a-f]*' -print -name '*' -prune |
# Read lines like /Production/ST/.../bcfa0dbbcf17f28c768d1d34da6b48a4 into DIR, one-by-one
while read DIR
do
        # We feed that whole directory into another find, telling it to look
        # for files.  It prints them one by one, piped into xargs,
        # which uses them as arguments for rm.
        find "${DIR}/PGP" -type f -mtime +2 | xargs -d '\n' echo rm
 
        # Remove these directories only if they're empty
        rmdir "${DIR}/PGP" && rmdir "${DIR}"
# the rmdir's will cause some error messages when they fail, redirect that to /dev/null
done 2> /dev/null

If that didn't answer your question you'll need to ask about something specific.
Quote:
so in my case do I have to give it as 0-9,a-z as it could be anything between 0-9 and a-z as the first letter of the folder.
I don't think so, those look like hex: binary numbers encoded as ASCII digits 0-9 and a-f.
Quote:
is there any restriction on the # of directories I can search using this wildcard?
There's no limit on find -name '*', no, because it can process them one-by-one. When you expand * inside the shell itself, it has to find all of them at once, before find is even run, potentially running out of room.
Quote:
also won't the rmdir "${DIR}" delete the full structure? I just need only till the 32 char folder deleted and the rest should be intact.
"rmdir /path/to/folder" only removes 'folder'. And it won't even do that unless it's empty. So I try to remove /Production/ST/st*/Outbound/Prod/PROD-*/bcfa0dbbcf17f28c768d1d34da6b48a4/PGP, and if that succeeds, I try to remove /Production/ST/st*/Outbound/Prod/PROD-*/bcfa0dbbcf17f28c768d1d34da6b48a4 itself.

And as I said, don't remove the echo until you're sure it does what you want.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find and replace with wildcard

HI there, I am trying to find and replace with wildcard with data chr1 69511 69511 A G 1/1:0,34:791,78,0:78:34 0/1:55,60:1130,0,1513:99:116 1/1:0,28:630,63,0:63:28 0/1:0,34:626,57,0:57:34 To this chr1 69511 69511 A G homo hetero homo hetero Where I find and replace 0/1 with... (3 Replies)
Discussion started by: daashti
3 Replies

2. Shell Programming and Scripting

How-To Exclude Directory in find command

How can i tweak the below find command to exclude directory/s -> "/tmp/logs" find . -type f \( ! -name "*.log*" ! -name "*.jar*" \) -printNote: -path option/argument does not work with the version of find that i have. bash-3.2$ uname -a SunOS mymac 5.10 Generic_150400-26 sun4v sparc sun4v (7 Replies)
Discussion started by: mohtashims
7 Replies

3. Shell Programming and Scripting

Find out directory where command is located

so i have a script that i do not want copies of that script to be roaming around. i want that script to be in only one location on the filesystem, and whoever wants to use it should just link to it. any idea on how to exit from a script if it is detected that the running version is a copy and... (5 Replies)
Discussion started by: SkySmart
5 Replies

4. Shell Programming and Scripting

ksh(!93) for loop with wildcard and empty directory

I'm sure this is by design, but using something like for f in dir/* do echo $f done produces unexpected (to me) results if run against an empty directory. I'd have expected it to not execute the loop, but it actually calls it with f set to 'dir/*'. Now I know that I'm trying to protect... (2 Replies)
Discussion started by: spr00t
2 Replies

5. UNIX for Dummies Questions & Answers

find command to look for current directory only

i have this find command on my script as: for i in `find $vdir -name "$vfile" -mtime +$pday` the problem with this code is that the sub-directories are included on the search. how do i restrict the search to confine only on the current directory and ignore the sub-directories. please advise.... (7 Replies)
Discussion started by: wtolentino
7 Replies

6. Shell Programming and Scripting

Change directory with wildcard in FTP server

Hi , I am looking for a command to change directory in FTP server with wildcard specified. Basically this is what i am trying. localserver# ftp remoteserver ftp> ls 41000_42000 42000_43000 ftp> cd 41* 550 CWD failed. '41*' : no such file or directory. Could anyone please let me know... (6 Replies)
Discussion started by: thavamaniraja
6 Replies

7. Shell Programming and Scripting

WildCard serach for files in a directory

Hi i have a requirement to search for all files in a directory. the files will start as file1_*,file2_*,file3_* where the wild card character is a timestamp. so on a particular day i may get files like file1_1103120042 file1_1102010345 file2_1101093423 file3_1103120042... (3 Replies)
Discussion started by: Sgiri1
3 Replies

8. Shell Programming and Scripting

Wildcard in Cshell find command

The following command works fine in my cshell script: set Deliverables = `find . -name "eliverables" -print` The following command does not work: set LASFiles = `find . -name "*." -print` In the first example, when tested in an if statement, the script will continue whether a... (3 Replies)
Discussion started by: phudgens
3 Replies

9. UNIX for Dummies Questions & Answers

how to find a file named vijay in a directory using find command

I need to find whether there is a file named vijay is there or not in folder named "opt" .I tried "ls *|grep vijay" but it showed permission problem. so i need to use find command (6 Replies)
Discussion started by: amirthraj_12
6 Replies

10. UNIX for Dummies Questions & Answers

Find wildcard .shtml files in wildcard directories and removing them- How's it done?

I'm trying to figure out how to build a small shell script that will find old .shtml files in every /tgp/ directory on the server and delete them if they are older than 10 days... The structure of the paths are like this: /home/domains/www.domain2.com/tgp/ /home/domains/www.domain3.com/tgp/... (1 Reply)
Discussion started by: Neko
1 Replies
Login or Register to Ask a Question