LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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-02-2012, 11:34 AM   #1
rajhans
LQ Newbie
 
Registered: Mar 2012
Location: India
Distribution: i'm a beginner
Posts: 10

Rep: Reputation: Disabled
script to tell user that no more size left in pendrive


i want to write a script in which user is copying files frm system to pendrive now i want to show the messege to user that "NEED EXTRA MEMORY" when pendrive is full..

df and du are two commands wich tells the size
 
Old 04-02-2012, 11:41 AM   #2
Sydney
Member
 
Registered: Mar 2012
Distribution: Scientific Linux
Posts: 147

Rep: Reputation: 36
You can run...
Code:
df -h | grep /I/assume/you/know/the/mount | awk '{print $5}'
into a variable while it is not 100%. Once it is 100% a while loop can end and spit out what you want.
 
Old 04-02-2012, 11:45 AM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,623

Rep: Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964
Quote:
Originally Posted by rajhans View Post
i want to write a script in which user is copying files frm system to pendrive now i want to show the messege to user that "NEED EXTRA MEMORY" when pendrive is full..

df and du are two commands wich tells the size
Spell out your words. And honestly, why bother? The standard Linux "cp" command will tell you if the destination device is full already....
 
Old 04-02-2012, 06:25 PM   #4
josephj
Member
 
Registered: Nov 2007
Location: Northeastern USA
Distribution: kubuntu
Posts: 214

Rep: Reputation: 112Reputation: 112
Quote:
Originally Posted by Sydney View Post
You can run...
Code:
df -h | grep /I/assume/you/know/the/mount | awk '{print $5}'
into a variable while it is not 100%. Once it is 100% a while loop can end and spit out what you want.
In most cases that won't work as expected. If you have 5k left and you try to add a 6k file, it will fail, but the drive will still have 5k free. This would, at best, confuse the user. At worst, it could generate an endless loop.

This is a classic case of a simple concept that's not so simple to implement. It ended up being more complex than I innitially thought it would be when several fine points of "the way things work" surfaced.

If you're really masochistic, you could write an "add to thumbdrive" script:
Run df on the thumbdrive and capture the fourth field of the second line of output with sed or awk. That's the number of (1k) free blocks on the drive.

Run du on the file or directory to be transferred and capture the first field on the last line which is the total number of (1k) blocks to be copied.

Test that the number of blocks to be copied (rounded up to the nearest multiple of 4) is less than the number of free blocks available.

If it's less, copy it. If not, issue a not enough space message. The 4 is the device block size - which for most modern disks is 4k. Files are alocated in blocks, not bytes, so their physical size will be a multiple of 4k blocks. If your drive uses a different block size, then use that instead.

(Upon rereading this: even this isn't good enough because you have to round up the size of each file! It would be easier just to add a fudge factor of "reserved" extra free space 2k times the maximum number of files you'll copy at one time. That would eliminate *almost* all false positives)

This algorithm could also generate false negatives (failure to copy) if some of the files to be copied were already present on the destination - so that some of the destination space would be reused.

These things can be fixed, but it turns a single command into a 30 - 50 line script or even larger.

Although this would work, it's a lot more trouble than just letting cp complain as suggested above.

Where it would make a difference is with copying multiple files at a time. In that case, some of the files may be copied before space runs out and they would have to be dealt with manually or by doing something like first placing them in a temporary location on the destination where they can easily be deleted. After a successful copy, they would just be moved (mv) to their final destination - which just adjusts directory entries and doesn't move any actual content a second time.
 
Old 04-03-2012, 02:45 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
It could be made much less complex by simply testing the available free space of the target at the start. If the remaining amount is less than a preset amount, say 5% or 10MiB, then have it throw up a warning and an option to abort the action.
 
Old 04-03-2012, 08:21 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by rajhans View Post
i want to write a script in which user is copying files frm system to pendrive now i want to show the messege to user that "NEED EXTRA MEMORY" when pendrive is full.
Is the user logged on at a virtual terminal or logged on to a graphical desktop?

How is the user copying files?
 
Old 04-04-2012, 07:39 PM   #7
josephj
Member
 
Registered: Nov 2007
Location: Northeastern USA
Distribution: kubuntu
Posts: 214

Rep: Reputation: 112Reputation: 112
@David The H. - your solution would be much simpler. Mine was developed step by step and it gradually became ridiculously complicated.

It's worth leaving there just to show how problems that sound simple end up complicated and then how stepping back and looking at the big picture (as you did) can still lead to a simple solution.
 
Old 04-05-2012, 10:42 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
I fully agree. Your first post demonstrates very clearly how "simple" problems often demand complex solutions. And yes, I posted to show how you should stop and first think carefully about what you really need, rather than getting too caught up in the details.

But then again, the complex way may actually be necessary, depending on the exact nature of the problem. The user may really need the copy operation to stop when the drive is full, for example, in which case the amount of empty space would need to be checked every time, just before a file is copied.
 
  


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
rootfs 0 size left sjcoder Linux - Newbie 1 10-05-2009 12:23 AM
size of the partition....(no space left) @d4M Linux From Scratch 1 01-23-2009 04:55 PM
Total partition size - User partition size is not equals to Free partition size navaneethanj Linux - General 5 06-14-2004 12:55 PM
ext3. size:26gb,space used-24gb...0 bytes left?? jago25_98 Linux - Software 4 12-09-2003 01:43 PM
Mounting pendrive as a normal user (non-root) hbarbosa Linux - Hardware 1 11-03-2003 02:20 PM

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

All times are GMT -5. The time now is 11:27 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