Cha*_*had 5 linux bash shell command-line ms-dos
抱歉,如果这是微不足道的 - 但如何在我的根文件夹的每个子目录中只列出一个文件?Linux 终端或 MS DOS 语法无关紧要。我猜这将是一个带有一些参数的 ls 或 dir 命令,但我在手册中没有找到任何命令的任何内容。
写了一个小脚本来做到这一点。
#!/bin/bash
for dir in `find . -type d` # Find directories recursively
do
# cd into directory, quotes are for directory names with spaces
cd "$dir"
# Print out directory name
echo "In directory $dir:"
# Force list output to be one entry per line, pipe through inverted grep to
# exclude directories, pipe through awk to get the first item. You can specify
# the number of files you want.
ls -p1 | grep -v / | awk -v "num_of_files=1" 'NR<=num_of_files { print $1 }'
# cd back to root directory
cd "$OLDPWD"
done
Run Code Online (Sandbox Code Playgroud)
输出:
In directory .:
test.sh
In directory ./bar:
barfile.txt
In directory ./baz:
bazfile.txt
In directory ./baz/quux:
something.txt
In directory ./foo:
foofile.txt
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3777 次 |
| 最近记录: |