Skip to content

Instantly share code, notes, and snippets.

@temberature
Created November 5, 2019 08:06
Show Gist options
  • Save temberature/89de7110008be522fd05b4718be4744d to your computer and use it in GitHub Desktop.
Save temberature/89de7110008be522fd05b4718be4744d to your computer and use it in GitHub Desktop.
move file one by one
#!/usr/bin/env bash
## This is the target path, the directory
## you want to copy to.
target=$2;
## Find all files and folders in the current directory, sort
## them reverse alphabetically and iterate through them
find $1 -maxdepth 1 -type f | sort -r | while IFS= read -r file; do
## Set the counter back to 0 for each file
counter=0;
## The counter will be 0 until the file is moved
while [ $counter -eq 0 ]; do
## If the directory has no files
if find "$target" -maxdepth 0 -empty | read;
then
## Move the current file to $target and increment
## the counter.
mv -v "$file" "$target" && counter=1;
else
## Uncomment the line below for debugging
# echo "Directory not empty: $(find "$target" -mindepth 1)"
## Wait for one second. This avoids spamming
## the system with multiple requests.
sleep 1;
fi;
done;
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment