gpt4 book ai didi

bash - 如何从文件列表中删除路径部分并将其复制到另一个文件中?

转载 作者:行者123 更新时间:2023-11-29 09:26:29 31 4
gpt4 key购买 nike

我需要在 FreeBSD 中使用 bash 脚本完成以下事情:

  • 创建目录。
  • 生成 1000 个唯一文件,其名称取自系统中的其他随机文件。
  • 每个文件都必须包含它所采用的原始文件的信息 - 名称和大小,不包含文件的原始内容。
  • 脚本必须以毫秒为单位显示有关其执行速度的信息。

  • 我能完成的是使用 findgrep 命令获取 1000 个唯一文件的名称和路径,并将它们放在一个列表中。然后我无法想象如何删除路径部分并在另一个目录中使用随机文件列表中的名称创建文件。我尝试了一个带有 basename 命令的 for 循环,但不知何故我无法让它工作,我也不知道如何做其他任务......

    最佳答案

    What I could accomplish was to take the names and paths of 1000 unique files with the commands "find" and "grep" and put them in a list



    我将假设有一个文件在每一行上保存每个文件的完整路径 (FULL_PATH_TO_LIST_FILE)。考虑到与此过程相关的统计数据并不多,我省略了它。但是,您可以添加自己的。
    cd WHEREVER_YOU_WANT_TO_CREATE_NEW_FILES
    for file_path in `cat FULL_PATH_TO_LIST_FILE`
    do
    ## This extracts only the file name from the path
    file_name=`basename $file_path`

    ## This grabs the files size in bytes
    file_size=`wc -c < $file_path`

    ## Create the file and place info regarding original file within new file
    echo -e "$file_name \nThis file is $file_size bytes "> $file_name

    done

    关于bash - 如何从文件列表中删除路径部分并将其复制到另一个文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16722178/

    31 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com