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

There were a few times that I wanted to copy the path of a configuration file, but this simple task seems not as easy as you think. It’s so strange that ls command has no such option for it, i.e. prefixing path, I am sure I have read all options.

A quick workaround I would do is:

echo $PWD foo<TAB> | xsel

In Bash, if you use / instead of space to separate the file name and use auto-completion, it will result a literal text \$PWD/foobar in prompt, which is not I want. And this method may have slight issue when the file is located in symlink (symbolic link) directory if you want to have the canonical path.

You may end up using echo $(pwd -P) foo<TAB>.

2   With find command

For multiple files listing, find is a better option for that, but you will need to specify path to find, or the output would be relative path. Also, same issue for symlink.

Combining with readlink is the best bet, which solves both issues:

find -exec readlink -f {} \;

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.