Skip to content

Instantly share code, notes, and snippets.

@rathankalluri
Created November 4, 2016 09:39
Show Gist options
  • Save rathankalluri/bced73b0ee6c62bf5499c04548ec1cdc to your computer and use it in GitHub Desktop.
Save rathankalluri/bced73b0ee6c62bf5499c04548ec1cdc to your computer and use it in GitHub Desktop.
Creating Log File in Directory in shell
#!/bin/sh
#function for my use
makedir()
{ echo -e "Listing all Directories and Files Present Under Home \n========================================"|cat >cr_file_for_test.txt
echo -e "\nDirectories\n--------------"|cat >>cr_file_for_test.txt
find /home -type d |cat >>cr_file_for_test.txt # I need only the directories in home so 'find @ home only of type d'
echo -e "\nFiles\n--------"|cat >>cr_file_for_test.txt
find /home -type f |cat >>cr_file_for_test.txt
echo "File cr_file_for_test.txt created"
}
# A program to find the directory and create a file in it
#start
clear
echo "Enter the directory name "
read dname
#check for a directory
if [ -d $dname ]
then
#if exist
echo "Directory Found"
cd $dname
if [ -e cr_file_for_test.txt ] #if file exists I am removing it
then
rm cr_file_for_test.txt
fi
echo -n "Creating File "cr_file_for_test.txt" "
for i in 1 2 3 #for making the user feel that I am actually working !!! :P
do
sleep 1
echo -n "."
done
makedir #calling function to create file
exit 0
else
#if do not exist
echo "Directory doesn't exist.Want to create one?(y/n)"
read y
#Creating a Directory
if [ $y = 'y' ]
then
mkdir $dname
cd $dname
makedir #calling function to create file
fi
fi
exit 0
#stop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment