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

Let’s say you are working on editing a file in Vim, during the middle, one of the other process is running hot CPU and you need to switch out and kill that hot process, the typical way would be:

  • 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

htop is a newer, faster version of top command. You should use htop if your system has it. Compare to top , htop has the following advantages:

  • 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

If you accidentally deleted a file and the file is “luckily” open by a process, you can recover the deleted file by using lsof (To find the process ID) and /proc/pid/fd/{id}

# 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 Engineer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Animated routes with QGIS

Why do we use elliptic curves for cryptography?

Top 5 Atom Code Editor Packages

Top Six Cloud Myths Debunked

Cheatsheet: which ERB tag should I use?

IRONSTAR Samara — Russia 2021' Livestream | LIVE° [Fullᴴᴰ]

Flash Stock Rom on Gplus x5 pro

Flash Android phone

Access Python Tuple’s Elements

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tony

Tony

Senior Cloud Engineer

More from Medium

[Backend] Linux crontab

LINUX LOOPS

Linux: chmod awesome tips and tricks

Brace Expansion in Linux: Making Your Life Easier

Digital art by green colour