I thought this would be more of a one liner to be honest.

Pretty simple in notion: using find on Mac OS X, locate files that meet a criteria, in my case: all file and directories in a directory: find ~/Downloads +time1000s

That finds what I need, then I run a conditional, if a dir exists, delete it, if not, create it: mkdir -p ~/.Trash/safe-to-delte-these-old-files

This means I need to add print0 to my find as files will have spaces, and I want to move, not copy but either way, there is a source, and a destination, and I am getting stuck:

https://gist.github.com/5c0tt/5a2c1fd39ae99d6fca05 Line 27 and 26 seem to cause me issues, I am stuck.

Suggestions on everyone from line 1 to the end. I am trying to hard to do this with POSIX in mind, but i can't even get variables to work then.

It seems BSD does not work the exact same way as other shells and what arguments they accept, which is why I am trying to be more POSIX, as I was told it should run anywhere then.

Thank you.

1

There are 1 answers

0
masseyb On

Took a glance at your git link, a couple of remarks if I may (still a noob tbh so may be irrelevant) :

dir_to_clean="/Users/my_username/Downloads" should probably be dir_to_clean="/Users/$current_user/Downloads" unless you actually have a literal /Users/my_username/Downloads folder.

Instead of cd'ing into your users directory and since you have hardcoded the path to that directory, you could use pushd & popd instead in order to build a stack of directories.

To answer your question, to capture files with spaces in the name for removal you could use something like :

find $dir_to_clean -not -name . +time1000s -exec mv -- {} ~/.Trash/

Could be something like this :

# Initialise variables, user, source to clean, destination
user=$(whoami);
src="/Users/$user/Downloads";
dest="~/.Trash/safe_to_delete";

# Move to directory to clean, not necessary, but if you really want to
pushd $src;

# Check if the destination exists, if not, create it 
if [ ! -d $dest ]; then
    mkdir -p $dest;
else
    echo "destination already exists";
fi

# Find all the file to move then move them
find . -not -name . +time1000s -exec mv -- {} "$dest/" \;

# Return to previous working directory 
popd;

pushd the $src directory onto the stack. find all the files in the now current directory ., -not -name . in order to avoid trying to trash the . & .. folders, -- tells find to stop parsing command line options (in cas your file/folder is named i.e. -myfile.txt), exec mv all of the arguments to $dest. popd the still current directory off of the stack. man find (/exec) for more details.

Note : It is also interesting to know that the difference of execution time between the -exec option versus results being piped into xargs can and will often be quite dramatic. Also, if your are actually sure that those files are safe_to_delete, then delete them instead of moving them (-exec rm -- {} $dest/). With all that said, you were correct, one liner.

Further reading : http://www.softpanorama.org/Tools/Find/using_exec_option_and_xargs_in_find.shtml