识别目录并使用bash中的find移动内容

时间:2016-02-24 13:27:32

标签: bash find

我想将一些特定目录的所有文件移动到另一个名为shp_all的目录。 所讨论的特定目录都包含其名称中包含“shp”的文件。 目录如下:

public class MainActivity extends Activity 
{
    CallNotifierService m_service;
    boolean isBound = false;

    private ServiceConnection m_serviceConnection = new ServiceConnection() 
    {
        @Override
        public void onServiceConnected(ComponentName className, IBinder service) 
        {
            m_service = ((CallNotifierService.MyBinder)service).getService();
            Toast.makeText(MainActivity.this, "Service Connected", Toast.LENGTH_LONG).show();
            isBound = true;
            Intent intent = new Intent(MainActivity.this, CallNotifierService.class);
            startService(intent);
        }

        @Override
        public void onServiceDisconnected(ComponentName className) 
        {
            Toast.makeText(MainActivity.this, "Service Dis-connected", Toast.LENGTH_LONG).show();
            m_service = null;
            isBound = false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent = new Intent(this, CallNotifierService.class);
        bindService(intent, m_serviceConnection, Context.BIND_AUTO_CREATE);
    }

    .
    .
    .

}

我设法找到一个方法来识别 -data --shp_all --d1 --d2 ... --dN 中包含名称为d1,...,dN的文件的所有目录:

"shp"

但是,我不知道如何正确使用 find . -name '*shp*' exec所有文件放在与输出相同的目录中,并将其移至{{ 1}}

以下是尝试过但无效的内容:

execdir

2 个答案:

答案 0 :(得分:2)

我相信有一种方法可以做到这一点(请记住上面Dave的建议,将目标目录移出find路径):

mv $(find . -name "*shp*" -printf "%h\n" | uniq)/* ../shp_all/

注意,这也将移动任何子目录。如果您真的只想要文件,则可以添加另一个级别以仅查找-type f

mv $(find $(find . -name "*shp*" -printf "%h\n" | uniq) -type f) ../shp_all/

答案 1 :(得分:1)

这里有一个小问题,因为您的目标路径./shp_all/位于您正在搜索的路径中。这会导致您遇到麻烦,因为在查找文件夹之前,文件可以移动到文件夹中 我建议在文件选择/移动期间将其移出 以下代码段应该在您上面描述的./data文件夹中运行。

mv ./shp_all ..
find . -name "*shp*" -exec mv {} ../shp_all/ \;

查找结尾的\;对于关闭-exec很重要,否则命令将无法执行。

希望有所帮助。
戴夫