Forum

How to delete files...
 
Notifications
Clear all

How to delete files in Linux VPS?

 
(@mrjames)
Active Member

Hello,

I am new on server management, can you help me.

I want delete all tmp files, It is taking more time to delete via FTP client.

Also, need a PHP function to delete those TMP files via cronjob.

Thank you.

 

Quote
Topic starter Posted : 13/02/2023 12:42 pm
(@administrator)
Member Admin

To delete files in a Linux VPS, you can use the rm command. Here are the steps to delete all tmp files in the VPS via SSH:

  1. Connect to your VPS via SSH using your terminal or an SSH client like PuTTY.
  2. Navigate to the directory where the tmp files are stored. The command for navigating to a directory is cd directoryname.
  3. Once you are in the directory, use the following command to delete all the tmp files: rm -rf *.tmp.

This command will recursively remove all files with the ".tmp" extension. The "-r" flag makes the command recursive, so it will delete files in subdirectories as well, and the "-f" flag forces the command to delete the files without prompting for confirmation.

If you want to delete the tmp files via PHP, you can use the unlink() function. Here's an example PHP script that deletes all the tmp files in a directory:

 

<?php
$dir = '/path/to/tmp/files';

// Open the directory
if ($handle = opendir($dir)) {
    // Loop through the directory
    while (false !== ($entry = readdir($handle))) {
        // Check if the file has a .tmp extension
        if (pathinfo($entry, PATHINFO_EXTENSION) == 'tmp') {
            // Delete the file
            unlink($dir . '/' . $entry);
        }
    }
    // Close the directory handle
    closedir($handle);
}
?>

 

Save this script as a PHP file on your VPS and run it via cronjob to delete the tmp files automatically. Here's an example cronjob command to run the script every day at midnight:

 

0 0 * * * php /path/to/script.php

 

This command will run the PHP script at midnight every day, deleting all the tmp files in the specified directory.

ReplyQuote
Posted : 16/02/2023 2:53 am
Share:
×