3

I'm writing a small bash script to archive directories off in a compressed format to another location when they are older than a certain age and am having some difficulty. Here's what I'm doing.

#!/bin/bash
# Archives completed CDP episodes to compressed storage. Will eventually expand to put these off-site as well.

InputDir="/home/wgant/stuff/"
OutputDir="/home/wgant/archived/"

find $InputDir$ -maxdepth 0 -mtime +1 -type d -exec echo $OutputDir${} \;

I'm a little green at bash, so I'm taking it slow. For the first step, I just want to dump a list of what the output files would be called. However, when I run the above, the filenames are right, but the full path is clearly being concatenated in, which I don't want. How do I just strip out the filename?

Obviously once this works, I'll put the calls in to create the tarballs and delete the directory in question.

1 Answer 1

3

The syntax is not quite right, this is better:

find $InputDir -maxdepth 0 -mtime +1 -type d -exec echo $OutputDir{} \;

To strip the prefix part of the paths, an easy way will be to cd into the directory before executing the find, like this:

InputDir="/home/wgant/stuff"
OutputDir="/home/wgant/archived"

(cd "$InputDir"; find . -maxdepth 0 -mtime +1 -type d -exec echo "$OutputDir"/{} \;)

Notice that I wrapped the last command within a (...). That is a subshell, the purpose of it is that the cd command is only effective inside it, the rest of the script will not be affected by it. This is important, because changing the working directory with cd is not recommended in scripting. Wrapping it within (...) makes it safe.

5
  • When I tested on the command line, cd'ing into the directory and using the "." instead of the full path still gave me a leading ./ in front of the filename. Is there some way to get rid of that? Also, is it reasonable when building a shell script to test the individual pieces on the console?
    – Will Gant
    Dec 26, 2016 at 20:47
  • If you don't have hidden files in that directory, then you can write find * instead of find . and then you won't have the ./ prefix. If you might have hidden files, let me know.
    – janos
    Dec 26, 2016 at 21:03
  • Not only it's reasonable to test piece by piece, I recommend it (and do it myself too).
    – janos
    Dec 26, 2016 at 21:04
  • That fixed it. Thank you! Now I can archive hundreds of gigs of stuff off of my drive!
    – Will Gant
    Dec 26, 2016 at 21:05
  • 1
    Whenever you use cd in a script, you should check to make sure it succeeded before continuing with the script, e.g. cd "$InputDir" || { echo "Error moving to $InputDir; exiting" >&2; exit 1; }. That way you can avoid having the rest of the script run in an inappropriate and possibly dangerous location (you wouldn't want it archiving off the wrong directory). But there's a trick here: since this section runs in a subshell, the exit only exits the subshell, not the main script. So you may have to check the exit status of the subshell, like (cd ... || { ... exit 1; }; ... ) || exit $?. Dec 27, 2016 at 3:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .