[英]Separate Directories from Files with “----” Bash Scripting
I want to separate directories from files in a list. 我想将目录与列表中的文件分开。 I would like them to appear as follows:
我希望它们显示如下:
DirectoryName_Two DirectoryName_Two
-- -
FileName1 FILENAME1
Basically, I want two or three dashes in between my directories and files. 基本上,我要在目录和文件之间插入两个或三个破折号。 Here is what the following code looks like.
以下是下面的代码。
Here is my code: 这是我的代码:
#!/bin/bash
if [[ $# -ge 1 ]]; then
cd "$1" 2> /dev/null
if [[ $? = 1 ]]; then
echo "Please enter a valid directory."
else
ls -a | sort -k 1 | awk '{printf "(%d) %s\n", NR, $0;}'
fi
else
ls -a | sort -k 1| awk '{printf "(%d) %s\n", NR, $0;}'
fi
Here's one possible solution: 这是一种可能的解决方案:
#!/bin/bash
if [[ $# -ge 1 ]]; then
dir_to_list=$1
if [[ ! -d ${dir_to_list} ]]; then
echo "Please enter a valid directory."
exit
fi
else
dir_to_list="."
fi
files=`ls --group-directories-first $dir_to_list`
DIRS="TRUE"
i=0
for f in ${files}; do
if [[ ${DIRS} == "TRUE" && ! -d ${dir_to_list}/${f} ]]; then
# First non-directory entry
echo ----
DIRS="FALSE"
fi
(( i++ ))
echo ${i}. ${f}
done
Cheers 干杯
Update: fixed bug for listing other directories 更新:修复了列出其他目录的错误
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.