Check folder or subfolder exists
Write a shell script which will receive any number of filenames as
arguments. The shell script should check whether such files already
exist. If they do, then it should be reported. If these files do not exist
then check if a sub-directory called mydir exists in the current
directory. If it doesn’t exist then it should be created and in it the files
supplied as arguments should get created. If mydir already exists then
it should be reported along with the number of files that are currently
presently in mydir.
#!/bin/bash
if [ $# -lt 1 ]; then
echo "Usage: $0 file1 [file2 ...]"
exit 1
fi
for i in "$@"; do
if [ -f $i ]; then
echo "File $i already exists."
elif [ -d "mydir" ]; then
for j in `find "mydir" -type f `; do
echo "File $j exists"
done
else
mkdir "./mydir"
for i in "$@"; do
touch "./mydir/"$i
done
echo "Directory mydir created with the files with name same as arguments"
break
fi
don
For more programs in shell, visit our Shell Coder Archives
Disclaimer : All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without prior written permission of website admin, except in case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to owner, addressed “Attention: Permissions Coordinator,” to the admin @coderinme