
我是新來的,目前正在進行一個小型專案。
我需要在 bash 中編寫一個腳本,以便每次將文件放入資料夾時掃描該資料夾。在第二部分中,它應該將其移至使用該檔案使用的名稱建立的新目錄中。
我想用因克朗或者手錶但我不知道這是否是一個好的解決方案。計劃是這樣的。
directory="/usr/share/docker-compose"
if "*.yml" exist; then
do
move /usr/share/used-images
提前致謝。
答案1
您可以使用inotifywait。範例腳本:
#!/bin/bash
watchdir="$1"
if ! [ -d "$watchdir" ]; then
echo "Dir $watchdir doesn't exist"
exit 1
fi
while file=$(inotifywait --format "%f" -e 'create' -e 'moved_to' "$watchdir"); do
if [ -f "$watchdir/$file" ]; then
tmpname=$(tempfile -d "$watchdir")
mv "$watchdir/$file" "$tmpname"
mkdir "$watchdir/$file"
mv "$tmpname" "$watchdir/$file/$file"
# YOURCOMMANDS
fi
done