How to find all .zip files and file size in linux?

by georgiana.senger , in category: General Help , a month ago

How to find all .zip files and file size in linux?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by viola_gleichner , a month ago


To find all .zip files and their file sizes in Linux, you can use the find command along with the ls command and some basic shell scripting.

Here is the command to achieve this:

find /path/to/search -type f -name "*.zip" -exec ls -lhS {} ; | awk '{print $5, $9}'

Replace /path/to/search with the directory where you want to search for the .zip files. This command will recursively search for .zip files in the specified directory.

Let's break down the command:

  1. find: Locates files based on various search criteria.
  2. /path/to/search: The directory path where you want to search for .zip files.
  3. -type f: Only search for regular files (excluding directories and other special files).
  4. -name "*.zip": Search for files with a .zip extension.
  5. -exec ls -lhS {} ;: Executes the ls command with the -lhS options on each file found, which displays the file size in a human-readable format and sorts files based on their size in descending order.
  6. awk '{print $5, $9}': Uses awk command to print the file size (5th field) and file name (9th field) from the ls command's output.

After running this command, you will get a list of .zip files with their respective file sizes.