Showing posts with label xargs. Show all posts
Showing posts with label xargs. Show all posts

Wednesday, January 16, 2013

Linux Util 3

1) To kill all processes of a process tree, say a bunch of  child processes spawned by a parent process (note that all the child processes have pids greater than that of parent's):

kill -9 -(ppid)

where ppid stands for parent pid. (just a negation symbol before the parent process id)

Another way to find the parent id (is same as process group id):

ps -eo "%p %r %c %a"
 
stackoverflow-page helped to solve this problem.

2) If you work by logging to your office machine (Linux server) from windows client machine using NoMachine (NX), and when you open lot of xterms, it is oftentimes confusing what each xterm window corresponding based just on the title of it. Just follow these steps to get new informative title (but this is active only for this session of your login)

unset PROMPT_COMMAND (assuming you have this env variable set in your .bashrc)
echo -ne "\033]0;title\007"
It is important to blindly follow the characters and their sequence in the echo command. Obviously, they mean something, but to keep the brevity, I am refraining from giving the explanation here.

3) If you want to do copy/paste operation in xterm, 

   a) highlight the text using mouse
   b) use middle button/scroll wheel of mouse to paste or
   c)  shift+insert to paste or when there is no middle button,or
   d) simultaneously click both mouse buttons to emulate middle-button click

Thanks to Ubuntu Forums for this help. 

4) All along I have been using pdftops - psselect - ps2pdf combination to select pages from a pdf document, but Today (Jan 23, 2013) I learnt about pdftk from
LinuxJournal to select pages from pdf document directly without converting to ps first. How nice!!

pdftk A=100p-inputfile.pdf cat A22-36 output outfile_p22-p36.pdf  

Input file is 100p-inputfile.pdf. Though the name sounds confusing, there is no need to prefix the name with 100p (100 pages).  It is just for this particular example. This command selects pages from 22 to 36 and creates an output file with just those pages.

5) April 17, 2013: Found this tip while wanting to put all docx files of submitted student homeworks in one directory to upload to google drive and open them there in order not to miss pictures or other openoffice incompatible things.
Thanks to Nixcraft I could do this and this is the first time I really found a way to use xargs command.

find . -name "*.docx" -print0 | xargs -0 -I file mv file onlydocs/


6)  To sync a local directory to a remote directory:

rsync -r -a -v -e "ssh -l yourname" local/directory/path remote.machine.address:remote/directory/path

You will be prompted to enter the password to the remote machine. If all else is correct, this should sync the files. Thanks to Nixcraft for this tip.

Sunday, December 25, 2011

Linux Util 1

1) To paste one of the columns of a multi-column file as one other column in the same file:
cut -f3 -d' ' fname1|paste -d' ' - fname1 > fname2

This command takes the 3rd column of file fname1 and pastes it as first column and writes the content to file fname2. Rename the file fname2 to fname1 to avoid keeping duplicate copies. The hyphen in the paste command can be switched to last to make the 3rd column as the last column instead of first. Here the column separator is the space character given after the '-d' switch.


2) I forget to leave space around binary operator used in expr command in shell scripting.

3) I  keep forgetting about the rules of double quotes and single quotes usage in shell scripting. If you give a single quoted word inside double quoted sentence, the single quote is not interpreted, but a $variable is substituted with its value.

4) To find all C/C++ program files and related header files:

find . -regex '.*\.\(c\|h\|cxx\|cpp\)' -print

This command fetches files with extension .c, .h, .cxx, .cpp and prints their path from the current directory. Please note the extra characters ".*." in the front. The second dot is escaped and so is the first open parenthesis and logical OR symbol '|'.

5) A modification to the above command is by replacing the last 'print' option to 'print0'. That is append number zero to print word. This will display all the file names in one line as against the previous output that prints one file name in one line.

6) If you want the contents of all these files in one go, give the following:

find . -regex '.*\.\(c\|h\|cxx\|cpp\)' -print0|xargs -o cat

7) To remove all spaces in the filenames of a directory, get inside the directory and type in command line:

rename 's/ //g' *

Here, the single space after first '/' is important. Replace '*' with other names if you do not want to apply this change to all files.