Loop through files bash help

Asnyder
Contributor III

I'm trying to write a bash script that basically stores a file list into an array, loops through the array and then appends something to the files. I.e. loop through a directory and add a . to the beginning to make it hidden.
I know this is fairly simple but I've never really worked with arrays or loops in bash at all.
1. Insert file list into array
2. Loop through array
3. cat something to the file name (would I have to use rn? or is there a way to add something to the filename?)

#!/bin/bash
#get files on desktop and store into array
cd /Users/$3/Desktop/
files=($(ls)) 

#loop through array and cat something to beginning
for file in files; do

    done
1 ACCEPTED SOLUTION

thoule
Valued Contributor II

https://www.cyberciti.biz/faq/bash-loop-over-file/

Sample Shell Script To Loop Through All Files

#!/bin/bash
FILES=/path/to/*
for f in $FILES
do
  echo "Processing $f file..."
  # take action on each file. $f store current file name
done

View solution in original post

1 REPLY 1

thoule
Valued Contributor II

https://www.cyberciti.biz/faq/bash-loop-over-file/

Sample Shell Script To Loop Through All Files

#!/bin/bash
FILES=/path/to/*
for f in $FILES
do
  echo "Processing $f file..."
  # take action on each file. $f store current file name
done