Skip to content

Instantly share code, notes, and snippets.

@dnestoff
Last active January 12, 2023 17:45
Show Gist options
  • Save dnestoff/61211b5897ffe1602ef2900aa20657e1 to your computer and use it in GitHub Desktop.
Save dnestoff/61211b5897ffe1602ef2900aa20657e1 to your computer and use it in GitHub Desktop.
Shell scripting blocks that I'm commonly re-using.
# add key name as first parameter WITHOUT file extension (don't add .pem)
key=$1
pem_file="${key}.pem"
pub_file="${key}.pub"
chmod 400 $pem_file
ssh-keygen -y -f $pem_file > $pub_file
ls .
#!/bin/bash
path=$1
if [ $# -eq 0 ]
then
echo "No folder path supplied"
echo "Please enter a folder path."
echo "exiting..."
exit
fi
# Change to the root folder
cd $path
# Find all empty folders in the nested folder structure and delete them
# This script first changes the current working directory to the root folder using the cd command.
# It then uses the find command to search for all directories (-type d) that are empty (-empty)
# in the nested folder structure and deletes them using the -delete option.
find . -type d -empty -delete
# This script will recursively delete all directories and their contents.
# Be careful when using the rm -r command, as it can delete a lot of files and folders very quickly and may not prompt for confirmation.
# find . -type d -exec rm -r {} \;
#find ip address of a machine in the terminal
ifconfig en0 inet
# # put current date as yyyy-mm-dd in $date
date=$(date '+%Y-%m-%d')
# put current date as yyyy-mm-dd HH:MM:SS in $date
date=$(date '+%Y-%m-%d_%H%M%S')
# print current date directly
echo $(date '+%Y-%m-%d')
output_file="audits_${date}.csv"
#!/bin/bash
path=$1
# Set the variable
delete_folders=true
if [ $# -eq 0 ]
then
echo "No folder path supplied"
echo "Please enter a folder path."
echo "exiting..."
exit
fi
# This script first changes the current working directory to the root folder
# using the cd command. It then uses a for loop to iterate over each of the
# items in the root folder (*). The if statement checks if the current item
# is a directory (-d "$folder") using the test command ([ ]). If the item is
# a directory, it will be processed in some way. In this example, the script
# simply echoes the name of the sub-folder.
# Change to the root folder
cd $path
# Loop through each sub-folder
# for folder in *; do
# if [ -d "$folder" ]; then
# # Do something with the sub-folder
# echo "Processing sub-folder: $folder"
# fi
# done
# This script will change into each sub-folder, list the files, copy them to
# another location, and then change back to the root folder.
for folder in *; do
if [ -d "$folder" ]; then
echo "Processing sub-folder: $folder"
# Change into the sub-folder
cd "$folder"
# Count the number of files (excluding hidden files) in the nested folder structure and print the result
file_count=$(find . -not -name ".*" -type f | wc -l)
echo "There are $file_count files in the nested folder: $folder."
# Move all files in nested folders to the root
sh ~/Desktop/Code/Shell_scripts/move_files_to_root_folder.sh .
# Delete empty folders, check if the variable is equal to true
if [ "$delete_folders" = true ]; then
sh ~/Desktop/Code/Shell_scripts/delete_folders.sh .
fi
# Change back to the root folder
cd ..
fi
done
# IF statements
if [[ $var = "All" ]]
then
echo "it's all!"
elif [[ $var = "Some" ]]
then
echo "it's some :-E"
else
echo "neither of those..."
fi
# check arguments given as STDIN
if [ $# -eq 0 ]
then
echo "No arguments supplied, exiting..."
exit
fi
#!/bin/bash
path=$1
if [ $# -eq 0 ]
then
echo "No folder path supplied"
echo "Please enter a folder path."
echo "exiting..."
exit
fi
# Change to the root folder
cd $path
# Find all files in the nested folder structure and move them to the root folder
# Note that this script will overwrite any existing files in the root folder
# with the same name as a file being moved.
find . -type f -exec mv {} . \;
#If you want to preserve the
# original files and rename the moved files to avoid conflicts, you can
# modify the mv command to use the -i option (interactive mode) and add a -n
# option (no clobber) to prevent overwriting. For example:
# find . -type f -exec mv -in {} . \;
# This will prompt the user to confirm whether to overwrite any existing
# files with the same name. You can also add a prefix or suffix to the moved
# files to distinguish them from the original files. For example:
# find . -type f -exec mv -in {} . \_moved \;
filename=$1
cd ~/Desktop/Mongo/CSV_data_pulls && ls
cp $filename ~/Downloads
# concatenate and interpolate strings
field_file_dir=/path/to/directory
field_file=_field_audits_v1.txt
field_file_path="${field_file_dir}/${field_file}"
echo $field_file_path
output_file_dir=/path/to/directory
output_file="audits_${date}.csv"
output_file_path="${output_file_dir}/${output_file}"
echo $output_file_path
#BASH
SECONDS=0;
while sleep .5 && ((SECONDS <= 60)); do
printf '\r%s: %2d' "One moment please" "$((60-SECONDS))"
done
printf '\n'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment