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