Skip to main content
Solved

Loop through files bash help

  • September 28, 2017
  • 1 reply
  • 0 views

Forum|alt.badge.img+10
  • Valued Contributor
  • 224 replies

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

Best answer by thoule

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 original
Did this topic help you find an answer to your question?

1 reply

Forum|alt.badge.img+15
  • Contributor
  • 589 replies
  • Answer
  • September 28, 2017

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

Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings