Find the answer to your Linux question:
Results 1 to 6 of 6
Hi all, I want to write a shell script which reads the directory and its sub directories and find whether .c files are there if there i have get the ...
  1. #1
    Just Joined!
    Join Date
    Jun 2007
    Posts
    14

    How to read directory in shell script

    Hi all,
    I want to write a shell script which reads the directory and its sub directories and find whether .c files are there if there i have get the name of the files individually. Can any one help them.How to use awk in the while loop too.

    Regards,
    Yugandhar.

  2. #2
    Linux Guru sdousley's Avatar
    Join Date
    Feb 2004
    Posts
    1,789
    A good start is to use find. If you want to do something to each of the files, then something like:

    Code:
    #!/bin/bash
    for file in $(find $1 -type f -iname '*.c'); do
      (What to do with the files, replacing filename with $file)
    done
    "I am not an alcoholic, alcoholics go to meetings"
    Registered Linux user = #372327

  3. #3
    tpl
    tpl is offline
    Linux User
    Join Date
    Jan 2007
    Location
    cleveland
    Posts
    452
    or try grep: "ls -R |grep *.c"
    the sun is new every day (heraclitus)

  4. #4
    Linux Guru sdousley's Avatar
    Join Date
    Feb 2004
    Posts
    1,789
    Quote Originally Posted by tpl View Post
    or try grep: "ls -R |grep *.c"
    This would work if it was JUST the filenames that you wanted, but if you also wanted their location, then the find method would be preferable since it includes that information. iirc, ls -R simply shows each directory's list under the folder name eg:

    folder:

    file1
    file2
    file3

    folder2:

    file4
    file5
    file6
    "I am not an alcoholic, alcoholics go to meetings"
    Registered Linux user = #372327

  5. #5
    Just Joined!
    Join Date
    Jun 2007
    Posts
    14

    How to read a directory using shell script

    Hi ,
    After getting all the .c files i will store it in a file and i have to read the file and get the names of the .c files one by one up to end of the file. How can i read one line at a time from a file.

  6. #6
    Linux Guru sdousley's Avatar
    Join Date
    Feb 2004
    Posts
    1,789
    If you use the find method i posted above, you dont need to post the file names out to a file, just include what you want done to the file in the loop.
    "I am not an alcoholic, alcoholics go to meetings"
    Registered Linux user = #372327

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...