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
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 - User
What does the shopt -s globstar nullglob command do?
- Assistant
The command shopt -s globstar nullglob sets two shell options in bash: globstar and nullglob.
The globstar option enables the use of ** in filename patterns to match all files and zero or more directories and subdirectories. For example, echo **/*.txt would print all the .txt files in the current directory and its subdirectories.
The nullglob option prevents filename patterns that don’t match any files from expanding to themselves, and instead expands them to an empty list. For example, without nullglob, echo *.foo would print *.foo if there are no .foo files in the current directory, but with nullglob, it would print nothing.
These options can be useful for writing scripts that need to handle files recursively or avoid errors when no files match a pattern. However, they can also change the behavior of some commands or cause unexpected results if not used carefully.
To unset these options, you can use shopt -u globstar nullglob.
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)