2

I want to check if a directory is not empty then move it to another directory else move on to the next directory.

I know it possible but getting my "if's" and "then's" mixed up. This is what I have done so far:

    if [ "$(ls -A /Volumes/Editorial\ Data/Photography/Digital\ Photographer/Content\ King\ DP)" ]; then
       echo "Not Empty"
    else
       echo "Empty"
    fi
    mv -n /Volumes/Editorial\ Data/Photography/Digital\ Photographer/Content\ King\ DP/* /Volumes/SAN\ CK1/Content\ King/2015/Magazines/Digital\ Photographer

The purpose of this script is to in sequence check 20+ separate directories for contents, each has a corresponding directory on another disk and if it finds anything in the source directory it moves it to the destination directory.

Once I get this one right I need to string 20 or more directories to check and move in a single script.

4 Answers4

2

Assuming you have a list of known directories, I would suggest an approach like this:

for dir in path/one path/two path/three; do
    contents=( "$dir"/* )
    if [[ ${#contents[@]} -gt 0 ]]; then
        mv "${contents[@]}" /destination/dir
    fi
done

A glob is used to fill an array with every path inside the directory. If the length of the array is greater than zero, all the files are moved to the destination.

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
  • Thanks for the quick response, i should heave said each folder i check has a corresponding folder that the contents needs to be moved to as a posed one folder to receive all. – Imagine Admin May 21 '15 at 15:28
  • what is the mapping between the input directory and output? Are the names related or something? You should edit your question to explain. – Tom Fenech May 21 '15 at 15:41
1

Using the beautiful answer by mweerden in Checking from shell script if a directory contains files, you can say:

if [ -n "$(find aaa/ -maxdepth 0 -empty)" ]; then
   mv -n /Folder/Source/* /Folder/Destination
fi

Or even shorter:

[ -n "$(find aaa/ -maxdepth 0 -empty)" ] && mv -n /Folder/Source/* /Folder/Destination

[ -n "$variable" ] checks if the length of $variable is nonzero.

Explanation

find ... -empty just outputs the name of the given directory if it does not contain anything.

$ mkdir aaa/
$ find aaa/ -maxdepth 0 -empty
aaa/

Now it won't print anything:

$ touch aaa/bbb
$ find aaa/ -maxdepth 0 -empty
$ 
Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • Worth mentioning that this relies on GNU find for `-maxdepth`. – Tom Fenech May 21 '15 at 15:11
  • Good point, @TomFenech . Updating to show that it can be done differently... in fact no, I cannot do it because it would become weird. I thought about doing `find dir/ -type f` but we may want to also check dirs within that directory, etc. – fedorqui May 21 '15 at 15:13
  • Thank you for your quick response and multiple solutions, do i replace aaa with the path to the folder to be check? and do i just repeat the command for all subsequent folders? – Imagine Admin May 21 '15 at 15:31
  • 1
    @ImagineAdmin since you are adding more info about your question in comments here and in the other answer, better update your question to show more context. – fedorqui May 21 '15 at 15:32
0

I see to ways to do this:

if [[ ! -z $(find $dir -type f -type d) ]]
then
    echo "Directory Not Empty. Move the files."
fi

This tests to see if any files are returned by the find command. Note I have to test the output to see if there are no files. The find will return an exit code of 0 even if nothing is found as long as the command is good.

and

if ! ls $dir/* > /dev/null 2>&1
then
    echo "Directory Not Empty. Move the files."
fi

Note in the second one, I don't use [ ... ] or [[ ... ]] tests. If there is nothing under $dir, the ls $dir/* will return a non-zero status, and my if statement will pick that up.

This should be much more efficient. I'm not shelling out to a new command, and ls should be faster than find if the purpose is to see if there are any entries at all under the directory. It'd be nice if I could use $dir/* directly without bothering with a command... I think you might be able to do something with shopt -s failglob which will exit in case of a glob expansion failure. Then, you might be able to use that output to test if you have any files in that directory.

David W.
  • 105,218
  • 39
  • 216
  • 337
0

Thanks for all the suggestion, with my very limited programming skills i have tried all the various permutations without success. I found that permission had a part to play in it so added the chown line but had to run that as root so added the line into the sudoers file so it sets the correct permission before it checks the folder for contents.

So far i have tried:

sudo chown -R admin /Volumes/Editorial\ Data/Photography/Digital\ Photographer/Content\ King\ DP if ! ls $(find /Volumes/Editorial\     Data/Photography/Digital\ Photographer/Content\ King\ DP/* > /dev/null 2>&1     then echo "Directory Not Empty. Move the files." mv -n /Volumes/Editorial\     Data/Photography/Digital\ Photographer/Content\ King\ DP/* /Volumes/SAN\     CK1/Content\ King/2015/Magazines/Digital\ Photographer fi 

as well as:

sudo chown -R admin /Volumes/Editorial\ Data/Photography/Digital\ Photographer/Content\ King\ DP if [ -n "$(find /Volumes/Editorial\ Data/Photography/Digital\ Photographer/Content\ King\ DP/ -maxdepth 1 -empty)" ]; then mv -n /Volumes/Editorial\ Data/Photography/Digital\ Photographer/Content\ King\ DP/* /Volumes/SAN\ CK1/Content\ King/2015/Magazines/Digital\ Photographer fi

but can't get it to work, i'll sure people are pulling their hair out looking at my shoddy coding but i know i'm close as the move command part works fine its just the after checking the folder its not then moving anything it finds.