Tuesday, 16 December 2014

Get the directory size from the terminal


Getting the size of the directory is easy with the du command, but if you want to get the size of all files in the directory  which should not include the sub-directory, you can use the command given below:

$ls -hs | head -1

This will give you the desired size.

Monday, 8 December 2014

Going invisible on the terminal


Did you ever think that you could type commands that would be invisible on your system but still would execute, provided you typed them correctly? This can be easily done by changing the terminal settings using the following command:

stty -echo


To restore the visibility of your commands, just type the following command:

stty echo

Monday, 17 November 2014

The power of hashtags


We use hashtags all over in Twitter. They are excellent for discovering and tagging long text. A # in the shell is used to comment a line. Hashtags can also make our workflow more productive. Let's see how this happens.

We normally type very long commands in our Bash shell and it becomes difficult to search for the same command from History. Here is a simple trick that will make searching easier. All you need to do is to append a hashtag at the end of every command you type. And later search for the hashtag in reverse-i-search (Ctrl + R). Adding a # at the end of the command will not affect the execution of the command since anything that follows # is treated as a comment in Bash - the text is silently ignored after #.

Let us understand the above procedure using an example.
Let us assume that I run the following command on my Bash shell:

$ tar xzvf filename #untar

Now, just press Ctrl + R in the terminal window and type untar; you will get the above command from the Bash history of commands.

Sunday, 16 November 2014

Delete files of the same filetypes in a folder and its sub-folders


Under Linux, you can delete files of a specific file type located in a folder and its sub-folders using the simple combination of the find and rm commands.
For example, if file types like .inf need to be deleted from all folders, we can execute the following command:

#find /path/to/the/folder/ -type f -iname "*.inf" -exec rm -vf {} \;

Or, if we want to remove files one be one, then we can use:

#find /path/to/the/folder/ -type f -iname "*.inf" -exec rm -iv {} \;

Thursday, 13 November 2014

Auto-insert text as a header in VIM


If you want to have headers or some text like '#!/bin/bash' or '#include' embedded when you open a file for C programming or bash, append the following lines in the .vimrc file:
       autocmd BufNewFile *.sh 0read ~/.vim/skeletons/skel_sh
       autocmd BufNewFile *.c 0read ~/.vim/skeletons/skel_c

skel_sh and skel_c will hold all that you want to add when it is a new file with the extension '.sh' or '.c'.

You can use this tip to customize further, as per your requirements.

Monday, 10 November 2014

Know your commands


Often, we need to know which command is packed in which rpm package. Here is a tip that will help you get to know the package name. Let us look at an example in which we need to know the name of the package that contains the ssh command.

$which ssh
/usr/bin/ssh

$rpm -qf /usr/bin/ssh
openssh-clients-5.5p1-21.fc14.2.i686

In the above commands, we get the location of ssh by running which and and then the rpm package name containing ssh is outputted by the second command.

This can be achieved in a single command by combing the both commands in one, as follows:

$rpm -qf `which ssh`
openssh-clients-5.5p1-21.fc14.2.i686

Note: Please be aware that the above tip may not output as desired for command aliases.

How to run a whole desktop environment on the remote machine with X11 session forwarding


To test you can first run something like this ssh -X -f username@host xclock and enter the password. You should then see the the clock window on your screen. If this does not work, please check in /etc/ssh/sshd_config whether X11 forwarding has been set to 'yes'.


You should see the GNOME panel appear on your local display.

Now, to run a whole desktop environment run

ssh -X -f username@host gnome-session

Finally, you can end your SSH session by pressing Ctrl-D.


Thursday, 6 November 2014

Counting the number of files in a directory



Here is a simple way to achieve this (hidden ones won't count):

echo * | wc -w

History of commands and searches in vi editor

In vi editor there are times when one fail to recollect a command which was ran few days back to do something. In this case history of commands and searches comes to help. This can be done in two ways:

  • Type q: for commands, or q/ for searches; or
  • Type : or / to start entering a command or search, then press the Ctrl-f ('cedit' key)