Please write me a BASH script that classifies (recursively) the files of an input folder…
- User
Please write me a BASH script that classifies (recursively) the files of an input folder (that defaults to ~/Downloads). The script should create folder for each category found (Documents, Torrents, Music, Videos, Programs). Write also the commands necessary to ensure this script runs as a cronjob every day at noon.
- Assistant
Certainly! Here's a BASH script that classifies files in an input folder recursively and creates separate folders for each category (Documents, Torrents, Music, Videos, Programs). It also includes the commands to set it up as a cronjob to run every day at noon.
#!/bin/bash # Set the input folder (defaults to ~/Downloads) input_folder="${1:-$HOME/Downloads}" # Create category folders if they don't exist mkdir -p "${input_folder}/Documents" mkdir -p "${input_folder}/Torrents" mkdir -p "${input_folder}/Music" mkdir -p "${input_folder}/Videos" mkdir -p "${input_folder}/Programs" # Move files to respective category folders find "$input_folder" -type f -print0 | while IFS= read -r -d '' file; do case "$(file -b --mime-type "$file")" in application/pdf|text/*) mv "$file" "${input_folder}/Documents" ;; application/x-bittorrent) mv "$file" "${input_folder}/Torrents" ;; audio/*|*/ogg) mv "$file" "${input_folder}/Music" ;; video/*) mv "$file" "${input_folder}/Videos" ;; application/*) mv "$file" "${input_folder}/Programs" ;; esac done - User
fantastic, now rewrite this as a python script
- Assistant
Here is a Python script that classifies files based on their extensions and moves them into corresponding folders:
import os import shutil extensions = { 'Documents': ('.doc', '.docx', '.pdf', '.txt', '.rtf', '.xls', '.xlsx'), 'Torrents': ('.torrent',), 'Music': ('.mp3', '.wav', '.wma'), 'Videos': ('.mp4', '.avi', '.mkv', '.mov'), 'Programs': ('.exe', '.dmg', '.deb') } default_folder = os.path.join(os.path.expanduser('~'), 'Downloads') for filename in os.listdir(default_folder): source = os.path.join(default_folder, filename) if os.path.isfile(source): for folder, extension_list in extensions.items(): for extension in extension_list: if filename.endswith(extension): destination = os.path.join(default_folder, folder, filename) os.makedirs(os.path.join(default_folder, folder), exist_ok=True) shutil.move(source, destination) breakTo ensure that this script runs as a cronjob every day at noon, add the following line to your crontab file:
0 12 * * * /usr/bin/python3 /path/to/classify_files.pyMake sure to replace
/path/to/classify_files.pywith the actual path to your Python script.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)