One Minute Linux Tricks (1)

I want to share some of the Linux command tricks that I learn from work, personal projects, friends and community in a series of blogs. These tricks helped me from time to time, to be more efficient at doing system administration works. I hope they can benefit you as well.

Switch From Vim Editor To Command Line

  • Save the file in Vim
  • Exit and troubleshooting
  • Reopen file file in Vim and continue

You can actually switch to command line and do troubleshooting without exit your vim editor. The trick here is to use CTRL+Z It will suspend the current process, and switch you back to command line. If you want to go back to the process, you just need to type fg :

# Open file for editing
$ vim test.text
Press "CTRL+Z" in Vim[2]+ Stopped vim test.text
# Troubleshooting and kill the hot process
$ kill -9 5678

[1]- Killed bash sleep.sh
# Now type fg
$ fg
You should be back in Vim

Use htop Instead of top

  • You can scroll the process list vertically and horizontally to see all process and complete command line parameters.
  • CPU/MEM/SWAP are presented in a better visual way
  • It is faster
  • Easier way of killing a process, F9 key then select the process
  • Easier way of renicing a process

Let the picture speak:

Snapshot of htop command

Recover Deleted Files With /proc File Descriptor

# I have a file open for reading in one terminal
(T1 /tmp) $ less test.txt
This is a test file, I will accidentally delete this file, but leave the file handler open,
and recover it
test.txt (END)
(T2 /tmp) $ rm test.txt(T2 /tmp) $ lsof -c less | grep test.txt
less 8411 root 4r REG 202,1 100 1450845 /tmp/test.txt (deleted)
$ ls -l /proc/8411/fd/
total 0
lrwx------ 1 root root 64 Apr 15 17:55 0 -> /dev/pts/1
lrwx------ 1 root root 64 Apr 15 17:55 1 -> /dev/pts/1
lrwx------ 1 root root 64 Apr 15 17:55 2 -> /dev/pts/1
lr-x------ 1 root root 64 Apr 15 17:55 3 -> /dev/tty
lr-x------ 1 root root 64 Apr 15 17:55 4 -> /tmp/test.txt (deleted)
$ cp /proc/8411/fd/4 /tmp/test.txt_recovered
$ cat /tmp/test.txt_recovered
This is a test file, I will accidentally delete this file, but leave the file handler open,
and recover it

Senior Cloud and DevOps Engineer from both HighTech and FinTech world.