Thursday, January 22, 2015

How to manage files whose name starting with hyphen (-), or double hyphen (--)

There are a few ways you can manage these kind of files, the normal way won't work, since this filename will be treated as options for almost all commands. Please see below on the method to manage these files:

Let's say the file name is -p, and you are trying to delete it,  the usual error is, since the -p is being treated as the flag for command rm, rather than a file name:

$ rm -p
rm: invalid option -- 'p'
Try 'rm ./-p' to remove the file ‘-p’.
Try 'rm --help' for more information.


So, the correct way to manage this file is:

To list:

$ ls ./-p
$ ls -- -p
$ find . -maxdepth 1 -iname "-p"


To delete:
$ rm -- -p
$ rm ./-p
$ find . -maxdepth 1 -iname "-p" -delete


To create:
$ touch ./-p


Basically, the ./ can be used with any command, while the " -- " have been tested working with ls and rm.

Hope this is helpful, thanks to stackexchange for this useful tips.

Thursday, January 15, 2015

How to kill whole process group (parent + child process)

This is very easy, and all this while I have been using ps with grep and awk, just to get the parent and child process PID, and feed it to kill command to kill the whole lot of them. Now no more, that is why reading the man page is very beneficial ;). To kill the whole group process, please see below example.

Let's say I want to kill teamviewer, and it's child processes:

check what is teamviewer and its children's PID:


$ pstree -Gap 31458

teamviewerd,31458 -f

├─{teamviewerd},31460

├─{teamviewerd},31461

├─{teamviewerd},31462

├─{teamviewerd},31463

├─{teamviewerd},31464

├─{teamviewerd},31465

├─{teamviewerd},31466

├─{teamviewerd},31549

└─{teamviewerd},24892

run kill to the PID of parent, and put - sign in front of the PID, to signal the whole group killing:
$ kill -TERM -31458

no more PID 31458, with the children processes
$ pstree -Gap 31458