Justin Leal
Justin Leal

Reputation: 1

Displaying file content with bash scripting

I am trying to write a bash script (display) that will allow me to access a directory, list the files, and then display the content of all of the files. So far I am able to access the directory and list the files.

#!/bin/bash

#Check for folder name
if [ "$#" -ne 1 ]; then
  echo " Usage: count [folder name]"
  exit 1
fi

#Check if it is a directory
if [ ! -d "$1" ]; then 
  echo "Not a valid directory"
  exit 2
fi

#Look at the directory 
target=$1
echo "In Folder: $target"  
for entry in `ls $target`; do
  echo $entry 
done

So if I use the command ./display [directory] it will list the files. I want to display the contents of all of the files as well but I am stuck. Any help would be appreciated thanks!

Upvotes: 0

Views: 948

Answers (2)

ceving
ceving

Reputation: 23876

Use find to find files. Use less to display files interactively or cat otherwise.

find "$target" -type f -exec less {} \;

Upvotes: 1

samsonjm
samsonjm

Reputation: 270

I thin a loop similar to your "look at the directory" loop would suffice, but using the cat command instead of ls

Upvotes: 0

Related Questions