Linux Tips

How to search a word in a directory which has more files

find [directory_path] -type f -name ‘*’ -exec grep -s [pattern] {} \; -print

How to find out elapsed time of a process

ps -eo “%p %y %x %c %t”|grep [pid]

How to remove the files in a directory if rm command fails with the error “too many arguments”

find [directory_path] -name ‘*’ | xargs rm

How to remove duplicate entries from history

export HISTCONTROL=ignoreboth

Lost Bash history?

If you have a terminal open in which you’re executing certain commands, then open another one and use that for a while. You’ll notice this new terminal doesn’t remember any of the commands typed in the first one. In addition, closing the first terminal, and then the second will overwrite any of the commands typed in the first terminal. This happens because Bash history is only saved when you close the terminal, not after each command. To fix this, add the following lines to your ~/.bashrc file:

shopt -s histappend
PROMPT_COMMAND=’history -a’

This will make Bash append an entry in its history after the execution of every command.

Password free environment with NIS

Save the below script in /etc/profile.d/ & give executable permission to it.

# If we are interactive and don’t have a public key, send prompt.
if [[ $INTERACTIVE != “false” ]] && [ ! -f $HOME/.ssh/ ]
echo “It doesn’t appear that you have set up your ssh key.”
echo “This process will make the files:”
echo ” ” $HOME/.ssh/
echo ” ” $HOME/.ssh/id_rsa
echo ” ” $HOME/.ssh/authorized_keys

ssh-keygen -t rsa
cat $HOME/.ssh/ >> $HOME/.ssh/authorized_keys
chmod 600 $HOME/.ssh/authorized_keys
chmod g-w $HOME

How to count number of files in a directory

$ find targetdir -type f -follow -maxdepth 1 | wc -l

Convertion of Mailbox format to Maildir format

mb2md -c -s [source_dir] -R -d [destination_dir]

Disk usage sort by size

du -k * | sort -nr | cut -f2 | xargs -d ‘\n’ du -sh

Recursively chmod directories only

find [dir_path] -type d -exec chmod 755 {} \;
This will recursively search your directory tree (starting at dir ‘dot’) and chmod 755 all directories only.

Similarly, the following will chmod all files only (and ignore the directories):

find . -type f -exec chmod 644 {} \;

The CPU states in the “top” command

eg: Cpu(s): 0.1%us, 4.2%sy, 20.0%ni, 75.5%id, 0.2%wa, 0.0%hi, 0.0%si, 0.0%st
us: userspace – what you’re running above the kernel
sy: system calls – what is running inside the kernel
ni: reniced processes.(attempting to run low priority processes)
id: idle
wa: waiting for i/o
hi: hardware interrupts – how much time is spent dealing with hardware
si: software interrupts – how much time is spent dealing with software-created interrupts (system calls, etc.)
st: steal time – the time Linux wanted to run on a CPU, but the hipervisor was not able to schedule CPU

Date & time in history command

Defining the environment variable as follows in .bashrc file


%d – Day
%m – Month
%y – Year
%T – Time

How to add a work or a letter at the beginning of every line in a file using vi editor

vi file name
:%s/^/[pattern to add]

How to add a word or a letter at the end of every line in a file using vi editor

vi filename
:%s/.*/&[pattern to add]

Note: Above tips are collected from various sources


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s