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 05-05-2009, 08:12 AM   #1
nemajan
LQ Newbie
 
Registered: May 2009
Posts: 9

Rep: Reputation: 0
Bash exercise


Hi!

I'm newbie Linux user, and i have to do an exercise. Its a bash script.
Here is the description:

"Write a script that finds every file that is older than N weeks and greater then M Gb-s."

I suppose every file on the hard disk.

I would really thank your help.

Z
 
Old 05-05-2009, 08:16 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

This looks like homework, which we don't do for you.

What have you tried and what did/didn't work?
 
Old 05-05-2009, 08:20 AM   #3
nemajan
LQ Newbie
 
Registered: May 2009
Posts: 9

Original Poster
Rep: Reputation: 0
Well, i tried using du for getting the size of the file. But i have no idea in what options should i use with du. Then, i don't know how to decide if its greater then M Gb-s.

Something like this : du -h G ? Is this okay?

The main problem is that i don't have experience with Bash and i don't have time to practice...
 
Old 05-05-2009, 08:37 AM   #4
SlowCoder
Member
 
Registered: Oct 2004
Location: Southeast, U.S.A.
Distribution: Fedora (Desktop), CentOS (Server), Knoppix (Diags)
Posts: 934

Rep: Reputation: 38
Research the 'find' command.
 
Old 05-05-2009, 08:50 AM   #5
nemajan
LQ Newbie
 
Registered: May 2009
Posts: 9

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by SlowCoder View Post
Research the 'find' command.
Thx man... that is something that is easy with experience...

find . -atime +7*n -size m*G -print

This, running from the top of the directory tree should be okay imo. Is it right?

Last edited by nemajan; 05-05-2009 at 08:53 AM.
 
Old 05-05-2009, 08:55 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by nemajan View Post
The main problem is that i don't have experience with Bash and i don't have time to practice...
Actually you must spend some time reading the manual pages and a good tutorial (for example http://rute.2038bug.com/index.html.gz). The solution to your problem is written in the question itself: "Write a script that finds...". Read the manual page of the find command
Code:
man find
looking for the needed options under the "TEST" section.
 
Old 05-05-2009, 08:56 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

You're getting close:

Find needs a starting point and your options aren't correctly implemented:

find . -atime +7 -size +10G

The dot (.) tells find to start where you started the command. find /home/whatever will start looking in /home/whatever, no matter where you enter the command. You do not need the *n and *G.

If you enter a size (-size option) and do not include - (less then) or + (greater then) it will only return files that are 10G.

Hope this clears things up a bit.
 
Old 05-05-2009, 09:01 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by nemajan View Post
find . -atime +7*n -size m*G -print

This, running from the top of the directory tree should be okay imo. Is it right?
Just try that and... read the error messages (if any) or see if the output matches exactly what you're trying to do. Since the problem is to find files older than... you have to use -mtime, since -atime look at the last access time, whereas the last modification time gives the actual age of the file.

Furthermore, you cannot do multiplication using the asterisk, since it has a special meaning in bash (is up to you to find out what it means).
 
Old 05-05-2009, 09:01 AM   #9
nemajan
LQ Newbie
 
Registered: May 2009
Posts: 9

Original Poster
Rep: Reputation: 0
Yeah, i think i'll be able to do it.

Thanks for the help!
 
Old 05-05-2009, 09:01 AM   #10
barunparichha
Member
 
Registered: Jun 2006
Location: Bangalore,india
Distribution: Linux(Redhat,fedora,suse,ubantu), Solaris (s8/s9/s10/nevada/open-solaris)
Posts: 285

Rep: Reputation: 32
You know the answer
Read bash scripting guide from tldp.
http://tldp.org/LDP/abs/html/


Best of luck!
 
Old 05-05-2009, 09:05 AM   #11
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by nemajan View Post
Yeah, i think i'll be able to do it.

Thanks for the help!
Good! Let us know your solution.
 
Old 05-05-2009, 09:07 AM   #12
nemajan
LQ Newbie
 
Registered: May 2009
Posts: 9

Original Poster
Rep: Reputation: 0
n=$1 * 7
m=$2
find . -mtime n -size mG -print


btw.: i'm not testing it now, because i can't, on this computer, i don't have Linux, i'm just trying to figure it out this way. I'll not forget it this way.
 
Old 05-05-2009, 09:13 AM   #13
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

If you want to use variables you need to changes the following:

find . -mtime ${n} -size ${m}G

You do need the curly brackets, otherwise the shell would not know what mG stands for (hence ${m}G).

Also: n=$1 * 7 will not work, you need to do something like this: let X=Y*7

Hope this helps.
 
Old 05-05-2009, 09:18 AM   #14
nemajan
LQ Newbie
 
Registered: May 2009
Posts: 9

Original Poster
Rep: Reputation: 0
Whis one ?

let "n=$1 * 7"
m=$2
find . -mtime ${n} -size ${m}G -print
 
Old 05-05-2009, 09:26 AM   #15
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by nemajan View Post
Whis one ?

let "n=$1 * 7"
m=$2
find . -mtime ${n} -size ${m}G -print
Almost there. The multiplication can be done in different ways:
Code:
let n="$1 * 7"
n=$(($1 * 7))
n=$(expr $1 \* 7)
the last one uses command substitution $(command) where the command to do arithmetics is expr. Note that the multiplication sign must be escaped using a backslash in this case.

Also, follow the advice by druuna in post #7 otherwise the -mtime and -size tests match only the exact values. Use the + sign for -mtime to match files "older than" and the + sign for -size to find files "bigger than".
 
  


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
shell scripting exercise siawash Linux - Certification 5 04-11-2009 06:06 PM
I need some help for a Linux exercise kromer12345 Programming 1 06-27-2007 01:25 PM
Exercise Logging Software Jonititan Linux - Software 3 03-17-2007 12:20 AM
Exercise: Life Without Oil rvijay General 61 08-13-2005 02:01 PM
help with this exercise njeri Programming 1 04-26-2005 08:35 AM


All times are GMT -5. The time now is 07:14 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration