乐闻世界logo
搜索文章和话题

How do you list files with a specific extension in a directory using shell commands?

1个答案

1

In the shell, the most common command to list files with specific extensions in a directory is ls used with wildcards. For example, if you want to list all .txt files in the current directory, you can use the following command:

bash
ls *.txt

This command will display all files ending with .txt in the current directory.

If you need to search the entire file structure including subdirectories, you can use the find command. For example, to find all .jpg files in the current directory and all its subdirectories, you can use:

bash
find . -type f -name "*.jpg"

Here, . represents the current directory, -type f indicates that you're only interested in files (ignoring directories), and -name "*.jpg" specifies the filename pattern.

Additionally, if you want to precisely control the search results, such as filtering by file modification time or size, the find command can do this. For example, to find all .png files modified in the last 7 days:

bash
find . -type f -name "*.png" -mtime -7

These methods are based on standard command-line tools for Unix and Linux systems, which are powerful and flexible.

2024年8月14日 17:42 回复

你的答案