Support >
  About cybersecurity >
  How to list all files by size in Linux
How to list all files by size in Linux
Time : 2024-11-28 16:54:58
Edit : Jtti

In Linux, you can use the ls command combined with some parameters to list all files in order of size. Here are some common methods:

Use the ls command to sort them in ascending order:

ls -lS

perhaps

ls -lSr

Here, -l means to list the file details in a long format, and -S means to sort by file size.

In descending order of size:

ls -lSr

The -r parameter indicates the reverse sorting result.

You can also add the file size to a readable format with -h:

$ ls -laSh /var/www/html/admin_portal/

If you want to recursively find and sort files by size in a directory, you can use the find command combined with the -exec argument:

Recursively list all files in the current directory and subdirectories, and sort them by size:

find . -type f -exec ls -lhS {} +

Here, -type f means to find only files, -exec is followed by a command that is executed for each file found, {} is a placeholder for the currently found file name, and + means to pass all found file names as arguments to the ls command at once.

If you want more details or to sort the size of the directory, you can use the du command combined with the sort command:

Lists all files and directories in the current directory, sorted by size:

du -sh * | sort -hr

Here, du -sh * shows the size of each file and directory in the current directory, and sort-hr is sorted in human-readable format (-r for descending).

These commands help you list the files in your Linux system by size. You can choose to use according to your needs. cron can also be used in Linux to periodically perform custom file size sorting tasks. A Cron is a scheduled task that executes commands on a regular basis in Linux. Open the terminal and enter the following command to edit the cron table:

crontab -e

The cron table editor for the current user opens. In the open editor you can add a new cron format as:

* * * * * command-to-be-executed

- - - - -

| | | | |

| | | | +----- day of week (0 - 6) (Sunday=0 or 7)

| | | +------- month (1 - 12)

| | +--------- day of month (1 - 31)

| +----------- hour (0 - 23)

+------------- minute (0 - 59)

If you want to automatically sort file sizes at 1am every day and save the results into a file, you can add the following lines:

0 1 * * * /usr/bin/ls -lS > /path/to/your/directory/size_sort.log

Here, / usr/bin/ls -ls is sorted by size command, > as symbols of redirect output to a file, / path/to/your/directory/size_sort. The log is what you want to save the file path of the result. Then save and launch.

To verify:

sudo service cron start

or

sudo systemctl start cron

Once this is done, the file size sorting task can be automatically performed according to the set schedule.

JTTI-Defl
JTTI-COCO
JTTI-Selina
JTTI-Ellis
JTTI-Eom