LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 10-08-2015, 11:28 AM   #1
Vishnu Vijayan
LQ Newbie
 
Registered: Oct 2015
Posts: 2

Rep: Reputation: Disabled
Question bash script that printout the full paths of all the files under the directory


Write a bash script that printout the full paths of all the files under the directory of interest and its sub-directories that match certain pattern

- Take two inputs from the user: one is the full path to the directory and the other is the pattern of interest

For example,
>> myscript.sh /home/student/master_project/test_directory comp
Your program "myscript.sh" will need to look into "/home/student/master_project/test_directory" and print out all the files that has "comp" in it.

The challenge is you do not know how many levels of subdirectories are there, so you need to use suitable loop control to cover all sub-directories.
 
Old 10-08-2015, 11:32 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
what have you tried so far and where are you stuck.
did your attempt print out an error message. please provide your source code so someone would be able to help correct your mistake.
 
1 members found this post helpful.
Old 10-08-2015, 11:37 AM   #3
Vishnu Vijayan
LQ Newbie
 
Registered: Oct 2015
Posts: 2

Original Poster
Rep: Reputation: Disabled
Its for my sister.

Quote:
Originally Posted by schneidz View Post
what have you tried so far and where are you stuck.
did your attempt print out an error message. please provide your source code so someone would be able to help correct your mistake.
Its for my sister. She is not well. She wants to submit it as an assignment. I dont know Linux. So i thought someone may help me here.
 
Old 10-08-2015, 11:41 AM   #4
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Hello and welcome!

Please take a moment to read through the LQ rules:

Quote:
Do not post homework assignments verbatim. We're happy to assist if you have specific questions or have hit a stumbling point, however. Let us know what you've already tried and what references you have used (including class notes, books, and searches) and we'll do our best to help. Keep in mind that your instructor might also be an LQ member.
Best regards,
HMW
 
Old 10-08-2015, 11:45 AM   #5
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by Vishnu Vijayan View Post
Its for my sister. She is not well.
If your sister is so unwell that she cannot use a computer on her own, I fail to see how she could possibly manage a class in GNU/Linux.

My condolences. I really hope she feels better soon. When she does, she is more than welcome to post her efforts here so that I and others will be able to help her out if needed.

Best regards,
HMW

Last edited by HMW; 10-08-2015 at 11:58 AM.
 
2 members found this post helpful.
Old 10-08-2015, 01:20 PM   #6
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,635

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by Vishnu Vijayan View Post
Its for my sister. She is not well. She wants to submit it as an assignment. I dont know Linux. So i thought someone may help me here.
Sorry, but this makes very little sense.

If your sister is so very ill that she cannot even type, why is she in school and/or expected to turn in assignments? If she just has a minor illness, then she is PERFECTLY able to post a question herself, or at the very least, give you what she has done so far, towards 'her' assignment. It seems much more likely that this is YOUR homework, and you're playing a sympathy card, trying to get someone to hand you a script.
 
Old 10-09-2015, 01:58 AM   #7
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Well... anyway. When your sister gets better, tell her that this:
Quote:
Originally Posted by Vishnu Vijayan View Post
The challenge is you do not know how many levels of subdirectories are there, so you need to use suitable loop control to cover all sub-directories.
Is utter nonsense.

In directory tmpdir I have a couple of files and a directory. The files look like this:
Code:
tmpdir$ cat afile.txt foo 
zumzumzum...
foo bar comp
The directory (bar) contains a file (sometext.txt) with this content:
Code:
tmpdir$ cat bar/sometext.txt 
comp
ladadi ladada
As you can see, I have the keyword "comp" in some of the files and directories in my tmpdir directory.

So, let us try a script as specified in your first post:
Code:
./myscript.sh /home/HMW/LQ/tmpdir/ comp
/home/HMW/LQ/tmpdir/foo
/home/HMW/LQ/tmpdir/bar/sometext.txt
Good. The script found all the files containing the keyword "comp" in tmpdir and its sub-directories. For this, I used not a single loop, and the script itself is three (3) lines. If you skip the
Code:
exit 0
It can be two (2) lines. But I recommend that you (oh, sorry, your sister) makes it mandatory to always include a proper exit status in her scripts. It is bad practice not to.

So, on second thought, when your sister gets better, tell her to go find a PROPER course in GNU/Linux, because this one seems pretty awful.

Best regards,
HMW
 
1 members found this post helpful.
  


Reply



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 script to access all files in a directory shinni Programming 5 04-24-2009 03:46 PM
Traversing files in a given directory using a bash script kaujot Programming 7 03-07-2008 12:39 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
Creating a bash script which understands paths to files en8scl Linux - Newbie 9 05-21-2006 07:22 PM
Can't create .ISO with full directory paths (-graft-points?) tdp05 Linux - Newbie 0 08-17-2004 04:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:54 PM.

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