[英]linux or unix shortcut command which can be used from and directory
[英]How to change directory structure in Linux/Unix from Command Line?
這是我要執行的操作:給定目錄“ XYZ”,我希望能夠以以下方式設置XYZ:一旦在其中創建了新的子目錄(“ ABC”),默認情況下該子目錄包含3個子目錄以及(“ 1”,“ 2”,“ 3”)。 例如:ls -la / ABC / XYZ /將顯示3個文件夾,而無需我手動創建這3個文件夾
捕獲“在XYZ中創建目錄ABC”事件時,使用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;
}
為了從命令行執行此操作,請安裝inotify-tools。
sudo apt-get install inotify-tools
然后您可以使用以下命令來監視XYZ目錄中的創建事件。
while ret=$(inotifywait -e create /tmp/XYZ); do mkdir /tmp/XYZ/{1,2,3}; done
在XYZ中創建任何目錄或文件后,將立即執行while塊中的命令。 在這種情況下,mkdir將創建更多目錄。 您可以根據需要在塊中添加更多檢查。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.