Show all file names in a directory, 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
#!/bin/bash
for file in ./**; do
echo "${file##/}"
done
Show files with a specific 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 the .webp format
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 libwep-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 to do this with
all files of this type in the directory
For example, the directory contains 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 so 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
Article author: Andrei Olegovich