简体   繁体   中英

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!

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM