LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-11-2010, 06:41 PM   #1
Pewgs
LQ Newbie
 
Registered: Apr 2010
Posts: 4

Rep: Reputation: 0
Going through files in a directory, bash scripting


Hey guys I am brand new to linux and the class I'm taking for it has more of a use the resources online and man pages method, meaning not much teaching has been going on.

Anyway the project I'm working on involves comparing files in a given directory, using bash scripting methods. So my program is given an argument for the directory you want to search and then carry out the instructions. So I know how to pass the argument using $1, but given the directory I'm not quite sure how to loop through all the files.

I'm not new to programming so I things such as if statements and loops buts it's been the linux specific commands that have given me trouble and I couldn't find anything in the man pages.

Sorry for being vague about the project, I want to be able to figure it out on my own but I can't really test anything until I figure this out, if you need more information about what I'm asking feel free to ask.

P.S. Hi Linux community
 
Old 04-11-2010, 06:55 PM   #2
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
The shell will expand the * argument so effectively you have to enter a loop for each x in * and in the loop do all your commands and then finally when done, exit.

All the best.

End
 
Old 04-11-2010, 07:00 PM   #3
fbsduser
Member
 
Registered: Oct 2009
Distribution: Hackintosh, SlackWare
Posts: 267

Rep: Reputation: 30
Code:
#!/bin/bash
        for i in $( ls ); do
            echo item: $i
        done
This will cycle through your files/directories and show them. It's a small start for you to work from.
 
Old 04-11-2010, 07:02 PM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Hi,

You can look here for bash scripting guides:
http://tldp.org/guides.html
 
Old 04-11-2010, 07:09 PM   #5
Pewgs
LQ Newbie
 
Registered: Apr 2010
Posts: 4

Original Poster
Rep: Reputation: 0
I'm sorry I don't quite understand, when you say * do you mean the variable stored as the directory? And by x do you mean each file in stored in the directory?

So for my example if this was my code:
#!/bin/bash

DIRECTORY=$1
for x in DIRECTORY
do
#comparing lines here
done

Like I said I'm completely new to linux and bash scripting.

EDIT: Nevermind fbsduser's post was very helpful

DOUBLE EDIT:
Ok but I'm at a new problem so because I need to compare two files in created a for loop in another for loop:

#!/bin/bash

DIRECTORY=$1
for i in $( ls $DIRECTORY )
do
for x in $( ls $DIRECTORY )
do
COMPARE=cmp i x
echo $COMPARE
done
done

from my understanding cmp should have an exit value but Im getting this error whenever it runs that line:
./remdup: line 8: i: command not found
I have a feeling that my line COMPARE=cmp i x is not valid. Does anyone know how I would express this?

Last edited by Pewgs; 04-11-2010 at 07:31 PM.
 
Old 04-11-2010, 07:41 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
The error is correct:

Quote:
./remdup: line 8: i: command not found
And you are correct about the offending line:

Quote:
COMPARE=cmp i x
If you look at your line carefully you have said the following:

1. COMPARE=cmp -> assign the value of cmp to COMPARE
2. i x -> execute the i command using x as a parameter

Check out : http://tldp.org/LDP/abs/html/command...#COMMANDSUBREF

Make sure you read through the examples, also look further down the page at $() as the alternative to `` (these are called back ticks which
are located under the ~ (tilde) key)
 
1 members found this post helpful.
Old 04-11-2010, 09:20 PM   #7
Pewgs
LQ Newbie
 
Registered: Apr 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Ok so this result I think is currently working:

COMPARE=`cmp $i $x`

In my directory that I'm testing I have two files I'm using for testing called "a" and "one". So when I run the program I'm getting this as a result

cmp: a: No such file or directory
cmp: a: No such file or directory
cmp: One: No such file or directory
cmp: One: No such file or directory

which through debugging I have determined is from ths line
COMPARE=`cmp $i $x`. Also when I echo $COMPARE it's spitting out a blank line which means that nothing is being assigned to it. Any thoughts? It's obvious that a and One are in the directory because the program recognizes to files name a and One, so I'm not sure why I'm getting these messages... Thanks guys you have been a real help so far.
 
Old 04-11-2010, 09:40 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
Ok, so as this is a learning process the best thing to do is run the command at the command line and confirm the output
is as expected. So step by step would be:

1. perform "cmp a One" (without the quotes) on the command line and check that it does compare.
2. once the above has finished, issue the following: echo $? (this will show you what the return value would have been)
3. if all the above as expected, instead of using for loops, try passing 'a' and 'One' as arguments to the script instead of directory
so code to mimic command line above is: (this goes in the current script with the rest remarked out)
Code:
cmp $1 $2
echo $?
4. from there you should then be able to piece your program back together.

Let me know if you get stuck or if the above is confusing?
 
Old 04-11-2010, 10:02 PM   #9
Pewgs
LQ Newbie
 
Registered: Apr 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
Ok, so as this is a learning process the best thing to do is run the command at the command line and confirm the output
is as expected. So step by step would be:

1. perform "cmp a One" (without the quotes) on the command line and check that it does compare.
2. once the above has finished, issue the following: echo $? (this will show you what the return value would have been)
3. if all the above as expected, instead of using for loops, try passing 'a' and 'One' as arguments to the script instead of directory
so code to mimic command line above is: (this goes in the current script with the rest remarked out)
Code:
cmp $1 $2
echo $?
4. from there you should then be able to piece your program back together.

Let me know if you get stuck or if the above is confusing?
All of this makes sense and when I try those I am getting desirable results, but I still don't understand why I'm getting the no such file error. I was thinking maybe its because I'm in a different directory but then how would the program know the names of the files?
 
Old 04-11-2010, 10:18 PM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
You need to look carefully at your code and compare to how you would do it on the command line,
ie from the command line, what would you write to compare (cmp) two files in another directory???

Then look at how your code is written.
 
Old 04-11-2010, 11:34 PM   #11
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Hi,

you should avoid using 'ls' in a script. This is problematic with filenames which contain spaces. Consider the following:
Code:
echo Test > 'filename with space'
Now run the following script:
Code:
for f in $( ls )
do
 echo $f
done
The output is:
Code:
filename
with
space
This is how you should normally script it
Code:
for f in "$PATH_TO_DIRECTORY"/*
do
 echo "$f"
done
This way you won't encounter the problem you are facing right now. However, you should try to understand why you get the 'no such file ...' error. grail already gave you the decisive hint.
 
  


Reply


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash scripting -- parsing a directory path int80 Programming 14 05-27-2017 11:33 PM
Bash scripting with directory checking if exists netpumber Programming 6 12-01-2009 07:35 PM
Bash Scripting - Configuration files piercey Programming 8 10-17-2008 10:41 AM
Bash scripting and trying to determine whether a directory exists? obelxi Programming 9 04-18-2005 11:22 PM
bash scripting - editing files brian0918 Programming 1 06-30-2003 06:16 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:09 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration