0

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.

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by cfajohnson
0

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

Edited by sknake: n/a

0

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
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.