csh script for listing files and directories
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Shell > csh script for listing files and directories




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

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


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-22-06 06: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)






[ Post a follow-up to this message ]



    Re: csh script for listing files and directories  
Dattu


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-22-06 06: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






[ Post a follow-up to this message ]



    Re: csh script for listing files and directories  
Dattu


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-22-06 06: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






[ Post a follow-up to this message ]



    Re: csh script for listing files and directories  
Chris F.A. Johnson


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-22-06 12:27 PM

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





[ Post a follow-up to this message ]



    Re: csh script for listing files and directories  
Michael Tosch


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-22-06 06: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





[ Post a follow-up to this message ]



    Re: csh script for listing files and directories  
Stachu 'Dozzie' K.


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-23-06 12:33 PM

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





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 01:56 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register