【问题标题】:how to make a folder named as sth that we read before in a text file by linux terminal?如何通过linux终端在文本文件中创建一个名为sth的文件夹?
【发布时间】:2018-04-15 20:45:06
【问题描述】:

我有一个包含文件夹名称的文本文件。我想通过 Ubuntu 终端读取该文本文件的上下文并创建一个具有该名称的文件夹(写入 txt 文件)。我不知道该怎么做,我制作了一个名为“in.txt”的文件,它的上下文是“name”,我尝试了下面的c代码:

#include <stdio.h>
int main(){
FILE *fopen (const char in.txt, const char r+);
}

我应该在终端上写什么?

【问题讨论】:

    标签: linux ubuntu terminal


    【解决方案1】:

    设文本文件in.txt为:

    foldername1
    foldername2
    

    然后就可以创建脚本文件script.sh

    #!/usr/bin/env bash
    
    # $1 will be replaced with the first argument passed to the script:
    mkdir -p $(cat "$1")    
    

    在 shell 中运行脚本为:./script.sh in.txtin.txt 中指定的文件夹名称现在应该可用。这假定in.txt 中每行一个文件名,没有空格。

    【讨论】:

    • mkdir -p $(cat in.txt) 以防他们存储诸如 foldername1/test 之类的行
    【解决方案2】:

    假设您有一个名为 in.txt 的文本文件,其中包含

    Rambo
    Johnny
    Jacky
    Hulk
    

    接下来你需要从文件中逐字读取,为此你可以使用fscanf()。读取单词后,您需要使用该单词名称创建一个文件夹。创建文件夹命令是mkdir。接下来,您需要从文件中执行mkdir,因为您使用system()execlp()

    这是一个简单的 C 程序

    int main(){
            FILE *fp = fopen ("in.txt", "r");
            if(fp == NULL) {
                    /* write some message that file is not present */
                    return 0;
            }
            char buf[100];
            while(fscanf(fp,"%s",buf) > 0) {
                    /* buf contains each word of file */
                    /* now create that folder use execlp */
                    if(fork() ==0 )
                            execlp("/bin/mkdir","mkdir",buf,NULL); /* this will create folder with name available in file */
                    else
                            ;
            }
            fclose(fp);
            return 0;
    } 
    

    请注意,如果目录已存在,mkdir 将失败。 像gcc -Wall test.c一样编译并像./a.out一样执行它会创建文件夹。

    编辑:相同如果你想使用open()系统调用,你不会有任何系统调用逐字读取,所以你需要字符串操作。

    int main(int argc, char *argv[]) {
            int inputfd = open("in.txt",O_RDONLY);
            perror("open");
            if (inputfd == -1) {
                    exit(EXIT_FAILURE);
            }
            /* first find the size of file */
            int pos = lseek(inputfd,0,2);
            printf("pos = %d \n",pos);
            /* againg make inputfd to point to beginning */
            lseek(inputfd,0,0);
            /*allocate memory equal to size of file, not random 1024 bytes */
            char *buf = malloc(pos);
            read(inputfd,buf,pos);/* read will read whole file data at a time */
            /* you need to find the words from the buf, bcz buf contain whole data not one word */
            char cmd[50];/* buffer to store folder name */
            for(int row = 0,index = 0; buf[row]; row++) {
                    if(buf[row]!=' ' && buf[row]!='\n') {
                            cmd[index] = buf[row];
                            index++;
                            continue;
                    }
                    else {
                            cmd[index] = '\0';
                            index = 0;/* for next word, it should start from cmd[0] */
                            if(fork() == 0 )
                                    execlp("/bin/mkdir","mkdir",cmd,NULL);
                            else ;
                    }
            }
            close(inputfd);
            return 0;
    }
    

    【讨论】:

    • 是的,可以,如果我想对这个进程使用系统调用而不是 c 库,我该怎么办,我的意思是如何使用“int open(const char * filename, int flags, int mode ) "而不是" fopen"?
    • 是的,这是可能的,如果您使用 open() 实现相同的功能,这将是可取的。您需要使用read() 调用而不是fscanf()
    • 我试过了但是不行:[Error] 'fork' is not declared in this scope
    • 并确保你打电话给if( fork() == 0 )而不是if( fork == 0 ),有时会发生。
    • 是的,我的代码:int inputfd = open("in.txt",O_RDONLY); if (inputfd == -1) { 退出(EXIT_FAILURE); } void buf = (char) malloc(1024); while(read(inputfd,buf,1024) > 0) { if(fork() ==0 ) execlp("/bin/mkdir","mkdir",buf,NULL);别的 ; }
    【解决方案3】:

    如果你想执行除 mkdir 之外的其他命令的另一个选项

    while read -r line; do mkdir -p $line && chown www-data:www-data $line; done < file.txt
    

    【讨论】:

      【解决方案4】:

      您可以从 Linux 终端使用 for 循环,例如:

      for line in $(cat in.txt); do mkdir $line; done

      这应该可以很好地创建多个文件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-02-08
        • 2016-12-23
        • 2022-01-13
        • 2019-09-28
        • 1970-01-01
        • 2013-12-29
        • 2021-12-26
        相关资源
        最近更新 更多