Created
November 5, 2019 08:06
-
-
Save temberature/89de7110008be522fd05b4718be4744d to your computer and use it in GitHub Desktop.
move file one by one
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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