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

How can I generate a list of files with their absolute path in Linux?

1个答案

1

In Linux, generating a file list with absolute paths can typically be achieved using the find command. The find command is a powerful utility in Linux for searching files, allowing searches based on various criteria and executing corresponding actions.

Here is a basic example of using the find command to generate a file list. Suppose we want to search for all .txt files in the current user's home directory:

sh
find /home/username -type f -name "*.txt"
  • find is the command.
  • /home/username is the starting directory for the search, which should be replaced with the actual user directory path or substituted with the tilde character ~ to denote the current user's home directory.
  • -type f specifies that we are searching for files only.
  • -name "*.txt" indicates that we are searching for files with names ending in .txt.

This command lists all files matching the criteria and displays their absolute paths.

If we want to output all absolute paths to a file, we can use output redirection:

sh
find /home/username -type f -name "*.txt" > filelist.txt

This will write the absolute paths of all .txt files in the current user's home directory to the filelist.txt file.

Additionally, if we need to include all files in subdirectories, not just .txt files, we can omit the -name option:

sh
find /home/username -type f > allfileslist.txt

This will output the absolute paths of all files in the user's home directory and all subdirectories to the allfileslist.txt file.

In practical applications, we may need to generate file lists based on additional conditions such as file size or modification time, which the find command supports.

For example, to list .log files modified within the last 30 days, the command can be:

sh
find /var/log -type f -name "*.log" -mtime -30
  • /var/log is a common directory for log files.
  • -mtime -30 specifies files modified within the last 30 days.

This concludes the methods for generating a file list with absolute paths in Linux.

2024年6月29日 12:07 回复

你的答案