As of 2016-02-26, there will be no more posts for this blog. s/blog/pba/
Showing posts with label file. Show all posts

du -sh is one of the common ways which I utilize du command, I used it to get the total disk size of current directory occupied. Another one is du -hd1, getting the disk sizes of each subdirectory uses, it lists one by one instead of a grand amount.

But how about the total sizes of individual files which you are interested? Not indented to show off my AWK scripting skill, but I did use AEK to sum it up byte counts from find or ls command if it’s too complicated, i.e. involving some directories. To be honest, that shows no skill at all, 10-minute AWK noob can do that and only reveals how I was unfamiliar with du command and clearly I didn’t RTFM. From its manpage:

-c, --total produce a grand total

It’s as simply as that and I didn’t even know before. So, basically, you can do:

find -L -name 'PATTERN' -print0 | du -ch --files0-from -

Or simply, if filenames do not contain spaces:

du -ch $(find -L -name 'PATTERN' -print0)

That’s all you need, although you still need some knowledge of find. The -L is for symbolic link (symlink), you can ignore/omit that if you don’t even know what it is, you probably don’t need that. For files in current directory, you can use it as if it’s a ls command, for example:

du -ch *.txt

That’s all.

I used to run the following commands to find out if a command exists (besides using Bash auto-completion if it's executable in PATH) and what kind of the command is:

$ type which
which is /usr/bin/which
$ file /usr/bin/which
/usr/bin/which: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.9, stripped

And I am not kidding you, I manually typed /usr/bin/which in the second command to find out the file type. file requires a full path. I feel little embarrassed. :) But, it's nothing wrong with it.

However, the better ways can be:

$ file `type -p which`
$ file `which which`

Note that type is a Bash shell builtin command (can differ from your environment), which is a normal program. You can also use locate for file'd a list of matched files if you only have partial filename. All three commands can accept a number of filenames.

combine is a Perl script from moreutils, I recently found it might be useful after I stopped introducing these nice programs from moreutils.

You can use it as in the following command usages:


combine file1 and file2
combine file1 not file2
combine file1 or file2
combine file1 xor file2

Also supports - as standard input.

Here is an example case I could thing of, say an engaged couple is getting married and they need to sort out who should be in their guest lists. They decide each of them can invite ten guests, so here is their own lists:


% cat groomslist
Bryan Shreve
Dennis Peachey
Eugene Scherer
Jessen
Jonathan Rochelle
Katherine Levin
Kay Begin
Nicholas Jarrell
Russell Bennett
Verna Butcher

% cat brideslist
Bryan Shreve
Dennis Peachey
Elizabeth Bixler
Janet Denison
Jeff Suh
Jeremy Friedman
Jonathan Rochelle
Katherine Levin
Russell Bennett
Shelley Tisdale

The first thing, they want to know who are invited by both:


% combine groomslist AND brideslist
Bryan Shreve
Dennis Peachey
Jonathan Rochelle
Katherine Levin
Russell Bennett

And the bride wants to check if her lovely fiancĂ© and husband-to-be invites some girls which he shouldn’t put them in the list:


% combine groomslist NOT brideslist
Eugene Scherer
Jessen
Kay Begin
Nicholas Jarrell
Verna Butcher

After groom passes the test, they move on to check who are invited only by one party:


% combine groomslist XOR brideslist
Eugene Scherer
Jessen
Kay Begin
Nicholas Jarrell
Verna Butcher
Elizabeth Bixler
Janet Denison
Jeff Suh
Jeremy Friedman
Shelley Tisdale

Now, they can decide if they really want to invite those people. Also since they have five common guests, therefore they can each invite more quests to fill up empty seats. If they want to edit, using vimdiff might be a good idea to merge the files.

combine does not require files to be sorted. If you want to use normal tools, for example:


% diff groomslist brideslist
3,4c3,6
< Eugene Scherer
< Jessen
---
> Elizabeth Bixler
> Janet Denison
> Jeff Suh
> Jeremy Friedman
7,8d8
< Kay Begin
< Nicholas Jarrell
10c10
< Verna Butcher
---
> Shelley Tisdale

% diff groomslist brideslist | grep ^<
...

Those files have to be sorted to get correct result.

I wanted to know how I could monitor file system changes, especially activities on my home directory, and this was about this thread. I was thinking the issue could be solved by generating a snapshot of files using find, then watching changes and making changes to the snapshot file. Using grep to do the search.

But, that’s simply stupid. Just using find would do that OP wants since that program OP uses only search for file names and folder names. For the first run of find it might be slower, 31000 more files took about six seconds. Well it’s not really slow. The consecutive runs only take about 0.1 seconds even after you add or delete files.

Anyway, it’s still fun to know how to monitor files. I installed inotify-tools package for inotifywait program.

http://farm6.static.flickr.com/5087/5205625460_60c806f25a.jpg

Here is the script I use:

inotifywait -m -r --format $'%T %e %w%f' --timefmt '%H:%M:%S' --exclude ~/'(\.mozilla|Documents/KeepNote)' -e modify -e move -e create -e delete ~ 2>&1 | awk '/^[0-9]/ {
sub(/'"${HOME//\//\\/}"'/, "~", $0)
split($0, a, " ")
len=length(a[1])+length(a[2])+1
printf "%-20s %s\n", substr($0, 0, len), substr($0, len+2)
// flush stdout
system("")
next
}
{print ; system("")}
' | tee -a /tmp/home_monitor

-m and -r are for recursively monitoring on files. I set up the output format and timestamp format. I exclude two things from being listed, one is ~/.mozilla and another is KeepNote’s files. You can only have one --exclude supplied, if you have more than one, then only the last one would be effective. You have to group them up. -e specify events you want to monitor.

I use awk to do format adjustment, column alignment. Also replacing the literal of your home directory, /home/username, with just ~. Then I pipe the output to tee, so I can see on screen and write output to file at the same time. This way, Conky can ${tail /tmp/home_monitor 10} the file.

I was planning to use named pipe:

% MPIPE=/tmp/home_monitor
$ mkfifo $MPIPE
$ inotifywait ... | awk ... >$MPIPE
$ tail -f $MPIPE

It will save some disk space, but it doesn’t work with Conky.

If you want to clean up /tmp/home_monitor when it gets too big, just do echo '' > /tmp/home_monitor. An important note for you, when you tail a file in Conky, make sure it exists. If you remove the file, Conky exits. I was hoping there was a Conky variable would run program in a thread and do similar task as $tail does, only the input is the standard output of that thread not a file.

By the way, I had never thought even in my own home directory would have many activities.