Skip to content
The Chat Archive
Human-written demonstration (OpenAssistant volunteers)· OpenAssistant (human-written) OpenAssistant Conversations (oasst2)

Please write me a BASH script that classifies (recursively) the files of an input folder…

In OpenAssistant data, “assistant” turns were written by human volunteers as demonstrations. They are not model outputs.
  1. 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.

  2. 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
    

    To set up the script as a cronjob to run every day at noon, follow these steps:

    1. Save the script to a file, for example, file_classification.sh.
    2. Make the script executable by running chmod +x file_classification.sh.
    3. Open your crontab for editing by running crontab -e.
    4. Add the following line to the crontab file:
    0 12 * * * /path/to/file_classification.sh
    

    Replace /path/to/file_classification.sh with the actual path where you saved the script.

    1. 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.

  3. User

    fantastic, now rewrite this as a python script

  4. 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'))
    

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.