Shell程序,用于按照目录名称开头的子树中的时间顺序(例如按修改日期)计算和显示目录列表 预期的表单输出结果:
directory <directory name>--| <--initial directory
catalog <name>--------------|
----------------------------| <--directories in the current directory
catalog <name>--------------|
directory <directory name>--| <--sub-directory
catalog <name>--------------|
----------------------------| <--directories in the current directory
catalog <name>--------------|
----------------------------
and etc.
这里我找到了递归列出目录和子目录以及修改日期的脚本。但是如何按时间顺序和嵌套水平对其进行排序?
#!/bin/bash
#script to recursively travel a dir of n levels
function traverse() {
for folderin $(ls "$1")
do
if [[ ! -f ${1}/${folder} ]]; then
stat="$(date -r ${1}/${folder} +"%F %T")"
echo "${1}/${folder} ${stat}"
traverse "${1}/${folder}"
fi
done
}
function main() {
traverse "$1"
}
main "$1"
非常感谢你。 祝你有愉快的一天。
P.S。输出格式类似 - 由嵌套级别和时间顺序分隔:
1 level:
/dir1/
/dir2/
/dir2/
2 level:
/dir1/dir1/
/dir1/dir2/
/dir1/dir3/
/dir2/dir1/
/dir2/dir2/
/dir2/dir3/
/dir3/dir1/
/dir3/dir2/
/dir3/dir3/
3 level:
/dir1/dir1/dir1/
/dir1/dir1/dir2/
/dir1/dir1/dir3/
/dir1/dir2/dir1/
/dir1/dir2/dir2/
/dir1/dir2/dir3/
/dir1/dir3/dir1/
/dir1/dir3/dir2/
/dir1/dir3/dir3/
etc.
OR
1 level:
/dir1/
/dir2/
/dir2/
2 level:
/dir1/dir1/
/dir1/dir2/
/dir1/dir3/
3 level:
/dir1/dir1/dir1/
/dir1/dir1/dir2/
/dir1/dir1/dir3/
2 level:
/dir2/dir1/
/dir2/dir2/
/dir2/dir3/
3 level:
/dir1/dir2/dir1/
/dir1/dir2/dir2/
/dir1/dir2/dir3/
2 level:
/dir3/dir1/
/dir3/dir2/
/dir3/dir3/
3 level:
/dir1/dir3/dir1/
/dir1/dir3/dir2/
/dir1/dir3/dir3/
etc.
不太重要,只是不要像这样混合嵌套级别:
/dir1/
/dir1/dir1/
/dir1/dir1/dir1/
/dir2/
/dir1/dir2/
/dir1/dir1/dir1/
/dir3/
/dir3/dir1/
/dir1/dir3/dir1/
答案 0 :(得分:1)
您可以尝试使用find
命令或tree -d -t -f
这是我创建的临时结构(ls -R =递归列表)
~/temp$ ls -R
.:
dir1/ dir2/ dir3/ file1
./dir1:
catalog1
./dir2:
./dir3:
现在您可以尝试使用find
命令,如下所示:
find . -type d -exec ls -dlrt {} \;
获取列表
OR
find . -type d -exec ls -dlrt {} \; | wc --lines
获得点数
编辑1:只获取顶级目录,您可以添加-maxdepth
并迭代地为其提供深度值,例如
find . -maxdepth 1 -type d -exec ls -dlrt {} \;
获取列表
OR
find . -maxdepth 1 -type d -exec ls -dlrt {} \; | wc --lines
获得点数
<强> -------------------------------------------- --- IGNORE ABOVE STUFF -------------------------------------------- ------- 强>
编辑2:
我明白了,现在我得到了你的问题..我做了一个bash脚本来完成你的任务......你需要
find
命令下面是我做的〜/ temp目录结构
$ ls -R temp
temp:
dir1/ dir2/ dir3/ file1
temp/dir1:
dir3/ dir4/
temp/dir1/dir3:
temp/dir1/dir4:
temp/dir2:
dir/ dir4/ dir5/ dir6/
temp/dir2/dir:
newdir/
temp/dir2/dir/newdir:
temp/dir2/dir4:
temp/dir2/dir5:
temp/dir2/dir6:
temp/dir3:
这里是bash脚本..删除评论/ debugging_echo&#39; s如果你认为它们太多了。 我试图解释脚本本身的逻辑(使用注释)。
#!/bin/bash
declare front_element="./temp"
#dir to start with
declare -a q=($(find "$front_element" -maxdepth 1 -type d -not -path "$front_element" -printf '%T@ %p\n' | sort | awk '{print $2}'))
#initial queue population
declare -a temp_arr
if [[ ${#q[@]} -eq 0 ]]; then
printf "%s%s contains %d child directories(last modified time sort): \n" "----->" "$front_element" "${#q[@]}"
else
printf "%s%s contains the following %d child directories(last modified time sort): \n" "----->" "$front_element" "${#q[@]}"
fi
printf "\t%s\n" "${q[@]}"
while [[ ${#q[@]} -ne 0 ]]
do
front_element="${q[0]}"
#Queue fetching front element
#echo "$front_element is front_element"
q=("${q[@]:1}")
#actual queue dequeue operation=>reduction in size
temp_arr=($(find "$front_element" -maxdepth 1 -type d -not -path "$front_element" -printf '%T@ %p\n' | sort | awk '{print $2}'))
#excluding self during find using -not -path self, %Tk=last modified time printed in k format(here used @=>time in seconds since UTC/Unix Epoch Jan 1, 1970 midnight along with fractional part), %p=found path, sort=>uses last modified time to sort and it sorts alphabetically if same last modified time for >=2 directories(highly unlikely as it has fractional part too)
if [[ ${#temp_arr[@]} -eq 0 ]]; then
printf "%s%s contains %d child directories. \n" "----->" "$front_element" "${#temp_arr[@]}"
else
printf "%s%s contains the following %d child directories(last modified time sorted): \n" "----->" "$front_element" "${#temp_arr[@]}"
fi
#displaying the count as well
if [[ ${#temp_arr[@]} -gt 0 ]]
then
printf "\t%s\n" "${temp_arr[@]}"
echo
for element in "${temp_arr[@]}"
do
q+=("$element")
done
fi
#appending newly found stuff to the current queue for further processing
#echo "${q[@]} is q at end of iteration"
#echo "${temp_arr[@]} is temp_arr at end of iteration"
done
以上是bash脚本的输出(在根目录上方的目录中运行)
对我来说,我的$ PWD / current_dir是〜即$ HOME 因为我的临时目录在这里,即临时目录的父亲是$ HOME
PS:它没有经过彻底的测试。
以下是您在编辑问题后在第一个结构中提到的输出方式。
$ ./script.bash
----->./temp contains the following 3 child directories(last modified time sort):
./temp/dir3
./temp/dir1
./temp/dir2
----->./temp/dir3 contains 0 child directories.
----->./temp/dir1 contains the following 2 child directories(last modified time sorted):
./temp/dir1/dir3
./temp/dir1/dir4
----->./temp/dir2 contains the following 4 child directories(last modified time sorted):
./temp/dir2/dir4
./temp/dir2/dir5
./temp/dir2/dir6
./temp/dir2/dir
----->./temp/dir1/dir3 contains 0 child directories.
----->./temp/dir1/dir4 contains 0 child directories.
----->./temp/dir2/dir4 contains 0 child directories.
----->./temp/dir2/dir5 contains 0 child directories.
----->./temp/dir2/dir6 contains 0 child directories.
----->./temp/dir2/dir contains the following 1 child directories(last modified time sorted):
./temp/dir2/dir/newdir
----->./temp/dir2/dir/newdir contains 0 child directories.
PPS:我已经对根目录进行了硬编码&#39; ./ temp&#39;,您需要将其更改为根目录