TIPS &TRICKS
Searching the History command using Control+R
I strongly believe that what I’m going to recommend may well become your most frequently-used feature of History. When you’ve already executed a very long command, you can simply search History using a keyword and re-execute the same command without having to type it fully.
Press Control+R and type the keyword. In the following example, I searched for red, which displayed the previous command cat /etc/redhat-release in History, which contained the word red.
Pressing Ctrl+R from the command prompt will display the reverse-i-search prompt shown below:
(reverse-i-search)`red': cat /etc/redhat-release
Note: Pressing Enter when you see your command will execute it from History.
# cat /etc/redhat-release
CentOS release 6.4 (Final)
Sometimes you want to edit a command from History before executing it; for example, you can search for httpd, which will display the service httpd stop from the command history. Select this command and change the Stop to Start, and re-execute it again as shown below.
(reverse-i-search)`httpd': service httpd stop
Now press either the left arrow or right arrow key when you see your command, and this will display the command for you to edit before executing it, as follows:
# service httpd start
Now let us see how we can use the previous command quickly using four different methods.
Sometimes, you may end up repeating the previous commands for various reasons. The following are the four different ways to repeat the last executed command.
1. Use the up arrow to view the previous command and press Enter to execute it.
2. Type ‘!!’ and press Enter from the command line.
3. Type ‘!-1’ and press Enter from the command line.
4. Pressing Ctrl+P will display the previous command. Then press Enter to execute it.
Examples:
# !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 enjoy this trick and I hope to soon share more of them with you.
—Sivakumar E, sivakumar.e@gmail.com
Listing the package for a particular binary/command
When you are not sure which package contains the binary/command you are looking for, you can run the following commands:
#yum whatprovides "*/mkpasswd"
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirror.pac-12.org
* epel: mirrors.xmission.com
* extras: mirror.hmc.edu
* updates: mirror.n5tech.com
expect-5.45-14.el7_1.x86_64 : A program-script interaction
and testing utility
Repo : base
Matched from:
Filename : /usr/bin/mkpasswd This lists the package that contains the 'mkpasswd' binary.
Note: If you are searching for a third party binary/ command, then make sure you’ve enabled third party repositories like too.
—Naresh Kumar, nareshkumarvn@gmail.com
Deleting a lot of files from a Linux directory
When we have a lot of files in a directory, regular commands such as rm * fail, giving the following error:
"argument list too long"
This is because the * is replaced by the list of all file names in that directory before the rm command gets executed.
If it is okay to rename the directory and create a new one with the same name, permissions and ownerships, then this is one way of doing it. Ensure the relevant services which use this directory are restarted in this case.
If it is not okay to rename the directory, we can use the Find command as follows from within that directory:
find . -delete The above command is much faster than the following: find . -exec rm {} \;
If we want to remove only the files but not directories, use the command given below:
find . -type f -delete
One common scenario in which this is helpful is when removing a lot of spam mails from a mail directory.
—Sisirkumar K, info@theuxgroup.com
Setting a process for CPU affinity
CPU affinity means binding a process to a given set of CPUs. Let us check the CPU affinity for a process. First, let’s find the PID of the process to be checked. In the case of my example, the PID is 22353 and its CPU affinity is set to CPU 0.
[root@Linux-Test1 ~]# ps -a Now let’s check the affinity, using the following code:
[root@Linux-Test1 ~]# taskset -cp 22353
pid 22353's current affinity list: 0
The example below shows how to change the CPU affinity of a running process to CPU 1.
[root@Linux-Test1 ~]# taskset -pc 1 22353
pid 22353's current affinity list: 0
pid 22353's new affinity list: 1
To run a new program on a specific CPU, use the following commands: #taskset -c 0 myscript.sh <-- This will run the
script on CPU 0.
#taskset -c 0,1 myscript.sh <-- In this case it will
use multiple cores[CPU 0 &1].
#taskset -c 0-4 myscript.sh <-- It will run on
multiple cores [CPU 0,1,2,3].
Hope this information on CPU affinity will help you run an application on multi-core processors.
—Sumit Chauhan, sumit1203@gmail.com
Back up and restore file permissions
Here is a tip to back up and restore file permissions in Linux.
To take a backup, go to the required directory (here we take /) and use the following command:
getfacl -R . > permissions.txt
To restore a file, use:
setfacl --restore=permissions.txt
—Natraj Solai, linuxraja@gmail.com