DEV Community

Cover image for How I Write Easy CLI Scripts to Automate My Life.
Jessica Wang
Jessica Wang

Posted on • Updated on

How I Write Easy CLI Scripts to Automate My Life.

Image description

I take hundreds of screenshots a day, and I always forget to delete them. They end up taking up a lot of memory on my machine, and sometimes I can't just bulk highlight & delete in fear of deleting important pictures & videos that I actually need.

Here's how I wrote a script to automate this for me.

If you don't like reading, here's a video tutorial you can watch:

Before Getting Started

I want to preface this by saying this tutorial heavily hinges on me using Warp AI, which is basically a ChatGPT integration built into the terminal that quickly generates answers/code snippets to any question.

If you want to follow along, you'll need to be using the Warp terminal app which you can download for free here.

In the next section, I'll be putting the prompt (what I asked Warp AI) and what Warp AI's output was. I basically just followed those instructions to write my script in under 5 minutes.

A Summary Of My Process

So my process for writing this script was just:

  • Asking Warp AI how to do something.
  • Copying the output it gave me (code blocks + commands to run).
  • Checking to see if it worked.
  • Asking Warp AI about any errors/getting it to tweak its output.

In the section below, I'll include the entire transcript of my interaction with Warp AI as I was building my script. This probably took me less than 5 minutes. Here's a summary if you don't want to read the whole transcript.


1) Ask Warp AI how to write a script that deletes all screenshots from my Documents directory.

2) Copying the output, but realizing it looks for everything named "screenshots.png" whereas my screenshots were named "Screen Shot XYZ.png"

3) Asking Warp AI to tweak its output to look for the phrase Screen Shot and also be case insensitive.

4) Getting base functionality working, and then improving on it by getting the script to print out the names of files it was deleting.

5) Asking Warp AI to explain some of the outputs so I can understand what my script is doing & learn in the process.


Warp AI Transcripts (Ok To Skip to Conclusion)

Prompt: Write me a script that deletes all screenshots from my Documents directory

Warp AI: Sure! Here's a script that will delete all screenshots from your Documents directory:

#!/bin/bash

cd ~/Documents
rm -f *screenshot*.png

Enter fullscreen mode Exit fullscreen mode

Save this script as a file with a .sh extension (e.g. delete_screenshots.sh) and make it executable with the following command:

chmod +x delete_screenshots.sh

Enter fullscreen mode Exit fullscreen mode

Then, you can run the script in the terminal with the following command:

./delete_screenshots.sh

Enter fullscreen mode Exit fullscreen mode

This will change to your Documents directory and delete all files with "screenshot" in the filename and a ".png" extension. Be careful when running this script, as it will permanently delete these files without prompting for confirmation.

Prompt: do I have to run chmod +x command every time I edit the .sh file?

Warp AI: No, you don't have to run the chmod +x command every time you edit the .sh file. The chmod +x command only needs to be run once to make the script executable.

However, if you make changes to the script and save the file, you will need to run the script again to see the effects of the changes. If you have already made the script executable with chmod +x, you can simply run the script again with ./script_name.sh to execute the updated version of the script.

If you make changes to the script and want to see the effects of the changes without having to run the chmod +x command again, you can use the following command to reload the script in the current shell session:

source script_name.sh

Enter fullscreen mode Exit fullscreen mode

This will reload the script and apply any changes you have made to it, without requiring you to make the script executable again.

Prompt: Can you edit the script above to delete anything with file name that includes "Screen Shot" and is case insensitive?

Warp AI: Yes, here's an updated script that will delete all files with "Screen Shot" in the filename (case insensitive) from your Documents directory:

#!/bin/bash

cd ~/Documents
shopt -s nocaseglob
rm -f *Screen\\ Shot*

Enter fullscreen mode Exit fullscreen mode

The shopt -s nocaseglob command enables case-insensitive globbing, which means that the *Screen\\ Shot* pattern will match filenames with "Screen Shot" in any combination of upper and lower case letters.

Save this script as a file with a .sh extension (e.g. delete_screen_shots.sh) and make it executable with the following command:

chmod +x delete_screen_shots.sh

Enter fullscreen mode Exit fullscreen mode

Then, you can run the script in the terminal with the following command:

./delete_screen_shots.sh

Enter fullscreen mode Exit fullscreen mode

This will change to your Documents directory and delete all files with "Screen Shot" in the filename (case insensitive). Be careful when running this script, as it will permanently delete these files without prompting for confirmation.

Prompt: can you edit this script above to output the names of all the screenshots it deleted, each on their own line?

Warp AI: Yes, here's an updated script that will delete all files with "Screen Shot" in the filename (case insensitive) from your Documents directory and output the names of the deleted files:

#!/bin/bash

cd ~/Documents
shopt -s nocaseglob
deleted_files=(*Screen\\ Shot*)
rm -f "${deleted_files[@]}"
printf "Deleted files:\\n%s\\n" "${deleted_files[@]}"

Enter fullscreen mode Exit fullscreen mode

The deleted_files=(*Screen\\ Shot*) line creates an array of all files with "Screen Shot" in the filename (case insensitive), and the rm -f "${deleted_files[@]}" line deletes all of these files. Finally, the printf command outputs the names of the deleted files.

Save this script as a file with a .sh extension (e.g. delete_screen_shots.sh) and make it executable with the following command:

chmod +x delete_screen_shots.sh

Enter fullscreen mode Exit fullscreen mode

Then, you can run the script in the terminal with the following command:

./delete_screen_shots.sh

Enter fullscreen mode Exit fullscreen mode

This will change to your Documents directory, delete all files with "Screen Shot" in the filename (case insensitive), and output the names of the deleted files. Be careful when running this script, as it will permanently delete these files without prompting for confirmation.

Prompt: What does the \n%s\n do?

Warp AI: The \\n characters in the printf command are escape sequences that represent a newline character, which causes the output to be formatted with each deleted filename on a separate line.

The %s format specifier in the printf command is a placeholder for a string value, which is replaced by the values of the deleted_files array. The @ symbol after the array name tells printf to expand the array into separate arguments, with each element of the array being treated as a separate string value.

So, the printf command with the %s specifier and the @ symbol expands the deleted_files array into separate string arguments, which are then printed on separate lines with a newline character before and after each string.

Conclusion

Image description

And in the end, I now have a working script that I run pretty easily from the command line.

It's crazy that I can so easily write bash scripts without knowing anything about CLI scripting.

Warp AI is OP.

Top comments (0)