Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to identify a folder and then decide whether to move it or not in bash

Tags:

linux

bash

I have a linux issue that I would like some help with...

I have an application that creates directories for me. I'd like to write a script that looks at these directories, checks to see whether the directory starts with a certain string of characters, if it does, leave it alone, if it doesn't then move it...

So, from a logical stance:

If directoryname begins with "ABC" then do nothing
else
move folder to sharedrive

This script will go in the cron.hourly folder so it runs automatically for me.

Any help is greatly appreciated!!!

like image 384
Stephen Melody Avatar asked Dec 01 '25 00:12

Stephen Melody


1 Answers

find . -mindepth 1 -maxdepth 1 -type d ! -name 'ABC*' -exec mv -i {} ./dest \;

If you can identify a simple pattern for the directories you don't want to move, you might even be able to use bash's extglob setting:

shopt -s extglob
mv !(ABC)*/ ./dest/
like image 81
kojiro Avatar answered Dec 02 '25 14:12

kojiro