Unix Shell - csh script for listing files and directories

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > November 2006 > csh script for listing files and directories





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author csh script for listing files and directories
matt.sti1@gmail.com

2006-11-22, 1:39 am

I am trying to create a script in UNIX that will list the files in the
current directory. Also, if there is a another directory within the
current directory it will list its contents. I am fairly new to csh. I
was able to get the first part where the contents of the current
directory is printed. I am having trouble with how to go to the next
directory and print its contents.

Here is a sample of what I have so far:

foreach file (*)
if (-d $file) then
echo $file "(directory)"
else if (-e $file) then
echo $file "(file)"
end


The output would look something like this:

a(file)
b(directory)
c(file)

Dattu

2006-11-22, 1:39 am

Hi Matt,
I hope the below script will do for u.

#!/usr/bin/csh
foreach abc (`find . -name "*"`)
set ab=`echo $abc | wc -c`
if ( $ab > 2 ) then
if ( -d $abc ) then
set File=`echo $abc | cut -c3-$ab`
echo " Directory :: $File"
else if ( -e $abc ) then
set File=`echo $abc | cut -c3-$ab`
echo " File :: $File"
endif
endif
end

Thanks,
dattu

Dattu

2006-11-22, 1:39 am

Modified one ...

#!/usr/bin/csh
foreach abc (`find . -name "*" -exec basename {} \;`)
if ( -d $abc ) then
echo " Directory :: $abc"
else if ( -e $abc ) then
echo " File :: $abc"
endif
end

Thanks,
Dattu

Chris F.A. Johnson

2006-11-22, 7:27 am

On 2006-11-22, matt.sti1@gmail.com wrote:
> I am trying to create a script in UNIX that will list the files in the
> current directory. Also, if there is a another directory within the
> current directory it will list its contents. I am fairly new to csh. I
> was able to get the first part where the contents of the current
> directory is printed. I am having trouble with how to go to the next
> directory and print its contents.
>
> Here is a sample of what I have so far:
>
> foreach file (*)
> if (-d $file) then
> echo $file "(directory)"
> else if (-e $file) then
> echo $file "(file)"
> end


You have been given a couple of answers, but before you spend too
much time learning csh, you should read these:

<http://www.grymoire.com/Unix/CshTop10.txt>
<http://www.grymoire.com/Unix/Csh.html#uh-0>
<http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/>


> The output would look something like this:
>
> a(file)
> b(directory)
> c(file)


--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Michael Tosch

2006-11-22, 1:17 pm

Chris F.A. Johnson wrote:
> On 2006-11-22, matt.sti1@gmail.com wrote:
>
> You have been given a couple of answers, but before you spend too
> much time learning csh, you should read these:
>
> <http://www.grymoire.com/Unix/CshTop10.txt>
> <http://www.grymoire.com/Unix/Csh.html#uh-0>
> <http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/>
>
>
>


In the Bourne shell (/bin/sh),
you can even have a recursive routine,
that will find/list the entire directory tree:

#!/bin/sh
readdir(){
for file in "$1"/*
do
if [ -f "$file" ]; then
echo "$file (file)"
elif [ -d "$file" ]; then
echo "$file (directory)"
readdir "$file"
fi
done
}
readdir "."

But in C-Shell you cannot do such things.

--
Michael Tosch @ hp : com
Stachu 'Dozzie' K.

2006-11-23, 7:33 am

On 22.11.2006, Michael Tosch <eedmit@NO.eed.SPAM.ericsson.PLS.se> wrote:
> Chris F.A. Johnson wrote:
[vbcol=seagreen]
>
> In the Bourne shell (/bin/sh),
> you can even have a recursive routine,
> that will find/list the entire directory tree:
>
> #!/bin/sh
> readdir(){
> for file in "$1"/*
> do
> if [ -f "$file" ]; then
> echo "$file (file)"
> elif [ -d "$file" ]; then
> echo "$file (directory)"
> readdir "$file"
> fi
> done
> }
> readdir "."


I would use find (GNU find, actually):
find . -type f -printf '%p (file)\n' -o -type d -printf '%p (directory)\n'

--
<Kosma> Niektórzy lubi± dozziego...
<Kosma> Oczywi¶cie szanujemy ich.
Stanislaw Klekot
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com