Technology Encyclopedia Home >How to delete files on a Linux server?

How to delete files on a Linux server?

To delete files on a Linux server, you can use the rm (remove) command. Here's how it works:

  1. Basic Syntax:

    rm [options] filename
    

    Example:

    rm oldfile.txt  
    

    This deletes oldfile.txt in the current directory.

  2. Delete Multiple Files:

    rm file1.txt file2.txt file3.txt  
    
  3. Recursive Deletion (for Directories):
    Use -r (or --recursive) to delete a directory and its contents:

    rm -r mydirectory/  
    
  4. Force Deletion (no Confirmation Prompt):
    Use -f (or --force) to skip confirmation warnings:

    rm -f importantfile.txt  
    
  5. Combine Options (Recursive + Force):

    rm -rf mydirectory/  
    

    ⚠️ Warning: rm -rf is powerful and can permanently delete data. Use with caution.

Example Scenario:

If you want to delete all .log files in /var/log/myapp/ without confirmation:

rm -f /var/log/myapp/*.log  

For secure file deletion (overwriting data before removal), consider tools like shred:

shred -u sensitivefile.txt  

If managing files on a Tencent Cloud Linux server, you can execute these commands via SSH or use Tencent Cloud Console > Cloud Virtual Machine (CVM) > Login to Server. For automated cleanup, consider Tencent Cloud Batch Compute or Serverless Cloud Function (SCF) for scheduled tasks.