gpt-4-0314· openai
Suzhen/CodeChat
could you write a bash script that lists the subdirectories in a specified directory?
- User
could you write a bash script that lists the subdirectories in a specified directory?
- Assistant
Sure, here's a simple bash script that takes a directory path as an argument and lists its subdirectories:
bash#!/bin/bash # Check if the argument is provided if [ -z "$1" ]; then echo "Usage: $0 <directory_path>" exit 1 fi # Assign the argument to a variable dir="$1" # Check if the directory exists if [ ! -d "$dir" ]; then echo "Directory not found: $dir" exit 1 fi # List subdirectories echo "Subdirectories of $dir:" find "$dir" -mindepth 1 -maxdepth 1 -type d exit 0Save this script as list_subdirs.sh and make it executable using the following command:
bashchmod +x list_subdirs.shNow you can run the script with a directory path as an argument:
bash./list_subdirs.sh /path/to/directory - User
could you write a bash script that lists the three latest subdirectories in a specified directory?
- Assistant
Sure, here's a bash script that lists the three latest subdirectories in a specified directory:
bash#!/bin/bash # Check if the argument is provided if [ -z "$1" ]; then echo "Usage: $0 <directory_path>" exit 1 fi # Assign the argument to a variable dir="$1" # Check if the directory exists if [ ! -d "$dir" ]; then echo "Directory not found: $dir" exit 1 fi # List the three latest subdirectories echo "Three latest subdirectories of $dir:" find "$dir" -mindepth 1 -maxdepth 1 -type d -print0 | xargs -0 ls -td | head -n 3 exit 0Save this script as list_latest_subdirs.sh and make it executable using the following command:
bashchmod +x list_latest_subdirs.shNow you can run the script with a directory path as an argument:
bash./list_latest_subdirs.sh /path/to/directory