The following is checking for any folder in the path that is more than 1 day old and matches the name criteria. If found, the folder is then moved to the 'trash' folder.

find /Users/myname/Desktop/backups/websites/testwebsite.co.uk -maxdepth 1 -type d -mtime +1 -name '[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]' -exec mv {} /Users/myname/Desktop/backups/trash \;

I need to modify the foldername, to include '_renamed' at the end.

I thought that I could use the curly braces from mv, but now realise this takes the full folder path as well.

find /Users/myname/Desktop/backups/websites/testwebsite.co.uk -maxdepth 1 -type d -mtime +1 -name '[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]' -exec mv {} /Users/myname/Desktop/backups/trash/{}_renamed \;

Is there any way that I can modify the folder path without copying the path as well?


Yes, find works with pathnames, not filenames.

There are two immediate solutions:

  1. You could extract the filename portion of the pathname, and use that in the destination pathname used by mv.

    find /Users/myname/Desktop/backups/websites/testwebsite.co.uk \
        -maxdepth 1 -type d -mtime +1 \
        -name '[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]' \
        -exec sh -c 'mv "$1" "/Users/myname/Desktop/backups/trash/${1##*/}_renamed"' sh {} ';'

    Here, ${1##*/} is the same as $( basename "$1" ), and $1 is the pathname given to the sh -c script by find.

  2. You could use -execdir in place of -exec if your find implementation supports it. With -execdir, the given command is executed in the parent folder of the found item, and {} will be either the basename of the item (with BSD find) or the basename prefixed by ./ (with GNU find).

    find /Users/myname/Desktop/backups/websites/testwebsite.co.uk \
        -maxdepth 1 -type d -mtime +1 \
        -name '[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]' \
        -execdir mv {} /Users/myname/Desktop/backups/trash/{}_renamed ';'


  • Thank you for your explanations! I had not heard of 'execdir'. This was perfect. – ccdavies Jun 17 '18 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.