Tips and Tricks

Back up a file

cp -a /etc/httpd/conf/httpd.conf{,.bak}

cp -a /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.$(date +%m-%d-%Y)

Deleting all e-mails in mutt

hold shift + d ~ A

Display Color Man Pages

vi .bashrc

# Colors for Man Pages
export LESS_TERMCAP_mb=$'\E[01;31m'
export LESS_TERMCAP_md=$'\E[01;31m'
export LESS_TERMCAP_me=$'\E[0m'
export LESS_TERMCAP_se=$'\E[0m'
export LESS_TERMCAP_so=$'\E[01;44;33m'
export LESS_TERMCAP_ue=$'\E[0m'
export LESS_TERMCAP_us=$'\E[01;32m'

Prevent linux man pages from clearing after you quit reading

  1. vi .bashrc
export PAGER='less -X'
  1. vi /etc/man.config
PAGER           /usr/bin/less -isX
  1. man ping | less -X

Refusing logins for maintenance

vi /etc/nologin

System is currently undergoing maintenance until 8:00 AM EST.
Please come back later.

Find external IP

curl -s|cut -d ":" -f2 | cut -d "<" -f1

Brace expansion for some commands

cp /etc/httpd/conf/httpd.conf{,.bak}

mkdir /tmp/test{1..10}

mkdir -p /tmp/{usr,bin,lib64,dev}

mv file_name.{txt,doc}

Copy all files including hidden files

cp -a /path_to_source/. /path_to_destination

scp -pr /path_to_source/. /path_to_destination

Delete all hidden files

rm /path_to_source/.*

Change hidden files permissions

cd /
shopt -s dotglob
chmod -R 777 *

Find out the biggest files and directories (including hidden ones)

du -sm .[!.]* /home | sort -rn | head


shopt -s dotglob
du -sm /home | sort -rn | head

Check the stats for a ext4/jbd2 journal device

  • For overall timing stats use this command: cat /proc/fs/jbd2/sda1-8/info
  • To watch the journal traffic in real-time use this technique:
mount -t debugfs none /sys/kernel/debug
echo 1 > /sys/kernel/debug/tracing/events/jbd2/enable
cat /sys/kernel/debug/tracing/trace_pipe | grep stats
  • To retrieve the journal info for an unmounted filesystem use: dumpe2fs -h /dev/sda1 | grep Journal

Number of Logical CPUs

  • AIX: bindprocessor -q
  • Solaris: psrinfo | wc -l
  • Linux: nproc or cat /proc/cpuinfo | grep processor | wc -l

Enable boot.log (RHEL 5)

cd /root
patch -bp0 <rc-bootlog.txt

Spying on the console

cat /dev/vcs1
fold -w 128 /dev/vcs1

Delete open files

find /proc/*/fd -ls | grep  '(deleted)'
> "/proc/<pid>/fd/<fd>"

Save the file before that

cp /proc/<pid>/fd/<fd> /tmp/<filename>
gzip -9 /tmp/<filename>
mv /tmp/<filename>.gz /var/log/
> /proc/<pid>/fd/<fd>

Find out the inodes usage for a file system

df -i /home
for i in /home/*; do echo -n "$i "; find $i | wc -l; done | sort -k2 -rn | head