Loop over files (Bash)

Uit De Vliegende Brigade
Versie door Jeroen Strompf (overleg | bijdragen) op 8 aug 2022 om 10:09 (→‎shopt)
(wijz) ← Oudere versie | Huidige versie (wijz) | Nieuwere versie → (wijz)
Naar navigatie springen Naar zoeken springen

This is cool: You can loop over files in a directory and do stuff with it!

Examples

Start simple

#!/bin/bash

cd ~/images-tmp
for file in *
do
   echo "File name: $file"
done	

Operate on each file

Create links to all files in this folder, and put those links in another folder:

for file in *.jpg
do

   source="${PWD}/${file}"
   link="/var/www/media.example.com/tmp2/$file"

   echo "	Path+file: $source - Destination: $link"

   ln -s "$source" "$link"

done

Complete example including shopt

I've seen the shopt statements in examples from others. I guess it stands for shell option. Turned out I really need them. And yes: This is not the most elegant or efficient script. It works.

#!/bin/bash

# Populate directory "all-product-images"
################################################################################
#
# * Use this script to populate directory "all-product-images" with links to
#   the actual images elsewhere
# * This script is repeatable: Added new images? Just add the path in the script
#   below, and run the script again
# * "ln" doesn't overwrite existing links. That makes this script repeateable
# * Maybe start with the most relevant directories?
# * Have just one directory, called "all-product-images" - Don't distinguish
#   between "with-logo", "without-logo", etc.: That makes it just much more
#   work
# * Source: https://wiki.devliegendebrigade.nl/Loop_over_files_(Bash)
#
#
# History
########################################
#
# * 2022.08.08: Created: There was a need for a directory with all images 
#   without logos
#
#
################################################################################
# create_all_links_from_directory()
################################################################################
#
create_all_links_from_directory()
{
	#
	# First loop for .jpg
	#
	for file in *.jpg
	do

		source="${PWD}/${file}"
		link="${root}all-product-images/$file"

		echo "	Source: $source 		Link: $link"

		ln -s "$source" "$link"
		
	done
	

	# Second loop for .png
	#
	for file in *.png
	do

		source="${PWD}/${file}"
		link="${root}all-product-images/$file"

		echo "	Source: $source 		Link: $link"

		ln -s "$source" "$link"
		
	done
}


################################################################################
# Loop through directories
################################################################################
#
# From most to least relevant?
########################################
#
# * It seems like nitpicking, but there actually are some cases where the
#   overall quality is better when doing this in the right order: Do 
#   "without logo" before "with logo"
#
#
# Start here
########################################
#
root="/var/www/media.example.com/"

shopt -s extglob
shopt -s nullglob

cd ${root}custom-jpg-without-logo; create_all_links_from_directory
cd ${root}custom-jpg-logo; create_all_links_from_directory
cd ${root}tools-jpg-without-logo; create_all_links_from_directory
cd ${root}tools-jpg-logo; create_all_links_from_directory
cd ${root}misc-jpg-without-logo; create_all_links_from_directory
cd ${root}misc-jpg-logo; create_all_links_from_directory
cd ${root}promotion-jpg-without-logo; create_all_links_from_directory
cd ${root}promotion-jpg-logo; create_all_links_from_directory

See also

Sources