OpenSource For You - - Tips & Tricks -

Searching the His­tory com­mand us­ing Con­trol+R

I strongly be­lieve that what I’m go­ing to rec­om­mend may well be­come your most fre­quently-used fea­ture of His­tory. When you’ve al­ready ex­e­cuted a very long com­mand, you can sim­ply search His­tory us­ing a key­word and re-ex­e­cute the same com­mand with­out hav­ing to type it fully.

Press Con­trol+R and type the key­word. In the fol­low­ing ex­am­ple, I searched for red, which dis­played the pre­vi­ous com­mand cat /etc/redhat-re­lease in His­tory, which con­tained the word red.

Press­ing Ctrl+R from the com­mand prompt will dis­play the re­verse-i-search prompt shown be­low:

(re­verse-i-search)`red': cat /etc/redhat-re­lease

Note: Press­ing En­ter when you see your com­mand will ex­e­cute it from His­tory.

# cat /etc/redhat-re­lease

Cen­tOS re­lease 6.4 (Fi­nal)

Some­times you want to edit a com­mand from His­tory be­fore ex­e­cut­ing it; for ex­am­ple, you can search for httpd, which will dis­play the ser­vice httpd stop from the com­mand his­tory. Se­lect this com­mand and change the Stop to Start, and re-ex­e­cute it again as shown be­low.

(re­verse-i-search)`httpd': ser­vice httpd stop

Now press ei­ther the left ar­row or right ar­row key when you see your com­mand, and this will dis­play the com­mand for you to edit be­fore ex­e­cut­ing it, as fol­lows:

# ser­vice httpd start

Now let us see how we can use the pre­vi­ous com­mand quickly us­ing four dif­fer­ent meth­ods.

Some­times, you may end up re­peat­ing the pre­vi­ous com­mands for var­i­ous rea­sons. The fol­low­ing are the four dif­fer­ent ways to re­peat the last ex­e­cuted com­mand.

1. Use the up ar­row to view the pre­vi­ous com­mand and press En­ter to ex­e­cute it.

2. Type ‘!!’ and press En­ter from the com­mand line.

3. Type ‘!-1’ and press En­ter from the com­mand line.

4. Press­ing Ctrl+P will dis­play the pre­vi­ous com­mand. Then press En­ter to ex­e­cute it.


# !ps ps aux | grep

apache 25452 0.1 0.2 454232 18524 ? S 08:58

0:32 /usr/sbin/httpd apache 25754 0.1 0.2 454232 18496 ? S 09:07

0:31 /usr/sbin/httpd

apache 25783 0.1 0.3 461520 25796 ? S 09:08

0:31 /usr/sbin/httpd

Hope you en­joy this trick and I hope to soon share more of them with you.

—Si­vaku­mar E, si­vaku­[email protected]

List­ing the pack­age for a par­tic­u­lar bi­nary/com­mand

When you are not sure which pack­age con­tains the bi­nary/com­mand you are look­ing for, you can run the fol­low­ing com­mands:

#yum what­pro­vides "*/mk­passwd"

Loaded plug­ins: fastest­mir­ror

Load­ing mir­ror speeds from cached host­file

* base: mir­

* epel: mir­rors.xmis­

* ex­tras: mir­

* up­dates: mir­

ex­pect-5.45-14.el7_1.x86_64 : A pro­gram-script in­ter­ac­tion

and test­ing util­ity

Repo : base

Matched from:

File­name : /usr/bin/mk­passwd This lists the pack­age that con­tains the 'mk­passwd' bi­nary.

Note: If you are searching for a third party bi­nary/ com­mand, then make sure you’ve en­abled third party repos­i­to­ries like too.

—Naresh Ku­mar, nareshku­[email protected]

Delet­ing a lot of files from a Linux direc­tory

When we have a lot of files in a direc­tory, reg­u­lar com­mands such as rm * fail, giv­ing the fol­low­ing error:

"ar­gu­ment list too long"

This is be­cause the * is re­placed by the list of all file names in that direc­tory be­fore the rm com­mand gets ex­e­cuted.

If it is okay to re­name the direc­tory and cre­ate a new one with the same name, per­mis­sions and own­er­ships, then this is one way of do­ing it. En­sure the rel­e­vant ser­vices which use this direc­tory are restarted in this case.

If it is not okay to re­name the direc­tory, we can use the Find com­mand as fol­lows from within that direc­tory:

find . -delete The above com­mand is much faster than the fol­low­ing: find . -exec rm {} \;

If we want to re­move only the files but not di­rec­to­ries, use the com­mand given be­low:

find . -type f -delete

One com­mon sce­nario in which this is help­ful is when re­mov­ing a lot of spam mails from a mail direc­tory.

—Sisirku­mar K, [email protected]­

Set­ting a process for CPU affin­ity

CPU affin­ity means bind­ing a process to a given set of CPUs. Let us check the CPU affin­ity for a process. First, let’s find the PID of the process to be checked. In the case of my ex­am­ple, the PID is 22353 and its CPU affin­ity is set to CPU 0.

[[email protected] ~]# ps -a Now let’s check the affin­ity, us­ing the fol­low­ing code:

[[email protected] ~]# taskset -cp 22353

pid 22353's cur­rent affin­ity list: 0

The ex­am­ple be­low shows how to change the CPU affin­ity of a run­ning process to CPU 1.

[[email protected] ~]# taskset -pc 1 22353

pid 22353's cur­rent affin­ity list: 0

pid 22353's new affin­ity list: 1

To run a new pro­gram on a spe­cific CPU, use the fol­low­ing com­mands: #taskset -c 0 <-- This will run the

script on CPU 0.

#taskset -c 0,1 <-- In this case it will

use mul­ti­ple cores[CPU 0 &1].

#taskset -c 0-4 <-- It will run on

mul­ti­ple cores [CPU 0,1,2,3].

Hope this in­for­ma­tion on CPU affin­ity will help you run an ap­pli­ca­tion on multi-core pro­ces­sors.

—Su­mit Chauhan, [email protected]

Back up and re­store file per­mis­sions

Here is a tip to back up and re­store file per­mis­sions in Linux.

To take a backup, go to the re­quired direc­tory (here we take /) and use the fol­low­ing com­mand:

get­facl -R . > per­mis­sions.txt

To re­store a file, use:

set­facl --re­store=per­mis­sions.txt

—Na­traj So­lai, lin­[email protected]

Newspapers in English

Newspapers from India

© PressReader. All rights reserved.