User Agents

Web browsers and everyday web tech, explained

Rename Duplicate Chrome Downloads with Puppeteer on Ubuntu

· 5 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide shows you how to use Puppeteer, a Node.js library that controls headless Chrome, to automatically detect and rename duplicate files in your Chrome downloads folder on Ubuntu. Instead of manually sorting files like file (1).zip, file (2).zip, etc., a script can rename them to something meaningful, like adding a timestamp or a sequence number. You'll need basic familiarity with the Linux command line and Node.js installed.

What You'll Need

  • Ubuntu (20.04 or newer recommended)
  • Node.js and npm (install with sudo apt install nodejs npm if not present)
  • Google Chrome or Chromium installed (Puppeteer can also download its own browser, but using the system one is faster)
  • A text editor to write the script

Step-by-Step

  1. Set up a project directory. Open a terminal and run:
    mkdir puppeteer-rename && cd puppeteer-rename
    npm init -y
  2. Install Puppeteer. Run npm install puppeteer. This downloads a compatible Chromium version by default. To use your system Chrome, set the PUPPETEER_EXECUTABLE_PATH environment variable or specify executablePath in the script.
  3. Create the script file. Create a file named rename-duplicates.js with a text editor (e.g., nano rename-duplicates.js).
  4. Write the basic Puppeteer launch code. Start with:
    const puppeteer = require('puppeteer');
    const fs = require('fs');
    const path = require('path');
    
    (async () => {
      const browser = await puppeteer.launch({ headless: true });
      // ... rest of the script
      await browser.close();
    })();
  5. Define the downloads folder path. Add const downloadsFolder = path.join(process.env.HOME, 'Downloads'); to point to your default downloads directory.
  6. Read the list of files. Use fs.readdirSync(downloadsFolder) to get an array of file names.
  7. Identify duplicates. Write a function that groups files by their base name (before the extension) and filters out groups with more than one file. For example, report.pdf and report (1).pdf share the base report.
  8. Rename the duplicates. For each duplicate set, decide on a new naming scheme. A common approach is to append a timestamp: report-20250115-143022.pdf. Use fs.renameSync(oldPath, newPath) to rename.
  9. Handle edge cases. Ensure the new file name doesn't already exist. If it does, add a counter (e.g., -1, -2). Also, skip files that are still being downloaded (check for .crdownload extension).
  10. Run the script. Execute node rename-duplicates.js in the terminal. It will launch a headless browser (which does nothing visible but is required for Puppeteer to work) and then rename files.

Common Mistakes

  • Not handling the case where the new filename already exists, causing an error or overwriting a file.
  • Renaming files that are still downloading (partial files with .crdownload).
  • Using a regex that incorrectly matches non-duplicate files (e.g., files with parentheses in their original names).
  • Forgetting to close the browser, leaving orphan processes.
  • Running the script from a different directory, causing the downloads path to be wrong.

Tips

  • Test the script on a copy of your Downloads folder first to avoid accidental data loss.
  • Use a dry-run mode: instead of renaming, just log what would be renamed. This helps verify the logic.
  • Add a filter to only rename specific file types (e.g., .pdf, .zip) to avoid touching unrelated files.
  • Consider using a more descriptive pattern like filename-YYYYMMDD-HHMMSS.ext to make files sortable by date.
  • If you have many files, use fs.readdirSync with { withFileTypes: true } to filter directories out.
  • For a more advanced solution, you could use Puppeteer to monitor the downloads folder in real time, but a simple one-time script is usually sufficient.

Troubleshooting

  • Puppeteer fails to launch: If you get a missing dependency error, install required libraries with sudo apt install -y libx11-xcb1 libxcb-dri3-0 libxcomposite1 libxdamage1 libxi6 libxtst6 libnss3 libxrandr2 libasound2 libpangocairo-1.0-0 libatk1.0-0 libatk-bridge2.0-0 libcups2 libdrm2 libxkbcommon0 libxss1 libgbm1.
  • Script runs but nothing happens: Check that your Downloads folder actually contains duplicates. Add a console log to print the list of files found.
  • Permission denied when renaming: Ensure you have write permissions to the Downloads folder. Run chmod u+w ~/Downloads if needed.
  • Node.js not found: If you installed Node.js via nvm, make sure the nvm environment is loaded in your terminal session.
  • Error: ENOENT when renaming: This usually means the source file doesn't exist. Double-check the path and that the file hasn't been moved.
  • Puppeteer downloads a new Chromium every time: To avoid this, set the environment variable PUPPETEER_SKIP_DOWNLOAD=true before installation and specify the executable path in the script.

Frequently Asked Questions

Why do I need Puppeteer for renaming files? I can just use a shell script.

You're right; a simple bash script can rename duplicates. However, Puppeteer is useful if you want to integrate with a web application or if you need to interact with the browser (for example, to trigger downloads from a page and then rename them). This guide focuses on using Puppeteer as a learning example, but the same logic can be adapted to a plain Node.js script without Puppeteer.

Can I use this script to rename files in other folders?

Yes, just change the downloadsFolder variable to point to any directory you want.

Will this script overwrite existing files?

No, if you implement the check for existing names as described, it will skip or increment a counter. Always test on a copy first.

How do I run the script automatically after each download?

You can set up a cron job or use an inotify watcher to trigger the script whenever a new file appears in the Downloads folder. But that's beyond the scope of this guide.

What if my downloads have different extensions like .tar.gz?

The script should handle multi-part extensions if you use path.extname carefully. For .tar.gz, path.extname returns .gz, so you might want to strip the last two extensions. Adjust the base-name extraction accordingly.

Is it safe to run Puppeteer on a headless server?

Yes, Puppeteer runs headless by default. It doesn't need a display, but it does require the necessary system libraries. The troubleshooting section lists them.