Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Sunday, 5 January 2014

Counting words on the command line

Recently, I have been writing a number of essays with various word limits. So, I have been finding myself in need of doing frequent word counts. Getting a word count for a file is easy using the wc utility.

wc -w /path/to/file

However, sometimes I just write a piece in a browser text box, which don't have word count. So, I want to get a word count from the console rather than saving it to a file and working with it. It turned out to be quite easy actually. I just had to cat the entire text and pipe that to wc.

cat << EOF | wc -w
>Your text here
>
>More here.
>EOF

Saturday, 14 December 2013

Checking memory usage for a set of processes

Recently, I had to monitor memory usage of a set of processes. I used pmap to help me out. The following script gave memory usage of each PID I was interested in.

for foo in `ps aux | grep <my_process_identifier> | awk -F ' ' '{print $2}'`
do
echo $foo
pmap $foo | tail -n 1
done

Thursday, 23 May 2013

Directory size listing

A lot of times when freeing up disk space [Well yes in today's world also people like me need to do it.], instead of opening my file manager and finding the size of the folders by checking the properties section in the right-click drop-down, I wanted to look at a list view where I can sort by folder size. Now, KDE's Dolphin file manager shows number of items within a folder but not the size and I anyways prefer the command line so I wanted a command line tool that lists size of directories. The amount of space left on the partitions can be easily found by the following command:

df -h

I also found a command for directory size.

du

However, its default behaviour is to show the size of files recursively. So, I do not get the size of the folders in the directory I am interested in. Instead, I get the size of files within those folders and recursively so forth. I needed to set the depth of search into the folders. I found a parameter with a similar name. The following command works just as I need.

du --max-depth=1 -h