linux - 在每个子目录中创建所有文件的 .txt

标签 linux bash

我需要在列表中所有文件的每个子目录中创建一个文本文件。
例如,subdirectory1 将包含其所有文件的列表作为 .txtsubdirectory2 还将包含所有 的列表>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/

相关文章:

python - 使用 pycurl 和端口号检索 url 的源代码?

linux - 二郎odbc : How to disable autocommit for oracle

bash - 我可以在不修改现有所有者/组的情况下在 Bash 中为特定组/用户添加权限吗?

regex - native bash 正则表达式 [[ $f =~ "^[^\.]+$"]] 从不匹配

bash - 如何使用 fd 而不是命名管道

linux - PV dev/dev/mapper/mpathc 的副本存在于未知设备 8 :96

python - python中的多服务器监视器/自动重启器

linux - 读取 USB UPS 状态的 Bash 脚本

linux - 如何将列输出合并到前一列的行尾?

string - 在 bash 脚本中乘以字符串