LinuxQuestions.org
Support LQ: Use code LQCO20 and save 20% on CrossOver Office
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 03-07-2008, 12:16 AM   #1
kaujot
LQ Newbie
 
Registered: Mar 2008
Posts: 4

Rep: Reputation: 0
Traversing files in a given directory using a bash script


This seems like a question I should have no problem with, but I am. I'm trying to write a bash script that takes in one directory as an argument, traverses the files in the directory, and prints each file name that has a size of 0. However, I can't seem to get it to work properly. Here's what I've got so far:

Code:
#!/bin/bash
if [ -d "$1" ]; then
  for file in "$1"; do
    echo HELLO
  done
fi
Of course, that's not doing what I want it to do right now, but I'm just trying to get it to say HELLO three times (there are three files in my target directory). At the moment, it only echoes HELLO once.

Thoughts?
 
Old 03-07-2008, 12:28 AM   #2
jschiwal
Moderator
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,263

Rep: Reputation: 561Reputation: 561Reputation: 561Reputation: 561Reputation: 561Reputation: 561
Code:
for file in "$1"; do
The "for file in" will be repeated for every element in the "$1" argument. There is only one, the directory you inputed.

Code:
for file in "$1"/*; do
A better way of doing it would be to use the find command.
Code:
if [ -d "$1 ]; then
   find "$1" -type f -empty
If you don't want to recurse subdirectories, add the option "-maxdepth 1". Using: for file in "$1"/*; do will return all filenames, so you would still need to test that it is a regular file and that it is a zero length file.

Last edited by jschiwal; 03-07-2008 at 12:30 AM.
 
Old 03-07-2008, 12:32 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: back to Arch
Posts: 16,641

Rep: Reputation: 423Reputation: 423Reputation: 423Reputation: 423Reputation: 423
When you say "for file in ABC", you want "ABC" to be something that appears as a list. You are just giving the statement one directory name.

Try:
for file in `ls $1`

(those are backtics, not single quotes)

I suppose you could also do this:

cd $1
for file in *
....etc.
 
Old 03-07-2008, 12:39 AM   #4
kaujot
LQ Newbie
 
Registered: Mar 2008
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by pixellany View Post
When you say "for file in ABC", you want "ABC" to be something that appears as a list. You are just giving the statement one directory name.

Try:
for file in `ls $1`

(those are backtics, not single quotes)

I suppose you could also do this:

cd $1
for file in *
....etc.
Thanks. That worked perfectly.
 
Old 03-07-2008, 12:40 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 237Reputation: 237Reputation: 237
Quote:
Originally Posted by pixellany View Post
When you say "for file in ABC", you want "ABC" to be something that appears as a list. You are just giving the statement one directory name.

Try:
for file in `ls $1`

(those are backtics, not single quotes)

I suppose you could also do this:

cd $1
for file in *
....etc.
the second method is more preferred way.
 
Old 03-07-2008, 06:48 AM   #6
doc.nice
Member
 
Registered: Oct 2004
Location: Germany
Distribution: Debian
Posts: 274

Rep: Reputation: 34
but beware if you have filenames with a blank in their name, then these methods will fail.

you had to use

for file in `ls -Q $1`

(-Q places quotes around each individual result)
 
Old 03-07-2008, 07:14 AM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: Slackware64 13.37, Kubuntu 10.04
Posts: 2,941

Rep: Reputation: Disabled
You also need to use the -d option so that directory names are listed instead of their contents.
ta0kira
 
Old 03-07-2008, 12:39 PM   #8
baikonur
Member
 
Registered: Oct 2005
Location: germany
Distribution: debian
Posts: 255
Blog Entries: 5

Rep: Reputation: 30
Quote:
Originally Posted by doc.nice View Post
but beware if you have filenames with a blank in their name, then these methods will fail.

you had to use

for file in `ls -Q $1`

(-Q places quotes around each individual result)
nice to know!

baik
 
  


Reply

Tags
bash, directory, file, traversal


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script to access all files in a directory shinni Programming 5 04-24-2009 03:46 PM
Using Bash, Find script files in a directory or subdirectories within... ray5_83 Programming 4 10-10-2008 07:42 PM
To rename files in a directory should I use Bash script or a Perl Script ? jamtech Programming 7 01-22-2008 11:25 PM
Bash script: symbolic linking all files with same extension in a directory Katachi Programming 1 12-25-2006 09:42 AM
Bash script to traverse directory tree and rename files intramaweb Programming 3 10-08-2006 12:51 PM


All times are GMT -5. The time now is 07:42 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration