Various actions with files and extensions in a Bash script

Introduction

Before reading this article make sure you are familiar with the content of the article «Bash basics»

Show the names of all files in a directory

Let us name the script that prints the names of all files in a directory list_sites.sh

#!/bin/bash for file in ./**; do echo "${file##/}" done

Let us apply this script to a directory sites of the following form

sites/ |-- aredel.com |-- heihei.ru |-- list_files.sh `-- testsetup.site

./list_files.sh

./aredel.com ./heihei.ru ./list_files.sh ./testsetup.site

Show files with a certain extension

If you need only files with the .jpg extension

#!/bin/bash for file in ./**.jpg; do echo "${file##/}" done

Get the file name without the extension

If you need only the file name without the extension

#!/bin/bash for file in ./**.jpg; do filename="${file%.*}" echo $filename done

Get only the file extension

If you need only the extension

#!/bin/bash for file in ./**.jpg; do extension="${file##*.}" echo $EXTension done

Convert images to .webp

Download webp

wget https://storage.googleapis.com/downloads.webmproject.org/releases/webp/libwebp-1.2.0-linux-x86-64.tar.gz
mkdir webp
cp libwebp-1.2.0-linux-x86-64.tar.gz webp/
tar -xvf libwebp-1.2.0-linux-x86-64.tar.gz
cp -r libwebp-1.2.0-linux-x86-64 libwebp-current
cd libwebp-current/bin
pwd
vi ~/.bashrc

export PATH=$PATH:/home/andrei/webp/libwebp-current/bin

#!/bin/bash for file in ./**.jpg; do filename="${file%.*}" newfile=$filename".webp" cwebp -q 60 $file -o $newfile done

Add a suffix to file names

If you need to keep the file extension and add some word or character to the original name. And do it with all files of this type in the directory

For example, the directory has the files a.jpg , b.jpg , c.jpg , and you need to turn them into a--small.jpg , b--small.jpg , c--small.jpg

#!/bin/bash POSTFIX="--small" # what you want to add EXT=".jpg" # to which files for file in ./**$EXT; do fullname="${file##/}" filename="${file%.*}" temp=$filename$POSTFIX mv $file ${temp}$EXT done

And now a script that does both, and before doing it checks whether such a file already exists

#!/bin/bash # # After this script is used all .jpg and .png images that did not have # --small. versions will get one # afterwards all .jpg and .png images will get their .webp version # including those --small version that were created by this script # # At the very end each image will have 4 versions: # original # postfixed # original .webp # postfixed .webp POSTFIX="--small" DOT="." for file in ./*; do EXT="${file##*.}" # echo $EXT if [[ $EXT = "jpg" ]] || [[ $EXT = "png" ]]; then # all images will without .webp version fullname="${file##/}" # echo $fullname filename="${file%.*}" # echo $filename webp_file=$filename".webp" if test -f "$webp_file"; then echo "$(date '+%Y-%m-%d %H:%M:%S,%3N')" ____ "\ $webp_file exists" >> image_proc.log else echo "$(date '+%Y-%m-%d %H:%M:%S,%3N')" ____ "\ Converting $file to $webp_file" >> image_proc.log cwebp -q 60 $file -o $webp_file # echo "no webp" fi # do not add postfix to files that already have one existing_postfix="${filename: -7:7}" if [[ $existing_postfix != "--small" ]]; then # check if this file already has prefixed version small_candidate=$filename$POSTFIX$DOT$EXT if test -f "$small_candidate"; then # file with a postfix already exists. # And its .webp version existed or was created # at previous step echo "$(date '+%Y-%m-%d %H:%M:%S,%3N')" ____ "\ $small_candidate already exists.\ No need to add $POSTFIX to $filename" >> image_proc.log else # create new file with prefix echo "$(date '+%Y-%m-%d %H:%M:%S,%3N')" ____ \ "Copy $file to $small_candidate" >> image_proc.log cp $file $small_candidate # check if its .webp version already exists webp_small_candidate=$filename$POSTFIX$DOT"webp" if test -f "$webp_small_candidate"; then echo "$(date '+%Y-%m-%d %H:%M:%S,%3N')" ____ "\ $webp_small_candidate already exists." else echo "$(date '+%Y-%m-%d %H:%M:%S,%3N')" ____ "\ Converting $small_candidate to $webp_small_candidate" >> image_proc.log cwebp -q 60 $small_candidate -o $webp_small_candidate fi fi else # check if this file with postfix has a webp version echo "$(date '+%Y-%m-%d %H:%M:%S,%3N')" ____ "\ $filename :ignored - already has --small postfix" >> image_proc.log fi else echo $(date '+%Y-%m-%d %H:%M:%S,%3N')" ____ "\ $EXT" :ignored as it is not jpg or png" >> image_proc.log fi done #!/bin/bash # # After this script is used all .jpg and .png images that did not have # --small. versions will get one # afterwards all .jpg and .png images will get their .webp version # including those --small version that were created by this script # # At the very end each image will have 4 versions: # original # postfixed # original .webp # postfixed .webp POSTFIX="--small" DOT="." for file in ./*; do EXT="${file##*.}" # echo $EXT if [[ $EXT = "jpg" ]] || [[ $EXT = "png" ]]; then # all images will without .webp version fullname="${file##/}" # echo $fullname filename="${file%.*}" # echo $filename webp_file=$filename".webp" if test -f "$webp_file"; then echo "$(date '+%Y-%m-%d %H:%M:%S,%3N')" ____ "\ $webp_file exists" >> image_proc.log else echo "$(date '+%Y-%m-%d %H:%M:%S,%3N')" ____ "\ Converting $file to $webp_file" >> image_proc.log cwebp -q 60 $file -o $webp_file # echo "no webp" fi # do not add postfix to files that already have one existing_postfix="${filename: -7:7}" if [[ $existing_postfix != "--small" ]]; then # check if this file already has prefixed version small_candidate=$filename$POSTFIX$DOT$EXT if test -f "$small_candidate"; then # file with a postfix already exists. # And its .webp version existed or was created # at previous step echo "$(date '+%Y-%m-%d %H:%M:%S,%3N')" ____ "\ $small_candidate already exists.\ No need to add $POSTFIX to $filename" >> image_proc.log else # create new file with prefix echo "$(date '+%Y-%m-%d %H:%M:%S,%3N')" ____ \ "Copy $file to $small_candidate" >> image_proc.log cp $file $small_candidate # check if its .webp version already exists webp_small_candidate=$filename$POSTFIX$DOT"webp" if test -f "$webp_small_candidate"; then echo "$(date '+%Y-%m-%d %H:%M:%S,%3N')" ____ "\ $webp_small_candidate already exists." else echo "$(date '+%Y-%m-%d %H:%M:%S,%3N')" ____ "\ Converting $small_candidate to $webp_small_candidate" >> image_proc.log cwebp -q 60 $small_candidate -o $webp_small_candidate fi fi else # check if this file with postfix has a webp version echo "$(date '+%Y-%m-%d %H:%M:%S,%3N')" ____ "\ $filename :ignored - already has --small postfix" >> image_proc.log fi else echo $(date '+%Y-%m-%d %H:%M:%S,%3N')" ____ "\ $EXT" :ignored as it is not jpg or png" >> image_proc.log fi done

Change the extension of files

Suppose you have JPG and jpg images and you want them all to be jpg

#!/bin/bash for file in ./*; do EXT="${file##*.}" # echo $EXT if [[ $EXT = "JPG" ]]; then # all images will without .webp version fullname="${file##/}" # echo $fullname filename="${file%.*}" # echo $filename jpg_file=$filename".jpg" if test -f "$jpg_file"; then echo "$(date '+%Y-%m-%d %H:%M:%S,%3N')" ____ "\ $jpg_file exists" >> JPG_to_jpg.log else echo "$(date '+%Y-%m-%d %H:%M:%S,%3N')" ____ "\ Converting $file to $jpg_file" >> JPG_to_jpg.log mv $fullname $jpg_file # echo "no webp" fi fi done

A more readable variant: the old and the new extension are set at the start of the script.

#!/bin/bash OLD_EXT=PNG NEW_EXT=png LOG_FILE=case_change.log for file in ./*; do EXT="${file##*.}" # echo $EXT if [[ $EXT = "$OLD_EXT" ]]; then fullname="${file##/}" # echo $fullname filename="${file%.*}" # echo $filename new_file=$filename".$NEW_EXT" if test -f "$new_file"; then echo "$(date '+%Y-%m-%d %H:%M:%S,%3N')" ____ "\ $new_file exists" >> $LOG_FILE else echo "$(date '+%Y-%m-%d %H:%M:%S,%3N')" ____ "\ Converting $file to $new_file" >> $LOG_FILE mv $fullname $new_file fi fi done

Trim file names from the end

Suppose you have jpg and png images and you need to remove from their names the suffix --big

In this script we check the extension not with an OR condition but with membership in an array

#!/bin/bash EXTENSIONS=(jpg png) REMOVABLE="--big" DOT="." for file in ./*; do EXT="${file##*.}" if [[ " ${EXTENSIONS[*]} " =~ " ${EXT} " ]]; then FILENAME="${file%.*}" fivelast=${FILENAME: -5} if [[ $fivelast = $REMOVABLE ]]; then mv "$file" "${file/$REMOVABLE/}" fi fi done

Article author: Andrei Olegovich