Going through files in a directory, bash scripting
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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?
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)
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.
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?
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?
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???
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.