[英]Bash Script for listing subdirectories and files in textfile
I need a Script that writes the directory and subdirectory in a text-file. 我需要一个在文本文件中写入目录和子目录的脚本。
For example the script lies in /Mainfolder and in this folder are four other folders. 例如,脚本位于/ Mainfolder中,此文件夹中还有其他四个文件夹。 Each contains several files. 每个文件包含几个文件。
Now I would like the script to write the path of each file in the textfile. 现在,我希望脚本在文本文件中写入每个文件的路径。
Subfolder1/File1.dat
Subfolder1/File2.dat
Subfolder2/File1.dat
Subfolder3/File1.dat
Subfolder4/File1.dat
Subfolder4/File2.dat
Important is that there is no slash in front of the listing. 重要的是清单前面没有斜杠。
Use the find
command: 使用find
命令:
find Mainfolder > outputfile
and if you only want the files listed, do 如果只想列出文件,请执行
find Mainfolder -type f > outputfile
You can also strip the leading ./
if you search the current directory, with the %P
format option: 如果您搜索当前目录,也可以使用%P
格式选项删除./
:
find . -type f -printf '%P\n' > outputfile
If your bash version is high enough, you can do it like that: 如果您的bash版本足够高,则可以这样做:
#!/bin/bash
shopt -s globstar
echo ** > yourtextfile
This solution assumes that the subdirectories contain only files -- they do not contain any directory in turn. 此解决方案假定子目录仅包含文件,而它们又不包含任何目录。
find . -type f -print | sed 's|^.*/S|S|'
I have created a single file in each of the four subdirectories. 我在四个子目录中的每个目录中都创建了一个文件。 The original output is: 原始输出为:
./Subfolder1/File1.dat
./Subfolder4/File4.dat
./Subfolder2/File2.dat
./Subfolder3/File3.dat
The filtered output is: 过滤后的输出为:
Subfolder1/File1.dat
Subfolder4/File4.dat
Subfolder2/File2.dat
Subfolder3/File3.dat
You can use this find with -exec
: 您可以将此查找与-exec
:
find . -type f -exec bash -c 'f="{}"; echo "${f:2}"' \;
This will print all files starting from current paths by removing ./
from front. 通过从前面删除./
,将从当前路径开始打印所有文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.