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 doneTo set up the script as a cronjob to run every day at noon, follow these steps:
- Save the script to a file, for example, file_classification.sh.
- Make the script executable by running chmod +x file_classification.sh.
- Open your crontab for editing by running crontab -e.
- Add the following line to the crontab file:
0 12 * * * /path/to/file_classification.shReplace /path/to/file_classification.sh with the actual path where you saved the script.
- Save the crontab file and exit the editor.
This cronjob will execute the script every day at noon (12:00 PM) and classify the files in the specified folder.
Please make sure to replace /path/to/file_classification.sh with the actual path where you saved the script, and modify the script if you want to use a different set of category folders or add additional file types to the categories.
- User
fantastic, now rewrite this as a python script
- Assistant
Absolutely! Here is your Bash script written in a Python.
import os import shutil import mimetypes from pathlib import Path # Set the input folder (defaults to ~/Downloads) input_folder = Path(os.environ.get('1', os.path.join(os.environ['HOME'], 'Downloads'))) # Create category folders if they don't exist (input_folder / 'Documents').mkdir(exist_ok=True) (input_folder / 'Torrents').mkdir(exist_ok=True) (input_folder / 'Music').mkdir(exist_ok=True) (input_folder / 'Videos').mkdir(exist_ok=True) (input_folder / 'Programs').mkdir(exist_ok=True) # Move files to respective category folders for file in input_folder.glob('*'): if file.is_file(): mime_type, _ = mimetypes.guess_type(file) if mime_type: if mime_type.startswith('application/pdf') or mime_type.startswith('text'): shutil.move(str(file), str(input_folder / 'Documents')) elif mime_type.startswith('application/x-bittorrent'): shutil.move(str(file), str(input_folder / 'Torrents')) elif mime_type.startswith('audio') or mime_type.endswith('/ogg'): shutil.move(str(file), str(input_folder / 'Music')) elif mime_type.startswith('video'): shutil.move(str(file), str(input_folder / 'Videos')) elif mime_type.startswith('application'): shutil.move(str(file), str(input_folder / 'Programs'))
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)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →