Displaying file content with bash scripting - linux

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

Related

Check if there's some file on a directory regardless of another directory inside - linux ksh

I'm trying to check if there's any file on a directory but i need it to be independent of the fact that can or cannot be another directory inside.
I'm using:
if [ -n "$(ls -A /unload/ebia 2>/dev/null)" ]
then
Exists="Yes"
else
Exists="No"
fi
echo "is any file inside $PATH ? $Exists."
If there's nothing on $PATH, it say no, and if there's any file inside, it say yes (it's correct), but if i create a directory inside $PATH, it keeps answering yes instead there's no file. How to avoid this?
use find:
if [ -n "$(find /unload/ebia/ -type f)" ]
then
Exists="Yes"
else
Exists="No"
fi
finds -type f switch only searches for files and skips directorys

Linux mkdir, find and mv

So guys, I have to write this script in Linux. I have to create a directory and see if it already exists or not and then I have to find all the file which end in ".c" and move them to the directory I created.
This is what I have so far:
#!/bin/bash
while
echo "name of the directory you want to create "
read -p "$name"; do
if [ ! -d "$name" ]; then
{
echo "Directory doesn't exist. Create: "
read -p "$name"
mkdir -p Scripts/"$name"
}
else
echo "Directory exists"
fi
find ./ -name '*.c' | xargs mv -t "$name"
done
When I try to execute it, it doesn't work. It doesn't create a new directory and also it says:
mv: failed to access '': No such file or directory.
Can you please help me find a solution to this?
I'm not exactly sure what you're trying to achieve. But there are a few things in your script that don't make sense.
What do you need a while loop for? Once you've made the folder and moved all scripts, there's no sense in running it again.
Why do you read the name of the directory twice? You can just read it once, store it in $name and use it until the end of the script.
You don't need find *.c will select all files ending with .c in the current directory.
With all that said, here's the script that does what you requested if I understood correctly.
#! /bin/bash
echo -n "Enter the name of the directory you want to create: "
read name
if [ ! -d Scripts/"$name" ]; then
echo "Directory doesn't exist. Creating it."
mkdir -p Scripts/"$name"
else
echo "Directory exists"
fi
echo "Moving files"
mv *.c Scripts/"$name"

Command script recognizes files as directories

The following code should count the number of elements that a directory contains, but as well as it does it correctly, it also recognizes every element inside the current directory as a directory .
I don't know how not to show the elements that are not directories. How could I do it?
Code is here: http://pastebin.com/9R4eB4Xn
termlog.txt:
https://justpaste.it/tgsl
As you may see, some files like .jpg or .zip are recognized as directories.
Your echo "Element is a directory" is between the if and the then. Move it after then :
for i in *
do
if [ ! -f "$i" ] && [ -d "$i" ]
then
echo "Element is a directory"
FILES=`ls -l "$i" | wc -l` # List the content of "$i" directory
# and count the number of lines
FILES2=`expr $FILES - 1` # Substract one because one line is
# occupied with the number of blocks
echo "$i: $FILES2" # Shows the name of the directory and
# the number of inputs that it has
fi
done
for i in `find DIRECTORY -maxdepth 2 -type d`; do echo "$i: `ls -1 $i | wc -l`"; done
If only interested in current directory, replace DIRECTORY with .

How to determine if a linux symbolic link points to a directory or a file

I have a directory that has symbolic links - some of them point to files and some of them to directories - how do I identify the ones poiting to directory in a shell script ( without any prejudice to names offcourse)
use ls -L option to follow symlinks
This is the script that I used to differentiate between directories with contents from files/empty directories
( this will work only if directory has some contents -- in my case I am anyway interested in those directories that have some content so I am happy - but do suggest better options if any
cd dir
for i in `ls `
do
if [ 1 -lt `ls -l -L $i | wc -l` ]
then
echo "$i is a non empty directory"
else
echo "$i is either an empty directory or a file"
fi
done

listing files from directory by passing command line arguments

I am trying to list files from any number of directories by using command line arguments.
I am trying to pass the directory paths through the command line and display files that are in searched directories. Something like this.
"Directory: PATH1"
files
files
files
"Directory: PATH2"
files
files
etc.
So I am using $* to pass all of the command line arguments but it only displays files from the first listed directory.
#!/bin/bash
cd $*
for filename in *
do
echo "Directory: $*"
echo $filename
done
Just in case you have a space in a directory name you might want to use "$#", and it doesn't hurt to check that a directory exists:
for dir in "$#" ; do
if [ -d "$dir" ] ; then
echo "Directory: $dir"
ls "$dir" # If all you want is to show the contents, this should do
else
echo "Not a directory: $dir"
fi
done
Something similar to this should work:
dirs=$*
for dir in $dirs
do
echo "Directory: $dir"
for filename in $(ls $dir)
do
echo $filename
done
done

Resources