Detect and print file or directory coderinme

Detect and print file or directory

Write a shell script which will receive any number of filenames as arguments. The shell script should check whether every argument supplied is a file or a directory. If it is a directory it should be approximately reported.If it is a filename then the name of the file, as well as the number of lines present in it, should be reported.


if [ $# -lt 1 ]; then
echo "Usage: $0 dir[/file ...]"
exit 1
for i in `ls $1`; do
if [ -d $i ]; then
echo $i" is a directory"
line=`wc -l $i | cut -d " " -f1`
echo "Name of the file: "$i
echo "Lines in the file: "$line

For more programs in shell, visit our Shell Coder Archives
Detect and print file or directory

Now what we know about file

According to tldp linux

“On a UNIX system, everything is a file; if something is not a file, it is a process.”

It is reasonably safe to suppose that everything you encounter on a Linux system is a file, there are some exceptions.

Directories: files that are lists of other files.

Special files: the mechanism used for input and output. Most special files are in /dev, we will discuss them later.

Links: a system to make a file or directory visible in multiple parts of the system’s file tree. We will talk about links in detail.

(Domain) sockets: a special file type, similar to TCP/IP sockets, providing inter-process networking protected by the file system’s access control.

Named pipes: act more or less like sockets and form a way for processes to communicate with each other, without using network socket semantics.

The -l option to ls displays the file type, using the first character of each input line:

command ls -l

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

A web developer(Front end and Back end), and DBA at Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me

Leave a reply:

Your email address will not be published.