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

Android bash列出中的所有目录、子目录、文件夹、子文件夹,然后输出到单独的文件,android,linux,bash,sed,command,Android,Linux,Bash,Sed,Command,我正在尝试创建两个非常相似的脚本。一个脚本是列出目录中的所有文件,然后列出子目录、子目录的子目录等等,直到没有更多的目录,然后将信息输出到一个日志文件,路径名从我开始的目录开始。另一个脚本与上面完全相同,但我需要它列出这些目录和子目录中的所有文件,而不是目录。我还需要从我开始的目录开始的路径名。但是,它们都需要在单独的日志文件中输出 我需要在与android兼容的sed命令或bash命令中执行此操作 /system/表示我要从中开始的目录 这将仅输出/system/中的目录,并成功生成一个日志文

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

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

/system/表示我要从中开始的目录

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

LOG_FILE=/data/local/log.log;
for d in /system/*; do [[ -f "$d" ]] || echo "$d" "$d"| tee -a $LOG_FILE; done
LOG_FILE=/data/local/log.log;
for f in /system/*; do [[ -d "$f" ]] || echo "$f" "$f"| tee -a $LOG_FILE; done
这将仅输出/system/中的文件,并成功生成日志文件:

LOG_FILE=/data/local/log.log;
for d in /system/*; do [[ -f "$d" ]] || echo "$d" "$d"| tee -a $LOG_FILE; done
LOG_FILE=/data/local/log.log;
for f in /system/*; do [[ -d "$f" ]] || echo "$f" "$f"| tee -a $LOG_FILE; done
不幸的是,我一直未能在这里或其他任何地方找到正确的方法。我几乎可以肯定,在我使用的安卓系统中,我不能递归地完成这项工作。我认为我所要做的就是以有限的方式使用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”表示):


这应该适用于以下目录:

find /system/ -type d -print > $LOG_FILE
这将为您提供文件列表:

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

这应该适用于以下目录:

find /system/ -type d -print > $LOG_FILE
这将为您提供文件列表:

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

要在行首和行尾添加文本,请将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

要在行首和行尾添加文本,请将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

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

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

并使用不同的c样式自定义输出。

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

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

并使用不同的c样式自定义输出。

下面的一行代码从起点到最深处逐级遍历

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;
设d=1;continue=0;而[[$continue-eq 0]];do find-mindepth$d-maxdepth$d-type f-exec echo“{}”\|格雷普;继续=$;(d++);完成;

根据您的需要,您可以在
-f型
-d型

之间进行选择。下面的一个班轮从起点到最深处逐级行走

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;
设d=1;continue=0;而[[$continue-eq 0]];do find-mindepth$d-maxdepth$d-type f-exec echo“{}”\|格雷普;继续=$;(d++);完成;

根据您的需要,您可以在
-type f
-type d
之间进行选择。如果我是您,听起来您需要进行大量处理。我会使用
查找
来获取文件列表及其类型:

$ 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
从这个例子中你看不清楚,但是最后的命令是按深度排序的


如果文件名中有换行符,上述所有操作都将失败。

听起来您需要进行大量处理,如果我是您,我将使用
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
从这个例子中你看不清楚,但是最后的命令是按深度排序的


如果文件名称中有换行符,上述所有操作都将失败。

谢谢您的回答,我忘记在脚本中添加一些重要信息,因此我在操作列表底部添加了编辑。谢谢您的回答,我忘了在我的脚本中添加一些重要信息,所以我在OPIf的底部添加了编辑。如果我想将更新函数中的文件或目录/子目录排除在日志文件之外,那该怎么做呢?如果[“$I”=~README\.md$],你可以添加一个if-else循环来跳过任何特定的文件
if[“$I”=~README\.md$];然后什么也不做;否则,做点什么;fi
。但是如果你有一长串的文件,我想使用类似的方法是最好的选择。如果我想将更新函数中的文件或目录/子目录排除在日志文件之外,那该怎么做呢?你可以添加一个if-else循环来跳过任何特定的文件
if[“$I”=~README\.md$];然后什么也不做;否则,做点什么;fi
。但是如果你有一长串的文件,我想使用类似的东西是最好的选择。UNIX中没有文件夹和/或子文件夹。有目录和文件。其他术语来自Windows或DOS。UNIX中没有文件夹和/或子文件夹。有目录和文件。其他术语来自Windows或DOS。