Skip to content
TheChatArchiveThe Chat Archive
Human-written demonstration (OpenAssistant volunteers)· OpenAssistant (human-written)Hugging Face 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

    Sure, here is a basic bash script that classifies files based on their extension. This script classifies documents (.doc, .docx, .txt, .pdf), torrents (.torrent), music (.mp3, .wav), videos (.mp4, .avi, .mkv), and programs (.exe, .dmg).

    #!/bin/bash
    
    # Input directory
    dir=${1:-"~/Downloads"}
    
    # Convert the path to absolute
    dir=$(readlink -f "$dir")
    
    # Folders to organize files
    declare -A folders
    folders=(
      ["Documents"]="doc docx txt pdf"
      ["Torrents"]="torrent"
      ["Music"]="mp3 wav"
      ["Videos"]="mp4 avi mkv"
      ["Programs"]="exe dmg"
    )
    
    # Create folders
    for folder in "${!folders[@]}"; do
      mkdir -p "${dir}/${folder}"
    done
    
    # Organize files
    shopt -s globstar nullglob
    
    for file in "$dir"/**/*.*; do
      if [[ -f $file ]]; then
        extension="${file##*.}"
        for folder in "${!folders[@]}"; do
          for ext in ${folders[$folder]}; do
            if [[ "$ext" == "$extension" ]]; then
              mv "$file" "${dir}/${folder}/"
              break 2
            fi
          done
        done
      fi
    done
    

    This script can be saved as organize_files.sh, and make sure to give it executable permissions with the command:

    chmod +x organize_files.sh
    

    To run this script daily at noon using cron, you can add a cron job by opening the crontab editor:

    crontab -e
    

    Then, add the following line in the crontab file, replacing /path/to/script with the absolute path where you saved the organize_files.sh script:

    0 12 * * * /bin/bash /path/to/script/organize_files.sh
    

    This means that the script will be run every day at 12:00. Make sure the script has executable permissions and that you've specified the correct path in the crontab file.

  3. User

    What does the shopt -s globstar nullglob command do?

  4. Assistant

    The built-in command shopt is used to set (-s) or unset (-u) Bash shell options (in the current shell). Your particular example would set the two options globstar and nullglob:

    • globstar makes the wildcard ** in filename expansion evaluate to matching filenames found by recursively descending into subdirectories to any level - for example, it might allow you to use something like "ls **/test.txt" to find file test.txt within any sub-subdirectory of the current directory. By default this option is disabled.
    • nullglob makes the wildcard * in filename expansion evaluate to "" (empty string) if no filenames are matching. By default this option is disabled, which, for example, means that "ls *.txt" would not be expanded at all if there were no files with extension .txt in the current directory, making ls complain about "No such file or directory *.txt" rather than just show an empty file list.

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