{"id":23375,"date":"2026-08-20T20:24:18","date_gmt":"2026-08-20T14:24:18","guid":{"rendered":"https:\/\/mcqacademy.com\/en\/?p=23375"},"modified":"2026-08-20T20:24:19","modified_gmt":"2026-08-20T14:24:19","slug":"how-to-shorten-the-cwebp-command-in-linux-mint-with-a-bash-function","status":"publish","type":"post","link":"https:\/\/mcqacademy.com\/en\/how-to-shorten-the-cwebp-command-in-linux-mint-with-a-bash-function\/2026\/","title":{"rendered":"How to Shorten the cwebp Command in Linux Mint with a Bash Function"},"content":{"rendered":"\n<p>I usually optimize images locally before uploading them to the live servers for my websites. Since I use Linux Mint, one of my regular tools for this job is <code>cwebp<\/code>, which I run directly from the command line to convert PNG and JPG images to WebP.<\/p>\n\n\n\n<p>My most-used command is simple:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cwebp image-name.png -o image-name.webp<\/code><\/pre>\n\n\n\n<p>It works well, but after using it again and again, I started looking for a way to avoid typing the same filename twice every time I converted an image. When filenames are long, that small repetition becomes even more annoying.<\/p>\n\n\n\n<p>Because I already use the Linux terminal for this task, I wanted a solution that would stay simple and keep using <code>cwebp<\/code> rather than adding another application. I solved it by creating a small Bash function called <code>towebp<\/code>.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>Now, instead of writing the full command, I can simply run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>towebp image-name.png<\/code><\/pre>\n\n\n\n<p>and Bash automatically creates:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>image-name.webp<\/code><\/pre>\n\n\n\n<p>This article explains the setup I use on Linux Mint, how the Bash function works, and how you can add the same shortcut to your own terminal.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Check That cwebp Is Installed<\/h2>\n\n\n\n<p>Before creating the shortcut, first confirm that <code>cwebp<\/code> is installed and available from your terminal.<\/p>\n\n\n\n<p>Run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>which cwebp<\/code><\/pre>\n\n\n\n<p>On my Linux Mint system, the output is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/usr\/bin\/cwebp<\/code><\/pre>\n\n\n\n<p>This confirms that the <code>cwebp<\/code> executable is installed and available in the shell path.<\/p>\n\n\n\n<p>If the command returns nothing, install the WebP command-line utilities before continuing.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Normal cwebp Command<\/h2>\n\n\n\n<p>The general <code>cwebp<\/code> command can include compression options and a quality value:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cwebp &#91;options] -q quality input.png -o output.webp<\/code><\/pre>\n\n\n\n<p>For a basic conversion, I usually use:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cwebp image-name.png -o image-name.webp<\/code><\/pre>\n\n\n\n<p>The command is easy enough, but the filename has to be written twice.<\/p>\n\n\n\n<p>For example, if the source image is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php-allowed-memory-size-exhausted-real-fix.png<\/code><\/pre>\n\n\n\n<p>the full command becomes:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cwebp php-allowed-memory-size-exhausted-real-fix.png -o php-allowed-memory-size-exhausted-real-fix.webp<\/code><\/pre>\n\n\n\n<p>That is more typing than I want for a task I perform regularly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Create a Short towebp Command with a Bash Function<\/h2>\n\n\n\n<p>Bash functions are a good fit for this because the shortcut needs to accept a filename, remove its current extension, and automatically create the WebP output filename.<\/p>\n\n\n\n<p>Open your Bash configuration file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nano ~\/.bashrc<\/code><\/pre>\n\n\n\n<p>Go to the very bottom of the file and add:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Convert image to WebP\ntowebp() {\n  cwebp \"$1\" -o \"${1%.*}.webp\"\n}<\/code><\/pre>\n\n\n\n<p>Then save the file in Nano:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Press <code>Ctrl + O<\/code><\/li>\n\n\n\n<li>Press <code>Enter<\/code><\/li>\n\n\n\n<li>Press <code>Ctrl + X<\/code><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Reload the Bash Configuration<\/h2>\n\n\n\n<p>After changing <code>~\/.bashrc<\/code>, reload it with:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>source ~\/.bashrc<\/code><\/pre>\n\n\n\n<p>You can also close the terminal and open a new one. In my case, I reopened the terminal to make sure the updated Bash configuration was loaded cleanly.<\/p>\n\n\n\n<p>That also resolved an initial <code>towebp: command not found<\/code> message I saw after adding the function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Use the New towebp Command<\/h2>\n\n\n\n<p>Once the function is loaded, switch to any directory that contains the image you want to convert.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cd ~\/Downloads\/website-images<\/code><\/pre>\n\n\n\n<p>Then run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>towebp file-name.png<\/code><\/pre>\n\n\n\n<p>If the source file is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>file-name.png<\/code><\/pre>\n\n\n\n<p>the function creates:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>file-name.webp<\/code><\/pre>\n\n\n\n<p>The original image remains unchanged.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How the Bash Function Works<\/h2>\n\n\n\n<p>The function contains this command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cwebp \"$1\" -o \"${1%.*}.webp\"<\/code><\/pre>\n\n\n\n<p>There are two important parts to understand.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What &#8220;$1&#8221; Means<\/h3>\n\n\n\n<p>Inside a Bash function, <code>$1<\/code> represents the first argument passed to the function.<\/p>\n\n\n\n<p>If you run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>towebp photo.png<\/code><\/pre>\n\n\n\n<p>then:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$1 = photo.png<\/code><\/pre>\n\n\n\n<p>The quotes around <code>\"$1\"<\/code> matter because they allow filenames containing spaces to be handled correctly.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What &#8220;${1%.*}&#8221; Means<\/h3>\n\n\n\n<p>The expression:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>${1%.*}<\/code><\/pre>\n\n\n\n<p>removes the shortest matching <code>.*<\/code> suffix from the filename. In this case, that effectively removes the existing file extension.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>photo.png<\/code><\/pre>\n\n\n\n<p>becomes:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>photo<\/code><\/pre>\n\n\n\n<p>The function then adds <code>.webp<\/code>, producing:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>photo.webp<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">It Also Works with JPG and JPEG Files<\/h2>\n\n\n\n<p>The function is not limited to PNG files.<\/p>\n\n\n\n<p>You can run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>towebp photo.jpg<\/code><\/pre>\n\n\n\n<p>and it creates:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>photo.webp<\/code><\/pre>\n\n\n\n<p>The same approach works with JPEG files:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>towebp photo.jpeg<\/code><\/pre>\n\n\n\n<p>which also creates:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>photo.webp<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Using Filenames with Spaces<\/h2>\n\n\n\n<p>The function can also handle filenames containing spaces, as long as you quote the filename when calling it.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>towebp \"My Website Image.png\"<\/code><\/pre>\n\n\n\n<p>This creates:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>My Website Image.webp<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Add a Default WebP Quality Setting<\/h2>\n\n\n\n<p>If you normally use the same quality setting for most of your website images, you can include that value directly in the function.<\/p>\n\n\n\n<p>For example, to always use quality 82:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Convert image to WebP using quality 82\ntowebp() {\n  cwebp -q 82 \"$1\" -o \"${1%.*}.webp\"\n}<\/code><\/pre>\n\n\n\n<p>Now this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>towebp image.png<\/code><\/pre>\n\n\n\n<p>runs the equivalent of:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cwebp -q 82 image.png -o image.webp<\/code><\/pre>\n\n\n\n<p>This is useful if you want to keep a consistent WebP compression setting across images prepared for your websites.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Troubleshooting: towebp Command Not Found<\/h2>\n\n\n\n<p>If you run the shortcut and see:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>towebp: command not found<\/code><\/pre>\n\n\n\n<p>first confirm that the function was actually saved in <code>~\/.bashrc<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>grep -n -A3 'towebp' ~\/.bashrc<\/code><\/pre>\n\n\n\n<p>You should see something similar to:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>120:towebp() {\n121-  cwebp \"$1\" -o \"${1%.*}.webp\"\n122-}<\/code><\/pre>\n\n\n\n<p>Next, check whether Bash has loaded the function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type towebp<\/code><\/pre>\n\n\n\n<p>If it is loaded correctly, Bash should report:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>towebp is a function<\/code><\/pre>\n\n\n\n<p>You can also check your <code>~\/.bashrc<\/code> file for Bash syntax errors:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>bash -n ~\/.bashrc<\/code><\/pre>\n\n\n\n<p>If there are no syntax errors, the command normally produces no output.<\/p>\n\n\n\n<p>If the function is present in <code>~\/.bashrc<\/code> but still is not available, close the current terminal window and start a new one. That fixed the problem in my setup.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Use a Bash Function Instead of an Alias?<\/h2>\n\n\n\n<p>A Bash alias is useful when you want to replace one fixed command with another fixed command. This shortcut needs a little more logic because it has to accept an input filename and build a new output filename automatically.<\/p>\n\n\n\n<p>A function handles that much more cleanly.<\/p>\n\n\n\n<p>It lets this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>towebp image.png<\/code><\/pre>\n\n\n\n<p>automatically behave like:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cwebp image.png -o image.webp<\/code><\/pre>\n\n\n\n<p>without requiring you to write the output filename yourself.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Final Bash Function<\/h2>\n\n\n\n<p>If you only need the basic version, this is the complete function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Convert image to WebP\ntowebp() {\n  cwebp \"$1\" -o \"${1%.*}.webp\"\n}<\/code><\/pre>\n\n\n\n<p>Add it to:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>~\/.bashrc<\/code><\/pre>\n\n\n\n<p>Then reload Bash:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>source ~\/.bashrc<\/code><\/pre>\n\n\n\n<p>or close the terminal and open a new one.<\/p>\n\n\n\n<p>After that, converting an image becomes:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>towebp file-name.png<\/code><\/pre>\n\n\n\n<p>and the output file is created as:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>file-name.webp<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>The standard <code>cwebp<\/code> command already does its job well, but if you use it frequently, repeating the same filename for input and output becomes unnecessary work.<\/p>\n\n\n\n<p>A small Bash function solves that neatly. The <code>towebp<\/code> command keeps the original filename, removes the old extension, and creates a WebP file with the same base name.<\/p>\n\n\n\n<p>For anyone who regularly prepares images locally before uploading them to WordPress or another website, this is a simple Linux Mint customization that makes the command-line workflow faster and easier to use.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to create a simple Bash function in Linux Mint that shortens the cwebp command and converts PNG or JPG images to WebP with less typing.<\/p>\n","protected":false},"author":4,"featured_media":23377,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_seopress_titles_title":"","_seopress_titles_desc":"","_seopress_robots_index":"","_seopress_robots_follow":"","_seopress_robots_imageindex":"","_seopress_robots_snippet":"","_seopress_robots_primary_cat":"","_seopress_robots_breadcrumbs":"","_seopress_robots_freeze_modified_date":"","_seopress_robots_custom_modified_date":"","_seopress_robots_canonical":"","_seopress_social_fb_title":"","_seopress_social_fb_desc":"","_seopress_social_fb_img":"","_seopress_social_fb_img_attachment_id":0,"_seopress_social_fb_img_width":0,"_seopress_social_fb_img_height":0,"_seopress_social_twitter_title":"","_seopress_social_twitter_desc":"","_seopress_social_twitter_img":"","_seopress_social_twitter_img_attachment_id":0,"_seopress_social_twitter_img_width":0,"_seopress_social_twitter_img_height":0,"_seopress_redirections_value":"","_seopress_redirections_enabled":"","_seopress_redirections_enabled_regex":"","_seopress_redirections_logged_status":"","_seopress_redirections_param":"","_seopress_redirections_type":0,"_seopress_analysis_target_kw":"","mcq_note":"","mcq_options":[],"mcq_multi_answer":"","mcq-view-count":0,"footnotes":""},"categories":[1871],"tags":[3294,3299,3300,3295,3298,3297,3224,3301,3296,3302,3303,3304],"class_list":["post-23375","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux-tutorials","tag-bash","tag-bash-function","tag-command-line","tag-cwebp","tag-image-conversion","tag-image-optimization","tag-linux-mint","tag-linux-terminal","tag-webp","tag-webp-conversion","tag-website-images","tag-wordpress-images"],"_links":{"self":[{"href":"https:\/\/mcqacademy.com\/en\/wp-json\/wp\/v2\/posts\/23375","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mcqacademy.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mcqacademy.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mcqacademy.com\/en\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/mcqacademy.com\/en\/wp-json\/wp\/v2\/comments?post=23375"}],"version-history":[{"count":2,"href":"https:\/\/mcqacademy.com\/en\/wp-json\/wp\/v2\/posts\/23375\/revisions"}],"predecessor-version":[{"id":23378,"href":"https:\/\/mcqacademy.com\/en\/wp-json\/wp\/v2\/posts\/23375\/revisions\/23378"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mcqacademy.com\/en\/wp-json\/wp\/v2\/media\/23377"}],"wp:attachment":[{"href":"https:\/\/mcqacademy.com\/en\/wp-json\/wp\/v2\/media?parent=23375"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mcqacademy.com\/en\/wp-json\/wp\/v2\/categories?post=23375"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mcqacademy.com\/en\/wp-json\/wp\/v2\/tags?post=23375"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}