How to list last 10 files in all the subdirectories in linux?

by alyson_bogan , in category: General Help , a year ago

How to list last 10 files in all the subdirectories in linux?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by modesta , a year ago

@alyson_bogan 

To list the last 10 files in all the subdirectories in Linux, you can use the "find" command along with the "ls" command and some additional options. Here's an example command:

1
find /path/to/directory -type f -exec ls -lt {} + | head -n 10


Replace "/path/to/directory" with the actual path to the parent directory where you want to start searching for files.


Explanation of the command:

  1. "find /path/to/directory" searches for files in the specified directory and all its subdirectories.
  2. "-type f" tells "find" to only search for regular files (excludes directories).
  3. "-exec ls -lt {} +" executes the "ls" command with the sorted file list encountered. The "-t" flag is used to sort the listing by modification time, and the "-l" flag shows detailed information of each file.
  4. "| head -n 10" pipes the output of the previous command to "head" command to display only the first 10 lines, which are the last 10 files based on modification time.


By running this command, you will get a list of the last 10 files (with their details) in all the subdirectories under the specified parent directory.

Member

by ethelyn , 8 months ago

@alyson_bogan 

It seems like something went wrong with the formatting in the response. Let me provide it in a clearer way:


To list the last 10 files in all the subdirectories in Linux, you can use the following command:

1
find /path/to/directory -type f -exec ls -lt {} + | head -n 10


Replace "/path/to/directory" with the actual path to the parent directory where you want to start searching for files.


Explanation of the command:

  • find /path/to/directory: searches for files in the specified directory and all its subdirectories.
  • -type f: tells "find" to only search for regular files (excludes directories).
  • -exec ls -lt {} +: executes the "ls" command with the sorted file list encountered. The "-t" flag is used to sort the listing by modification time, and the "-l" flag shows detailed information of each file.
  • | head -n 10: pipes the output of the previous command to "head" command to display only the first 10 lines, which are the last 10 files based on modification time.


By running this command, you will get a list of the last 10 files (with their details) in all the subdirectories under the specified parent directory.