hai dani,

i have a problem with listing the contents of file in another directory.

my default path is in /home/pallu
where my file called DirProg script exists , now i want to find any file which starts with a or A in a dir and store the content in another file called output.dat.

im not able to dispaly file in the directory threw script.

my code is below:----


echo "Enter a directory name"
read dname

if test -d $dname
then

if grep -c ^A dirlist1
then
fname=`grep ^A dirlist1`
cat $fname|cat > output.dat
echo "Content of file is written in a file called output.dat"
fi

else
echo "The entered name is not a directory"
fi

thanks in advance.

Recommended Answers

All 2 Replies

You should probably use find :

#!/bin/bash
echo -n "Enter a directory: "
read dir

if ! test -d ${dir}; then
  echo "Directory does not exist"
  exit
fi

# To search child directories
rm -f output_withchildren.dat
find ${dir} -type f -iname a\* >> output_withchildren.dat

# To not search child directories
rm -f output_nochildren.dat
find ${dir} -maxdepth 1 -type f -iname a\* >> output_nochildren.dat

hai dani,

i have a problem with listing the contents of file in another directory.

my default path is in /home/pallu
where my file called DirProg script exists , now i want to find any file which starts with a or A in a dir and store the content in another file called output.dat.

im not able to dispaly file in the directory threw script.

my code is below:----


Please use CODE tags.

echo "Enter a directory name"
read dname

if test -d $dname
then

	if grep -c ^A dirlist1
	then
	       fname=`grep ^A dirlist1`
		cat $fname|cat > output.dat


Why two cats?

cat $fname > output.dat
echo "Content of file is written in a file called output.dat" 
	fi

	

else
	echo "The entered name is not a directory"
fi
read dname
if [ -d "$dname" ]
then
    printf "%s\n" "$dname"/A* > output.dat
else
    echo "The entered name is not a directory"
fi
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.