track the size of the directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting track the size of the directory
# 8  
Old 08-16-2008
With find -exec you can avoid the pesky quoting / special file name issues:

Code:
find . -type f -atime +10 -exec mv {} /tmp/old \;

If your find has -print0 then you can also use that; it was designed specifically as a workaround for this scenario.

Code:
find . -type f -atime +10 -print0 | xargs -0i mv {} /tmp/old

This is an extension of GNU find, though.

How about make a temporary file of the specified size, format it as a partition, mount it, and simply write to it until it's full?
# 9  
Old 08-16-2008
Quote:
Originally Posted by era
With find -exec you can avoid the pesky quoting / special file name issues:

Code:
find . -type f -atime +10 -exec mv {} /tmp/old \;

If your find has -print0 then you can also use that; it was designed specifically as a workaround for this scenario.

Code:
find . -type f -atime +10 -print0 | xargs -0i mv {} /tmp/old

This is an extension of GNU find, though.

How about make a temporary file of the specified size, format it as a partition, mount it, and simply write to it until it's full?
-print0 won't fix the problem I described. I can still inject a path to trick mv:

Code:
Tsunami size_directory # find . -type f -print0|xargs -0i echo mv {} /tmp/old
mv ./unix/ ../etc/passwd /tmp/old
Tsunami size_directory #

# 10  
Old 08-16-2008
But xargs will supply (the moral equivalent of) the necessary quoting.

Code:
vnix$ mkdir -p "unix/ ../etc"
vnix$ touch "unix/ ../etc"/passwd
vnix$ find ./unix -name passwd -print0 | xargs -0i mv -v {} /tmp/
`./unix/ ../etc/passwd' -> `/tmp/passwd'

This is on Ubuntu but should work on any system with GNU find.
# 11  
Old 08-16-2008
Oops, my bad. {} is taken as a single quoted argument. It was "echo" who mislead me. Sorry.
# 12  
Old 08-17-2008
Actually, to be perfectly precise, it's xargs -0 which is the key there. File names cannot contain a null byte because it's the terminator character of the underlying string encoding convention. find -print0 uses that to unambiguously output filenames with (otherwise) arbitrary character data in them, and xargs -0 conveniently knows how to handle that. Even more conveniently, the stream of null-terminated file names from find can be passed as is to exec by xargs without any additional translation or fiddling.
# 13  
Old 08-17-2008
Yeah in other words instead of using a whitespace as a terminating char in the output it does it right by using the proper '\0' (null byte).

So if no -print0 is used the output could be "output1/ output2/output2\n\0" and then a second program that uses that output would break it into two arguments (using whitespace as a separating reference). In the other hand, if -print0 is used then the output would be "output1/ output2/output2\0" and then xargs -0 (the second program) instead of using whitespace as a separating reference uses '\0' thus considering that output has a single argument.

Unfortunately I think the every-day bash programmer wouldn't understand this very easily. More manual information is needed.
# 14  
Old 08-17-2008
Actually, xargs without -0 breaks up input on newlines, but that's still a vulnerability because you can create a file called /tmp/(newline)/etc/passwd

If by "everyday" you mean people who fail to double-quote variables which might contain whitespace then yes, this is probably way beyond their grasp. But surely, nobody who reads this forum on a regular basis would still be doing that? (Ha ha only serious.)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Ls directory size reporting byte size instead of file count

I have been searching both on Unix.com and Google and have not been able to find the answer to my question. I think it is partly because I can't come up with the right search terms. Recently, my virtual server switched storage devices and I think the problem may be related to that change.... (2 Replies)
Discussion started by: jmgibby
2 Replies

2. Shell Programming and Scripting

How to delete some of the files in the directory, if the directory size limits the specified size

To find the whole size of a particular directory i use "du -sk /dirname".. but after finding the direcory's size how do i make conditions like if the size of the dir is more than 1 GB i hav to delete some of the files inside the dir (0 Replies)
Discussion started by: shaal89
0 Replies

3. UNIX for Dummies Questions & Answers

directory tree with directory size

find . -type d -print 2>/dev/null|awk '!/\.$/ {for (i=1;i<NF;i++){d=length($i);if ( d < 5 && i != 1 )d=5;printf("%"d"s","|")}print "---"$NF}' FS='/' Can someone explain how this works..?? How can i add directory size to be listed in the above command's output..?? (1 Reply)
Discussion started by: vikram3.r
1 Replies

4. Solaris

Directory size larger than file system size?

Hi, We currently have an Oracle database running and it is creating lots of processes in the /proc directory that are 1000M in size. The size of the /proc directory is now reading 26T. How can this be if the root file system is only 13GB? I have seen this before we an Oracle temp file... (6 Replies)
Discussion started by: sparcman
6 Replies

5. UNIX for Dummies Questions & Answers

directory size with ls -l

am I right in assuming that in unix a directory size is just information about that directory stored somewhere on the file system, and not a sum of its contents? This is because ls -l gives 1024 as my directory size, when the directory contains many gigs worth of stuff. also, is du -sk dir ... (2 Replies)
Discussion started by: JamesByars
2 Replies

6. UNIX for Dummies Questions & Answers

Possible to track FTP user last login? Last and Finger don't track them.

Like the topic says, does anyone know if it is possible to check to see when an FTP only user has logged in? Because the shell is /bin/false and they are only using FTP to access the system doing a "finger" or "last" it says they have never logged in. Is there a way to see when ftp users log in... (1 Reply)
Discussion started by: LordJezo
1 Replies

7. Programming

Size of Directory

I have a C program to find out the total size of all the files in a directory. So what it does is.. it scans through all the files in the directory and does a stat() on each of them and sums up the size returned by st_size . Is there a quicker way to do this.. without doing a stat() on each... (1 Reply)
Discussion started by: tantric
1 Replies

8. AIX

size of directory with ls -l

hello When i do a "ls -l" in a directory (Aix 5.3), i have the result : >ls -l total 65635864 -rw-r--r-- 1 lobi system 2559909888 Feb 20 15:06 cible5.7bdat -rw-r--r-- 1 lobi system 1020098870 Feb 20 13:06 cible6.7bdat -rw-r--r-- 1 lobi system 1544789511 Feb 20 11:06 cible9.7bdat -rw-r--r--... (2 Replies)
Discussion started by: pascalbout
2 Replies

9. Programming

size of a directory

hai friends I need a program to find the size of a directory.. When i tried to get the size, it always gives the default space allocated for it. How can i findout the exact size of a directory using a c program Thanks in advance Collins (6 Replies)
Discussion started by: collins
6 Replies

10. UNIX for Dummies Questions & Answers

size of a directory?

hi, say i have the following directory structure a/b/c/d... can i do df -kt /a/b/c/d and the output will gives me the total space of the directory space in d? or the output will just be the total space of the parent directory a. hope its clear.. (2 Replies)
Discussion started by: yls177
2 Replies
Login or Register to Ask a Question