我希望使用linux查看目录下的特定文件列表。 比如说: - 我在当前目录中有以下子目录
Feb 16 00:37 a1
Feb 16 00:38 a2
Feb 16 00:36 a3
现在,如果我ls a* -
,我可以看到
bash-4.1$ ls a*
a:
a1:
123.sh 123.txt
a2:
a234.sh a234.txt
a3:
a345.sh a345.txt
我想从目录中过滤出.sh
个文件,以便输出为: -
a1:
123.sh
a2:
a234.sh
a3:
a345.sh
可能吗?
此外还可以打印sh文件的第一行吗?
答案 0 :(得分:2)
以下find
命令适合您:
find . -maxdepth 2 -mindepth 2 -path '*/a*/*.sh' -print -exec head -n1 {} \;
答案 1 :(得分:0)
看看这些选项。我希望你能找到你想要的东西
find / -name foo.txt -type f -print # full command
find / -name foo.txt -type f # -print isn't necessary
find / -name foo.txt # don't have to specify "type==file"
find . -name foo.txt # search under the current dir
find . -name "foo.*" # wildcard
find . -name "*.txt" # wildcard
find /users/al -name Cookbook -type d # search '/users/al'
find /opt /usr /var -name foo.scala -type f # search multiple dirs
find . -iname foo # find foo, Foo, FOo, FOO, etc.
find . -iname foo -type d # same thing, but only dirs
find . -iname foo -type f # same thing, but only files
find . -type f \( -name "*.c" -o -name "*.sh" \) # *.c and *.sh files
find . -type f \( -name "*cache" -o -name "*xml" -o -name "*html" \) # three patterns
find . -type f -not -name "*.html" # find allfiles not ending in ".html"
find . -type f -name "*.java" -exec grep -l StringBuffer {} \; # find StringBuffer in all *.java files
find . -type f -name "*.java" -exec grep -il string {} \; # ignore case with -i option
find . -type f -name "*.gz" -exec zgrep 'GET /foo' {} \; # search for a string in gzip'd files
答案 2 :(得分:0)
仅使用ls
,您可以使用以下命令获取.sh文件及其父目录:
ls -1 * | grep ":\|.sh" | grep -B1 .sh
将提供输出:
a1:
123.sh
a2:
a234.sh
a3:
a345.sh
但请注意,如果您有任何名为123.sh.txt
的文件
为了在每个文件夹中打印第一个.sh文件的第一行:
head -n1 $(ls -1 */*.sh)
答案 3 :(得分:0)