将除1之外的所有目录移动到另一个文件夹(如果存在),将所有文件保留在后面

时间:2022-09-01 21:09:45

I'm trying to move some dirs from one location to another, but I need to leave one in place (all files will remain in place). I've tried several things, but nothing seems to work. I have tested the value of DIR_COUNT and it works as expected. However, when used in a conditional or case statement, it doesn't work as expected.

我正试图将一些目录从一个位置移动到另一个位置,但我需要保留一个位置(所有文件都将保留在原位)。我尝试过几件事,但似乎没什么用。我测试了DIR_COUNT的值,它按预期工作。但是,在条件语句或case语句中使用时,它无法按预期工作。

conditional

有条件的

#!/bin/bash
DIR_COUNT=$(find path/to/dir/*[^this_dir_stays_put] -type d -maxdepth 0 | wc -l)
echo $DIR_COUNT
if [[ $DIR_COUNT > 0 ]]
  then
    find path/to/dir/*[^this_dir_stays_put] -type d -maxdepth 0 -exec mv {} new/location \;
    echo "Moving dirs."
  else
    echo "No dirs to move."
fi

case

案件

#!/bin/bash
DIR_COUNT=$(find path/to/dir/*[^this_dir_stays_put] -type d -maxdepth 0 | wc -l)
echo $DIR_COUNT
case $DIR_COUNT in
  0)
    echo "No dirs to move."
  *)
    echo "Moving dirs."
    find path/to/dir/*[^this_dir_stays_put] -type d -maxdepth 0 -exec mv {} new/location \;;;
esac

With both versions of the code, everything is fine provided the directories to be moved exist, but if there aren't any to move, I have problems.

使用这两个版本的代码,一切都很好,只要存在要移动的目录,但如果没有任何移动,我就会遇到问题。

conditional

有条件的

$ sh script.sh
find: find path/to/dir/*[^this_dir_stays_put]: No such file or directory
0
No dirs to move.

case

案件

$ sh script.sh
find: find path/to/dir/*[^this_dir_stays_put]: No such file or directory
0
Moving dirs.
find: find path/to/dir/*[^this_dir_stays_put]: No such file or directory

2 个解决方案

#1


3  

Skip the conditional and case statement.

跳过条件和案例陈述。

find path/to/dir/*  \! -name 'this_dir_stays_put' -type d -maxdepth 0 \
   -exec mv {} new/location \;

#2


0  

I am assuming you have something like this:

我假设你有这样的事情:

dir_a
dir_b
dir_c
dir_d
dir_e

You want to move all those directories except dir_c.

您想要移动除dir_c之外的所有目录。

Sometimes the easiest way is to move all the directories to the new location, and then just move the one you want back. No?

有时最简单的方法是将所有目录移动到新位置,然后移动您想要的目录。没有?

Okay, if you use Kornshell it's pretty simple. If you use Bash, you need to first set the extglob option like this:

好的,如果你使用Kornshell,它非常简单。如果你使用Bash,你需要先设置如下的extglob选项:

$ shopt -s extglob

Now, you can use the extended globbing syntax to specify directory exceptions:

现在,您可以使用扩展的globbing语法来指定目录异常:

$ mv !(dir_c) $new_location

The !(dir_c) matches all files except dir_c. This works in Kornshell. It works in BASH, but only if you've first set extglob.

!(dir_c)匹配除dir_c之外的所有文件。这适用于Kornshell。它适用于BASH,但前提是你首先设置了extglob。

#1


3  

Skip the conditional and case statement.

跳过条件和案例陈述。

find path/to/dir/*  \! -name 'this_dir_stays_put' -type d -maxdepth 0 \
   -exec mv {} new/location \;

#2


0  

I am assuming you have something like this:

我假设你有这样的事情:

dir_a
dir_b
dir_c
dir_d
dir_e

You want to move all those directories except dir_c.

您想要移动除dir_c之外的所有目录。

Sometimes the easiest way is to move all the directories to the new location, and then just move the one you want back. No?

有时最简单的方法是将所有目录移动到新位置,然后移动您想要的目录。没有?

Okay, if you use Kornshell it's pretty simple. If you use Bash, you need to first set the extglob option like this:

好的,如果你使用Kornshell,它非常简单。如果你使用Bash,你需要先设置如下的extglob选项:

$ shopt -s extglob

Now, you can use the extended globbing syntax to specify directory exceptions:

现在,您可以使用扩展的globbing语法来指定目录异常:

$ mv !(dir_c) $new_location

The !(dir_c) matches all files except dir_c. This works in Kornshell. It works in BASH, but only if you've first set extglob.

!(dir_c)匹配除dir_c之外的所有文件。这适用于Kornshell。它适用于BASH,但前提是你首先设置了extglob。