Do I need an Array for this?

Hi all,

I am trying to create a script that will read directories over 180 days so I can tar them and back them up. I am stuck on this part.

 
FIND_OLD_DIR='find . -type d -mtime +180' 
DEL_DIRECTORIES="expr substr $FIND_OLD_DIR 3 8"
$DEL_DIRECTORIES

When I run this I get "expr: syntax error"

If i replace
FIND_OLD_DIR='find . -type d -mtime +180' with
FIND_OLD_DIR='WELCOME'

then it works and displays "LCOM"

I am assuming it is because FIND_OLD_DIR is holding multiple values from my multiple directories.

My question is how do I make this work. Do I need an array? Do I write it out to a file and then read the file back in one at a time? What is the easiest way. I am new to shell scripting.

Thanks in advanced

Ralph

FIND_OLD_DIR is holding the literal string "find . -type d -mtime +180". You used single quotes, not backticks.

You don't need backticks or eval, though. Which is good because
1) Backticks can only hold a limited number of results
2) Eval is dangerous. If someone had a file named `rm -Rf ~/*` they'd have a real rotten day.

You also want find to stop when it matches a dir and match no deeper, otherwise it may find more folders inside the folder.

# find .         Search inside .
# '!' -name '.'  ...for things not named .
# -type d        ...which are directories
# -mtime +180    ...that are 180+ days old
# -prune         ...and if they match all the above, don't search deeper
find . '!' -name '.' -type d -mtime +180 -prune |
# "echo a b c d | xargs touch" is equivalent to "touch 'a' 'b' 'c' 'd'".
# The -I{} forces xargs consider each line as one and only one argument.
        xargs -I{} tar -cf - > file.tar

If you find thousands and thousands of directories, tar might be run more than once because there'd be too many filenames to fit in one call to tar. Hunting for folders instead of files should definitely reduce the number of things that need to be fed into tar, though.

---------- Post updated at 03:04 PM ---------- Previous update was at 02:45 PM ----------

If you're in Linux, you can avoid this limitation by feeding the filenames into tar with -T:

# find .         Search inside .
# '!' -name '.'  ...for things not named .
# -type d        ...which are directories
# -mtime +180    ...that are 180+ days old
# -prune         ...and if they match all the above, don't search deeper
find . '!' -name '.' -type d -mtime +180 -prune |
# Create file.tar, reading filenames from standard input
        tar -cf file.tar -T -

Thanks Corona,

I an trying just the find command that you gave me but I am gettin the following error:

FIND_OLD_DIR="find . '!' -name '.' -type d -mtime +180 -prune"

Error:
find: `\'!\'': No such file or directory

If I type it out in the command line like this:
find . ! -name . -type d -mtime +180 -prune

it works fine...any suggestions??

It does nothing of the sort when I copy-paste it into my shell, what did you actually do?

You still haven't put it in backticks, by the way, that's double-quotes. backticks are the same key as the tilde key minus the shift.

Solving simple problems with eval is like mowing the lawn with a flamethrower -- silly, difficult, dangerous, often touches things you didn't mean to, and the result is almost never exactly what you wanted. eval is to be avoided if you possibly can.

My suggestion is to use the code I gave you in the manner it's intended. Copy-paste it into your script, with comments or without, and it ought to work.

# Linux version
find . '!' -name '.' -type d -mtime +180 -prune | tar -cf file.tar -T -
# Other systems
find . '!' -name '.' -type d -mtime +180 -prune | xargs -I{} tar -cf file.tar

If my code doesn't do what you need, please explain what you're trying to do and I'll show you better ways to accomplish that than eval + backticks.

Thank you Corona,

I finally figured out what you meant with the backticks and it worked perfectly.

Thanks for your help!!!

Rlph

1 Like