Tuesday, January 15, 2013

Some useful stuff

Here are a few useful commands to make your work easier in Linux.

Emptying the contents of a file without deleting it

We use the cat command to do this.Just issue this command on the console

cat /dev/null > filename/filepath

The file name/path should be replaced by the filename of file path you wish to empty.

Transferring file/directory to a remote machine without scp

Suppose you want to transfer a file existent on your system to your colleague's box.You both use a debian system.The first thought would be to use the scp command.The scp works over ssh.Your colleague's system doesn't have an ssh server installation.What to do then?Well the solution to this is the netcat(nc) command.

On your system  

Pipe the output of file through cat and set netcat to listen on some random port(say 5001).
cat file-to-send | nc -l 5001

On your colleague's system

Open a nc session to port 5001 and redirect the output to a local file.
nc your-box-ip-address 5001 > filename

Creating a directory stack for quick navigation

If you need to switch to another directory for a bit and then come back to the current one,you can use the directory stack.Use the following commands at the console.Just notice the path from where you execute these commands

pushd /var/log : Takes you to the 'log' directory under 'var'

popd : After doing some work(in /var/log), you want to return to you previous directory.Issue this command and you are back.

Remove a file/directory interactively using rm

We all know that the rm command is used to remove a file/directory in linux.However,in some situations we might remove a wrong file accidently when using rm command.To avoid such accidents we make the rm command interactive by aliasing it.We put this alias in the ~/.bashrc file.

To edit the .bashrc file just type the following command
vi ~/.bashrc

Once in the file,add this line anywhere in the file
alias rm='rm -i'
save the file and exit the vi editor

To see the effect,just close the current terminal/console and open the new one.Now whenever you try to remove a file/directory,the shell prompts before any deletion.

The whatis and which commands

These are two very useful commands.The whatis command displays a single line description about the command.The syntax is
whatis [command]

Remove [] when specifying the command :-)
There is always the linux man pages,in case you want to know about a command in details.

The which command returns the absolute path of an executable that is called when a command is issued.This is specially useful in determining whether you are using a locally compiled version or the distributed version of the program.
The syntax is
which [command]

For example which mysql returns

Thats it for now.I will regularly post this kind of stuff.


No comments: