Use xargs to mv a directory from find results into another directory

前端 未结 5 552
孤独总比滥情好
孤独总比滥情好 2020-12-23 20:54

I have the following command:

find . -type d -mtime 0 -exec mv {} /path/to/target-dir \\;

This will move the directory founded to another

5条回答
  •  礼貌的吻别
    2020-12-23 21:11

    find ./ -maxdepth 1 -name "some-dir" -type d -print0 | xargs -0r mv -t x/
    

    find: with option -print0, the output will end with '\0';

    xargs: with option -0, it will split args by '\0' but whitespace, -r means no-run-if-empty, so you will not get any errors if find didn't get any output. (The -r is a GNU extension.)

    I usually use this in scripts when I'm not sure if the target files exist or not.

提交回复
热议问题