我需要在列表中所有文件的每个子目录中创建一个文本文件。
例如,subdirectory1
将包含其所有文件的列表作为 .txt
,subdirectory2
还将包含所有 的列表>subdirectory2
文件作为 .txt
。
我试过了
#!/bin/bash
for X in "$directory" *
do
if [ -d "$X" ];
then
cd "$X"
files="$(ls)"
echo "$files" >> filesNames.txt
fi
done
然而,这并没有产生任何东西。我绝对需要它作为 shell 脚本,因为它将成为管道脚本的一部分,但我似乎无法让它工作。
这是调整后的脚本,给我没有这样的文件或目录注释。我知道该文件夹存在并且已在此命令之前运行的命令中使用它。
#!/bin/bash
#Retrieve the base directory path
baseDir=$(dirname "$ini")
#Retrieve the reference genome path
ref=$(dirname "$genome")
#Create required directory structure
tested="$baseDir/tested"
MarkDups1="$baseDir/MarkDups1"
#don't create if already exists
[[ -d "tested" ]] || mkdir "$tested"
[[ -d "MarkDups1" ]] || mkdir "$MarkDups1"
#create a text file with all sorted and indexed bam files paths
#!/bin/bash
for x in $MarkDups1/*/;
do
(cd "$x"; ls > filesNames.txt)
done
最佳答案
要迭代的序列应该是 "$directory"/*/
。
for x in "$directory"/*/; do
(cd "$x"
files=(*)
printf '%s\n' "${files[@]}" > filesNames.txt
)
done
关于linux - 在每个子目录中创建所有文件的 .txt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40312357/