Reading from Directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading from Directory
# 1  
Old 04-23-2010
Reading from Directory

I am trying to make a simple script where you type in a directory, and the script tells you how many directories are within the directory, how many files, and some other general file information. My problem is setting up a for loop to read the info about the files inside the specified directory. For example:

I am the directory: ~/temp
test is in the directory: ~/temp/test

I type:

file.sh test

Code:
file=$1  

for file in $file [ ls '*' ]
do

if [ -d $file ]
then
  dirCount=`expr $dirCount + 1`
fi

done

This is my current code, but it does not work. It keeps telling me the information about the directory that I am in, rather than the 'test' directory. Does anyone know why this is happening/how to fix it?

Any help is greatly appreciated.

---------- Post updated at 05:27 PM ---------- Previous update was at 04:48 PM ----------

I have also tried

for file in$(find $1 -iname '*'); do

and

for file in `ls *`

All seem to do the same thing
# 2  
Old 04-23-2010
All of those constructs are going to break down when you have a large number of files since there's a limit to the number of arguments you can cram into a for loop. Also, `ls *` is the same thing as just * anyway. See useless use of backticks and useless use of ls *. Besides, * won't find anything deeper than the immediate directory.

A better way is to read files one by one from a file or stream. Here we save to a temporary file:
Code:
TMP=`mktemp`
find "$1" > "$TMP"

DIRCOUNT=0
FILECOUNT=0
while read FILENAME
do
        if [ -f "$FILENAME" ]
        then
                FILECOUNT=$((FILECOUNT+1))
        elif [ -d "$FILENAME" ]
        then
                DIRCOUNT=$((DIRCOUNT+1))
        fi
done < "$TMP"

rm -f "$TMP"

echo "Directories: ${DIRCOUNT}"
echo "Files: ${FILECOUNT}"

# 3  
Old 04-24-2010
Code:
#/bin/ksh or bash or ...
cd "$1"
d=0
f=0
# no need to use ls, you can use command line "filename generation", it
# works in every line, test example echo *
for file in *
do
        [ -d "$file" ] && ((d+=1)) && continue
        [ -f "$file" ] && ((f+=1)) && continue
        # more rules ...
done
echo "dir :$d"
echo "file:$f"

In this example you don't need cd, in this case for line will be
Code:
for file in "$1"/*

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

' for reading (No such file or directory)

1.1 Solaris 10 8/07 s10s_u4wos_12b SPARC 1.2 Patch: 127714-03 Obsoletes: Requires: 120011-14 Incompatibles: Packages: SUNWsshcu, SUNWsshdu, SUNWsshu Patch: 128253-01 Obsoletes: Requires: Incompatibles: Packages: SUNWsshcu Patch: 126630-01 Obsoletes: Requires: Incompatibles: Packages: SUNWtcsh 1.3... (3 Replies)
Discussion started by: alvinoo
3 Replies

2. Shell Programming and Scripting

Reading contents of files from a directory

I have a directory with the files, 1st: I want to Diplay each filename, underline it 2nd: Display the contents under the filename and 3rd: Redirect the above content to other file 4th: Remove the files from the directory #!/bin/ksh for i in $( cat $a/b/c.txt ) do echo "... (1 Reply)
Discussion started by: Aditya_001
1 Replies

3. Shell Programming and Scripting

Reading in all files from parent directory (GAWK)

Hi all, I'm very, very new to scripting (let alone SHELL) and was wondering if anyone could help me out as I seem to be in a spot of bother. I collect data (.dat files) which are automatically seperated into several sub directories, so the file paths I'm reading in at the moment would be... (11 Replies)
Discussion started by: gd9629
11 Replies

4. Shell Programming and Scripting

reading files from a directory.

Can some body help me to code this? go to a specific directory.(/home/abcd/test) file1.txt, file2.txt, ... .. filen.txt read the files in side the folder 'test' and print the content of each file into other folder in the same directory lets say(testresult) with the same file name... (4 Replies)
Discussion started by: rocking77
4 Replies

5. UNIX for Dummies Questions & Answers

Reading files in script from another directory

Hi I'm trying to call my files from different directories in my script. Can you please help me. Here is my script: #!/bin/bash #---------------------------------------------------------------------------------------------------------------------- #This script allows the user... (1 Reply)
Discussion started by: ladyAnne
1 Replies

6. AIX

reading files from a directory.

Hi all, I have a shell script where it processes a set of files from a particular directory (shared location among 4 servers). i.e. under this directory /shared/work/ I have a set of files that needs to be processed. Since the number of files are alot, I have this script to be run from 4... (2 Replies)
Discussion started by: haroon_a
2 Replies

7. UNIX for Dummies Questions & Answers

Reading filename from directory

I am using the following code to read filename from the directory: for i in ` ls $inputDir | grep $partialName*.csv` do echo $i done But the echo is giving me the following: ls | grep cm_ctx*.csv instead of the full filename "cm_ctx_2009_07_15_17_18.csv" Any ideas anyone? I... (2 Replies)
Discussion started by: khanvader
2 Replies

8. Shell Programming and Scripting

perl problem: reading from directory

hi friends. i want to make a perl script to read /tmp directory and make a list of all users which are using /tmp directory, how much space they are using and permission of all files which are using by all usres. i have tried some option but at last i am hopeless. plz plz help me. ... (1 Reply)
Discussion started by: praneshmishra08
1 Replies

9. Shell Programming and Scripting

Reading files in directory

Hi Everyone , have a nice day i need a help on this thing algo is something like in certain path like /root/user1 i have many files , i need a code which could open every file one by one and then each file has contents like this <moid>CcnCounters=CAPv3-Received-Total-Requests, Source =... (3 Replies)
Discussion started by: Dastard
3 Replies

10. UNIX for Dummies Questions & Answers

reading directory for most recent file?

Dear All, I'm trying to write a script that searches thru a directory looking for a most recent file and then scp that file. I have the scp working, but I don't know how to browse the directory and select the most recent file. The file name includes a date & time stamp (e.g.... (3 Replies)
Discussion started by: duncan_glover
3 Replies
Login or Register to Ask a Question