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.
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:
# 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.
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.
# 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.
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?
I think I forgot a pipe. No wonder it was puzzling you.
# 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.
I don't think so, those look like hex: binary numbers encoded as ASCII digits 0-9 and a-f.
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.
"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.