0
votes

My conditional works properly when the dirs exist, but if they don't, it seems to execute both then and else statements (is that the correct term?).

script.sh

#!/bin/bash
if [[ $(find path/to/dir/*[^thisdir] -type d -maxdepth 0) ]]
  then
    find path/to/dir/*[^thisdir] -type d -maxdepth 0 -exec mv {} new/location \;
    echo "Huzzah!"
  else
    echo "hey hey hey"
fi

prompt
For the first call, the dirs are there; in the second, they've been moved from the first call.

$ sh script.sh
Huzzah!
$ sh script.sh
find: path/to/dir/*[^thisdir]: No such file or directory
hey hey hey

How can I fix this?

tried suggestion(s)

if [[ -d $(path/to/dir/*[^thisdir]) ]]
  then
    find path/to/dir/*[^thisdir] -type d -maxdepth 0 -exec mv {} statamic-1.3-personal/admin/themes \;
    echo "Huzzah!"
  else
    echo "hey hey hey"
fi

result

$ sh script.sh
script.sh: line 1: path/to/dir/one_of_the_dirs_to_be_moved: is a directory
hey hey hey
4
You tried suggestion was near right, but need to keep out $(), and have to reach ...dir/!(thisdir) instead of *[^thisdr]. But this would work only if path/to/dir/*[^thisdir] match exactly one entry. Take a look at my answer...F. Hauri

4 Answers

2
votes

There seem to be some errors:

First, the pattern path/to/dir/*[^thisdir] is interpreted in bash in the same manner than path/to/dir/*[^dihstr] mean *all filename ending by d, i, h, s, t or r.

Than if you are searching for something in this dir (path/to/dir) but not on path/to/dir/thisdir, and not on a nth subdir, you could bannish find and write:

Edit: There was an error on my sample too: [ -e $var ] was wrong.

declare -a files=( path/to/dir/!(thisdir) )
if [ -e $files ] ;then
    mv -t newlocation "${files[@]}"
    echo "Huzzah!"
else
    echo "hey hey hey"
fi

If you need find for searching in subirs, please give us samples and/or more descriptions.

1
votes

Your error is probably occurring at if [[ $(find path/to/dir/*[^thisdir] -type d -maxdepth 0) ]] and then it goes to else because find errors out.

find wants its directory parameter to exist. Based on what you are trying to do you should probably consider

$(find path/to/dir/ -name "appropriate name pattern" -type d -maxdepth 1)

Also, I'd consider using actual logical function in if. See this for file conditionals.

0
votes

Try adding a #!/bin/bash on the first line to ensure that it is bash that is executing your script, as recommended by this post:

Why is both the if and else executed?

0
votes

The OP wishes to move all files excluding thisdir to a new location.

A solution using find would be to exclude thisdir using find's functionality, rather than by using bash's shell expansion:

#!/bin/bash
if [[ $(find path/to/directory/* -maxdepth 0 -type d -not -name 'thisdir') ]]
    then
        find path/to/directory/* -maxdepth 0 -type d -not -name 'thisdir' -exec mv {} new/location \;
        echo "Huzzah!"
    else
        echo "hey hey hey"
fi

This has been tested, and works under bash version 4.2.39, and GNU findutils v4.5.10.