我需要在 FreeBSD 中使用 bash 脚本完成以下事情:
我能完成的是使用
find
和 grep
命令获取 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/