Một doanh nghiệp nọ có nhu cầu dùng ảnh chân dung cho nhân viên, họ không muốn dùng AI mà muốn chụp ảnh thật. Số lượng chụp là cả trăm người nhưng họ muốn ảnh sau khi chụp sẽ được chia theo từng người một.
Cái này dễ. Nếu dùng CaptureOne, bạn chỉ cần ghi tên của người được chụp vào phần Headline chẳng hạn, khi xuất ảnh ra thì cho vào thư mục được đặt tên theo Headline.

Tất nhiên là làm như này sẽ phải nhập liệu thủ công (chép và dán), đôi khi có thể nhầm lẫn, thiếu sót so với một số giải pháp chuyên dụng là dùng máy quét mã vạch. Nhưng hiện tại không có cách nào khác rẻ mà hiệu quả hơn.
Ảnh sẽ được đẩy lên Google Drive rồi sau đó nhân viên truyền thông bên khách hàng thường sẽ gửi lại địa chỉ thư mục gốc, mọi người sẽ vào tìm thư mục theo tên của mình.
Nhưng nếu vì lí do “quyền riêng tư”, doanh nghiệp không muốn mọi người thấy thư mục ảnh của nhau thì sao? Lúc này nhân viên truyền thông sẽ phải chia sẻ thủ công từng thư mục một. Khoảng đôi ba chục người thì còn làm được, chứ hàng trăm người thì việc này hơi bị quá sức.
Rất may là có giao diện dòng lệnh có thể chia sẻ thư mục Google Drive. Với một chút hiểu biết về bash và sự trợ giúp của ChatGPT (nếu muốn), bạn có thể viết một đoạn mã chưa đến 100 dòng để giải quyết vấn đề quá sức trên.
#!/bin/bash
# Set the role for sharing folders
# default role: reader
# other roles: commenter, writer
sharing_role='reader'
# Do not edit below
root_folder_id="$1"
# Check if root_folder_id is empty
if [ -z "$root_folder_id" ]; then
echo "Usage: $0 <root_folder_id>"
exit 1
fi
# Try to get folder info
if output=$(gdrive files info "$root_folder_id" 2>/dev/null); then
printf "\n### Root folder info:\n\n"
echo "$output"
else
echo "Invalid folder ID or no access: $root_folder_id"
exit 1
fi
confirm_continue() {
echo ""
local prompt="${1:-Do you want to continue? [y/N]} "
read -r -p "$prompt" reply
case "$reply" in
[yY]|[yY][eE][sS])
echo "Continuing..."
;;
*)
echo "Aborted."
exit 1
;;
esac
}
printf "\nPlease check the root folder info above\n"
confirm_continue
# Getting list of sub-folder IDs
: > gfolders.txt
gdrive files list --parent "$root_folder_id" >> gfolders.txt
# Remove header line, clean up text
# portable sed for both macOS and Linux
sed -i.bak '1d' gfolders.txt && rm -f gfolders.txt.bak
sed -i.bak -E 's/[[:space:]]+folder[[:space:]]+.*//g; s/[[:space:]]{2,}/\t/g' \
gfolders.txt && rm -f gfolders.txt.bak
# Move name before ID
: > folders.txt
while IFS=$'\t' read -r col1 col2; do
if [ -n "$col1" ] && [ -n "$col2" ]; then
printf "%s\t%s\n" "$col2" "$col1" >> folders.txt
fi
done < gfolders.txt
printf "\n### Sub-folder IDs:\n\n"
cat folders.txt
printf "\nPlease check the list of sub-folders above\n"
confirm_continue
# Merging: Name - Email - Folder ID
sort -t $'\t' -k1,1 emails.txt > emails.sorted
sort -t $'\t' -k1,1 folders.txt > folders.sorted
# Use outer join (-a1 -a2) to detect mismatches, fill missing with "MISSING"
join -t $'\t' -a1 -a2 -e "MISSING" -o 0,1.2,2.2 emails.sorted folders.sorted > list.txt
printf "\n### Merged list:\n\n"
cat list.txt
printf "\nReady to share\n"
confirm_continue
# Create log file
logfile=$(date +"%Y%m%d-%H%M").log
touch "$logfile"
# Sharing
while IFS=$'\t' read -r name email folder_id; do
if [ "$email" != "MISSING" ] && [ "$folder_id" != "MISSING" ]; then
gdrive share --role "$sharing_role" --type user --email "$email" "$folder_id"
printf "%s\t%s\n" "$name" "$email" >> "$logfile"
else
printf "Skipping: %s (email=%s, folder=%s)\n" "$name" "$email" "$folder_id" | tee -a "$logfile"
fi
done < list.txt
printf "\nDONE!\n"