Skip to content

Instantly share code, notes, and snippets.

@thiegomoura
Created June 28, 2023 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thiegomoura/6f8fffffa8de7f481f5b6378f3bc9b9d to your computer and use it in GitHub Desktop.
Save thiegomoura/6f8fffffa8de7f481f5b6378f3bc9b9d to your computer and use it in GitHub Desktop.
A shell script to copy txt files
if [ $# -ne 2 ]; then
echo "Invalid parameters! Use: $0 <origem> <destination>"
exit 1
fi
origen="$1"
destination="$2"
# check if origen directory exits
if [ ! -d "$origen" ]; then
echo "Origem directory do not exists"
exit 1
fi
# check if exists files in origen directory
files=( $(find "$origen" -maxdepth 1 -type f -name "*.txt") )
if [ ${#files[@]} -eq 0 ]; then
echo "Not exits files to copy in this origen directory."
exit 1
fi
# check if destination directory exits and create one if not
if [ ! -d "$destination" ]; then
mkdir -p "$destination"
fi
# check if origem not equal to destination
if [ "$origen" -ef "$destination" ]; then
echo "Error: origen and destination are the same directory."
exit 1
fi
cp "$origen"/*.txt "$destination"
# check if copy has been sucessfull
if [ $? -eq 0 ]; then
echo "Copy files finished with success."
# Ask about files exclusion from origem folder
read -p "You want to exclude the files from origem? (y/n): " confirmation
while [[ ! $confirmation =~ ^[YyNn]$ ]]; do
read -p "Please, answer only with 'y' for yes or 'n' for not: " confirmation
done
if [ "$(echo "$confirmation" | tr '[:upper:]' '[:lower:]')" = "y" ]; then
rm "$origen"/*.txt
echo "Files succesfull excluded."
else
echo "Files not excluded."
fi
else
echo "Fail during the copy of files."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment