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

标签 bash shell filenames freebsd

我需要在 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/

    相关文章:

    bash 羊群 : Why 200?

    bash - `<<()` 语法是什么?

    linux - 更改文件的编号 Bash

    带有文件名的 Excel 宏在 Office 2016 64 位中不起作用

    c# - 获取 Windows 可移植设备上文件的全名

    java 文件名过滤模式

    Linux/bash 管道和重定向

    bash - 使用 bash 脚本搜索匹配的文件名模式

    bash - 如何在 bash 命令中转义双引号

    shell - "Exception handling"在 shell 脚本中