简体   繁体   English

如何从命令行更改Linux / Unix中的目录结构?

[英]How to change directory structure in Linux/Unix from Command Line?

Here is what I want to do: Given Directory "XYZ", I want to be able to setup XYZ in way that once there is new sub-directory ("ABC") created in it, by default that subdirectory contains 3 sub-directories as well ("1","2","3"). 这是我要执行的操作:给定目录“ XYZ”,我希望能够以以下方式设置XYZ:一旦在其中创建了新的子目录(“ ABC”),默认情况下该子目录包含3个子目录以及(“ 1”,“ 2”,“ 3”)。 Eg: ls -la /ABC/XYZ/ would display 3 folders without me creating those 3 folders manually 例如:ls -la / ABC / XYZ /将显示3个文件夹,而无需我手动创建这3个文件夹

use inotify to monitor filesystem events and execute relative operations when capture 'create driectory ABC in XYZ' event. 捕获“在XYZ中创建目录ABC”事件时,使用inotify监视文件系统事件并执行相关操作。 this is a sample from http://onestraw.net/essay/inotify/ 这是来自http://onestraw.net/essay/inotify/的示例

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/inotify.h>

#define MONITOR_PATH    "/var/onestraw/"
#define MONITOR_MASK    IN_CREATE | IN_DELETE | IN_ACCESS | IN_MODIFY

inline void _err(const char *str)
{
    perror(str);
    exit(1);
}

inline void inotify_loop(int fd)
{
    char buf[4096];
    size_t len;
    struct inotify_event *event;
    while (1) {
        len = read(fd, buf, sizeof(buf));
        if (len < 0) {
            _err("read() failed");
        }
        for (event = (struct inotify_event *)buf;
             (char *)event < &buf[len];
             event =
             (struct inotify_event *)((char *)event + sizeof(*event) +
                          event->len)) {
            if (event->mask & IN_CREATE)
                printf("add %s\n", event->name);
            if (event->mask & IN_DELETE)
                printf("delete %s\n", event->name);
            if (event->mask & IN_ACCESS)
                printf("access %s\n", event->name);
            if (event->mask & IN_MODIFY)
                printf("modify %s\n", event->name);
        }
    }
}

int main(int argc, char *argv[])
{
    int fd;

    if ((fd = inotify_init()) < 0) {
        _err("inotify_init() failed");
    }
    //if (inotify_add_watch(fd, argv[1], MONITOR_MASK) < 0) {
    if (inotify_add_watch(fd, MONITOR_PATH, MONITOR_MASK) < 0) {
        _err("inotify_add_watch() failed");
    }

    inotify_loop(fd);
    return 0;
}

In order to do that from the command line, install the inotify-tools. 为了从命令行执行此操作,请安装inotify-tools。

 sudo apt-get install inotify-tools

and then you can use the following command to monitor the XYZ directory for create events. 然后您可以使用以下命令来监视XYZ目录中的创建事件。

 while ret=$(inotifywait -e create /tmp/XYZ); do   mkdir /tmp/XYZ/{1,2,3}; done

As soon as any directory or file is created in XYZ, the commands in the while block will execute. 在XYZ中创建任何目录或文件后,将立即执行while块中的命令。 mkdir in this case, creating further directories. 在这种情况下,mkdir将创建更多目录。 you can add further checks as per your requirement in the block. 您可以根据需要在块中添加更多检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM