I need help with a bash script that loops through a directory and its subdirectories,…
- User
I need help with a bash script that loops through a directory and its subdirectories, looking for files with a specific extension (e.g., .txt). For each file found, the script should extract the date it was last modified, and create a new directory named after that date (if it doesn't already exist). The script should then move the file into the directory named after its modification date. Can you help me write this script in bash?
- Assistant
Sure! Here's a bash script that implements the functionality you described:
code#!/bin/bash # specify the directory to search and the file extension to look for search_dir="./" file_ext=".txt" # loop through all files in the search directory and its subdirectories for file in $(find $search_dir -type f -name "*$file_ext"); do # extract the date the file was last modified file_date=$(date -r "$file" +%Y-%m-%d) # create a directory named after the modification date, if it doesn't already exist if [ ! -d "$file_date" ]; then mkdir "$file_date" fi # move the file into the directory named after its modification date mv "$file" "$file_date" doneThis script uses the find command to search the specified directory (search_dir) and its subdirectories for files with the specified extension (file_ext). For each file found, it extracts the modification date using the date command, and creates a directory named after that date using the mkdir command (if it doesn't already exist). Finally, it moves the file into the directory named after its modification date using the mv command.
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 →