bash列出所有目录,子目录,文件夹,子文件夹然后输出到单独的文件

时间:2016-07-09 08:51:25

标签: android linux bash sed command

我正在尝试创建两个非常相似的脚本。一个脚本是列出目录中的所有文件,然后列出子目录,子目录的子目录等等,直到没有更多目录,然后将信息输出到日志文件,路径名称从我从的目录开始。另一个脚本与上面完全相同,但我需要它来列出那些目录和子目录中的所有文件,而不是目录。我还需要从我开始的目录开始的路径名。但是,它们都需要在单独的日志文件中输出。

我需要在sed命令或与android兼容的bash命令中执行此操作。

/ system /表示我想从

开始的目录

这只输出/ system /中的目录并成功生成一个日志文件:

LOG_FILE=/data/local/log.log;
for d in /system/*; do [[ -f "$d" ]] || echo "$d" "$d"| tee -a $LOG_FILE; done

这只输出/ system /中的文件并成功生成一个日志文件:

LOG_FILE=/data/local/log.log;
for f in /system/*; do [[ -d "$f" ]] || echo "$f" "$f"| tee -a $LOG_FILE; done

不幸的是,我在这里或其他任何地方都没有成功找到如何正确地进行搜索。我几乎可以肯定我不能在我正在使用的Android系统中递归执行。我认为我所有的工作都是有限的sed和bash。

任何帮助都将不胜感激。

编辑:所以我忘了添加我需要在下面的方式中添加行到文件中每行的开头和结尾,但是我需要进行上述更改。

LOG_FILE=/local/log.log;
tmp1="cp"
tmp2=tmp
tmp3=test
for f in /system/*; do [[ -d "$f" ]] || echo $tmp1 $tmp2/$tmp3"$f" "$f"| tee -a $LOG_FILE; done

所以输出会像这样输出/system/file.txt是我需要复制的路径(用“$ f”表示):

cp tmp/test/system/file.txt system/file.txt
cp tmp/test/system/file2.txt system/file2.txt

5 个答案:

答案 0 :(得分:2)

这适用于目录:

find /system/ -type d -print > $LOG_FILE

这应该是你的文件列表:

find /system/ -type f -print > $LOG_FILE

答案 1 :(得分:1)

要在行的开头和结尾添加文本,请将xargs与find

一起使用
find /system/ -type f|xargs -i echo "BEGIN" {} "END" > $LOG_FILE

更新

#¡/bin/bash

LOG_FILE="log"
tmp1="cp"
tmp2=tmp
tmp3=test

for i in `find /system/ -type f`;
do
  [[ -d "$i" ]] || echo $tmp1 $tmp2/$tmp3"$i" "$i"| tee -a $LOG_FILE;  
done

输出

cp tmp/test/dir/file dir/file

答案 2 :(得分:0)

您可以一次性遍历文件和目录的目录:

find . \( -type d -printf "%p\n" \) -o \( -type f -printf  "cp %p tmp/test/%p\n" \) >out.log

并使用不同的 c风格 output format directives来自定义输出。

答案 3 :(得分:0)

以下一个班轮从起点到最深层逐级行走

def predict(self, X):
        """Predict using the linear model
        Parameters
        ----------
        X : {array-like, sparse matrix}, shape = (n_samples, n_features)
            Samples.
        Returns
        -------
        C : array, shape = (n_samples,)
            Returns predicted values.
        """
        return self._decision_function(X)

    _center_data = staticmethod(center_data)

取决于您的需求,您可以在let d=1; continue=0; while [[ $continue -eq 0 ]]; do find <start path> -mindepth $d -maxdepth $d -type f -exec echo "<Your message> {}" \; | grep . ; continue=$? ;(( d++ )); done; -type f

之间进行选择

答案 4 :(得分:0)

如果我是你并且我使用find来获取文件列表及其类型,那么您需要进行大量处理:

$ find tmp -printf '%y %p\n'
d tmp
f tmp/file001
f tmp/file002
f tmp/file003
d tmp/foo
f tmp/foo/file004

然后你可以随心所欲地做任何事情,例如:

$ find tmp -printf '%y %p\n' | awk '$1=="d"{sub(/^../,"");print}'
tmp
tmp/foo

$ find tmp -printf '%y %p\n' | awk '$1=="f"{sub(/^../,"");print}'
tmp/file001
tmp/file002
tmp/file003
tmp/foo/file004

$ find tmp -printf '%y %p\n' | awk -F'/' '{print NF, $0}' | sort -k1,1n | cut -d' ' -f2-
d tmp
d tmp/foo
f tmp/file001
f tmp/file002
f tmp/file003
f tmp/foo/file004

$ find tmp -printf '%y %p\n' | awk -F'/' '{print NF, $0}' | sort -k1,1nr | cut -d' ' -f2-
f tmp/foo/file004
d tmp/foo
f tmp/file001
f tmp/file002
f tmp/file003
d tmp

你无法从示例中看到它,但最后的命令是按深度排序。

如果你的名字中有换行符的文件,上述所有内容都将失败。

相关问题