Linux Pro Tip Thread

Hey everyone,

I figure we have The Small Linux Problem Thread, but that kind of encourages cool solutions or hacks to problems. We need a thread like the Windows section has, about hidden Windows 10 things that are awesome.

So, here’s one that I use every day that is SO FREAKING AWESOME.

tmux is a program that allows you have multiple terminals open at once and multiple sessions of terminals open.

I’m usually at my battlestation, hacking away in a terminal. Sometimes I leave, take my laptop into the living room or whatever, and ssh in to resume my work.

Ctrl + b and d will “detach” the session on the server wherever it’s at. On my laptop, I can ssh into my battlestation (or my server) and tmux ls to find the session (if there are multiples) then tmux attach -t session number

BOOM. If I had three shells open, one ssh’d into another server, one with Vim open, and another tailing logs, I’m back in the saddle, picking up right where I left off.

@wendell thanks for the tip. I used tmux for a while as a i3wm supplement. I had no idea this functionality existed until I watched your Linux Admin beta series.

So, everyone, what Linux Pro Tips do you have?

14 Likes

My favorite linux tip is showing people how todo a exclusive shell expansion in bash

rm -rf !(foo.txt) - Will remove everything besides foo.txt

if it doesnt work you might have to enable it

shopt -s extglob

10 Likes

One of my favorite Linux tips is that you can pipe the output of dd to a gzip compressed file:

dd if=/dev/sdc status=progress | gzip -c --best > /path/to/image.img.gz

This has the advantage of reducing the size of the image by removing the zero space so your left with only the size of whats on the disk instead of the full image.

and when you want to retrieve the image and push it back to a drive you can simply pipe it to dd:

gunzip -c /path/to/image.img.gz | dd of=/dev/sdc status=progress
8 Likes

here’s an awesome tip i used to always set up with csh on my BSD boxes, which i really should re-work for BASH if possible.

If you set up your prompt (in csh) to be something like

: user@host:/path/to/here ;

Then you can select multiple lines of commands to cut/paste - because the : begins a comment and the ; denotes end of line/end of comment.

No need to cut, paste into an editor and remove all the prompts. Just paste the lines complete and the prompt will not be included in the commands sent to the shell

4 Likes

Actually, if you split your bash prompt to multiple lines, e.g.

# user@host:/path/here$
<command line goes here>

that would work… copying and pasting blocks of text in the terminal won’t result in your prompt lines doing anything, they’ll be ignored and only the command lines would be processed.

1 Like

I started writing a port about bash keyboard shortcuts but after some time i wondered why i didn’t just search for someone who have already done it

source: https://ss64.com/bash/syntax-keyboard.html

Summary

Ctrl + a Go to the beginning of the line (Home)
Ctrl + e Go to the End of the line (End)
Ctrl + p Previous command (Up arrow)
Ctrl + n Next command (Down arrow)
Alt + b Back (left) one word
Alt + f Forward (right) one word
Ctrl + f Forward one character
Ctrl + b Backward one character
Ctrl + xx Toggle between the start of line and current cursor position
Editing:
Ctrl + L Clear the Screen, similar to the clear command
Alt + Del Delete the Word before the cursor.
Alt + d Delete the Word after the cursor.
Ctrl + d Delete character under the cursor
Ctrl + h Delete character before the cursor (Backspace)

Ctrl + w Cut the Word before the cursor to the clipboard.
Ctrl + k Cut the Line after the cursor to the clipboard.
Ctrl + u Cut/delete the Line before the cursor to the clipboard.

Alt + t Swap current word with previous
Ctrl + t Swap the last two characters before the cursor (typo).
Esc + t Swap the last two words before the cursor.

ctrl + y Paste the last thing to be cut (yank)
Alt + u UPPER capitalize every character from the cursor to the end of the current word.
Alt + l Lower the case of every character from the cursor to the end of the current word.
Alt + c Capitalize the character under the cursor and move to the end of the word.
Alt + r Cancel the changes and put back the line as it was in the history (revert).
ctrl + _ Undo

TAB Tab completion for file/directory names
For example, to move to a directory ‘sample1’; Type cd sam ; then press TAB and ENTER.
type just enough characters to uniquely identify the directory you wish to open.

Special keys: Tab, Backspace, Enter, Esc
Text Terminals send characters (bytes), not key strokes.
Special keys such as Tab, Backspace, Enter and Esc are encoded as control characters.
Control characters are not printable, they display in the terminal as ^ and are intended to have an effect on applications.

Ctrl+I = Tab
Ctrl+J = Newline
Ctrl+M = Enter
Ctrl+[ = Escape

Many terminals will also send control characters for keys in the digit row:
Ctrl+2 → ^@
Ctrl+3 → ^[ Escape
Ctrl+4 → ^
Ctrl+5 → ^]
Ctrl+6 → ^^
Ctrl+7 → ^_ Undo
Ctrl+8 → ^? Backward-delete-char

Ctrl+v tells the terminal to not interpret the following character, so Ctrl+v Ctrl-I will display a tab character,
similarly Ctrl+v ENTER will display the escape sequence for the Enter key: ^M

History:
Ctrl + r Recall the last command including the specified character(s)
searches the command history as you type.
Equivalent to : vim ~/.bash_history.
Ctrl + p Previous command in history (i.e. walk back through the command history)
Ctrl + n Next command in history (i.e. walk forward through the command history)

Ctrl + s Go back to the next most recent command.
(beware to not execute it from a terminal because this will also launch its XOFF).
Ctrl + o Execute the command found via Ctrl+r or Ctrl+s
Ctrl + g Escape from history searching mode
!! Repeat last command
!n Repeat from the last command: args n e.g. !:2 for the second argumant.
!n:m Repeat from the last command: args from n to m. e.g. !:2-3 for the second and third.
!n:$ Repeat from the last command: args n to the last argument.
!n:p Print last command starting with n
!$ Last argument of previous command
ALT + . Last argument of previous command
!* All arguments of previous command
^abc­^­def Run previous command, replacing abc with def
Process control:
Ctrl + C Interrupt/Kill whatever you are running (SIGINT)
Ctrl + l Clear the screen
Ctrl + s Stop output to the screen (for long running verbose commands)
Then use PgUp/PgDn for navigation
Ctrl + q Allow output to the screen (if previously stopped using command above)
Ctrl + D Send an EOF marker, unless disabled by an option, this will close the current shell (EXIT)
Ctrl + Z Send the signal SIGTSTP to the current task, which suspends it.
To return to it later enter fg ‘process name’ (foreground).
Emacs mode vs Vi Mode
All the above assume that bash is running in the default Emacs setting, if you prefer this can be switched to Vi shortcuts instead.

Set Vi Mode in bash:

$ set -o vi
Set Emacs Mode in bash:

$ set -o emacs

2 Likes

A thing i don’t see people mention a lot is man -k

man -k printf
Search the short descriptions and manual page names for the keyword printf as regular expression. Print
out any matches. Equivalent to apropos printf.

Source: Ubuntu man manpage

1 Like

dd has a sparse option that does the same thing. So does the cp command.

1 Like

Turning off desktop compositing in KDE is as simple as a key combination shortcut:

Shift+Alt+F12

If you’re used to turning off the compositor on startup then rebooting, this is faster.

1 Like

Also, enter “bg” (background) to continue running the process in the background.
“Ctrl+Z” then “bg” is similar to “process-name &”

3 Likes

Thanks, i didn’t know that

1 Like

Great stuff in here so far! Love the different shell tips. Maybe we can add some ksh stuff, something I’m trying to master :grin:

Wow, super handy! I always have to turn this off due to nVidia compositor (generally) being better. I’ve been using KDE over Gnome lately. Thank you!

Another one that’s useful is pgrep. pgrep with a matching string will give you the PID. There are some arguments that you can throw with it, check out the man page for more details.

Recently, I had a few processes that were stopping randomly, and we would have to ssh in and manually restart them. I made a cron to start the job every 5 minutes, but that often yielded emails of “An instance of this program is already running”. Ugh.

So, solution? */5 * * * * pgrep app_name > /dev/null || /opt/app/app_name start

Every five minutes pgrep will run, piping the output into /dev/null (minimal processing). If no PID was generated, the or statement would run, starting the application. :sunglasses:

Kind of a lazy/cheap trick, but for this application that has little documentation, it helped keep things moving while we worked with the vendor to find a lasting solution.

1 Like

Not sure how commonly LVM tags are used, but I found them handy when doing dangerous experiments on systems with multiple system partitions.

# snapshot 2 LVs (say one is root and the other is logs or something)
lvcreate --snapshot --add-tag "$(uname -r)" --add-tag "os_installed" --name "root_os_installed" vg1/root
lvcreate --snapshot --add-tag "$(uname -r)" --add-tag "os_installed" --name "logs_os_installed" vg1/var-log

# keep current kernel (CentOS)
yumdb set installonly keep kernel-"$(uname -r)"

### Catastrophic Mistakes ###

# look at lvs for the corresponding kernel version (maybe write it down)
lvs --options lv_tags @os_installed

# roll back
lvconvert --merge @os_installed

# reboot (don't forget to select the right kernel)
reboot

It’s scriptable of course:

Script
# hardware platform (ex: x86_64)
hw="$(uname --hardware-platform)"

# find target kernel via LVM tags
target_kernel=$(lvs --options lv_tags @"${rollback_tag}" |
  grep --max-count=1 ${hw} |
  sed 's/,/\n/g' |
  grep --max-count=1 ${hw})

# set target kernel as default in boot loader
target_kernel_grub_number=$(grep --perl-regexp "submenu|^menuentry" /boot/grub2/grub.cfg |
  cut --delimiter "'" -f2 |
  grep --line-number "${target_kernel}" |
  cut --delimiter ":" -f1 |
  awk '{print $1 - 1}')
  
grub2-set-default ${target_kernel_grub_number}

lvconvert --merge @"${rollback_tag}"

shutdown --reboot +1
4 Likes

That’s a nice one. Would that be a valid method of running say an rsync script only if it wasn’t still running?

2 Likes

Definitely. I think rsync comes with a status or lock file, too, so you could probably check for that. But it should work the same!

1 Like

That’s given me an idea to automate some stuff. Seems ps likes to add a space to outputs though :confused: makes numbers act like strings which is not useful.

Give you an idea where i’m going with my snippet from a larger script.


for p in $(pgrep bash); do if  [[ $(ps -p $p -o %cpu=) -gt 1000 ]] ; then kill -9 $p; else echo do nothing; fi; done;

Wonder if ps has an option to strip it, otherwise ill need to “convert” it to a number.

3 Likes

More shell expansion tips :smiley:

mkdir -p /mnt/{0..9}

Will interate over the numbers from 0-9 and make a folder for each one. It can also be more than a single digit. You can also use letters too if thats your fancy.

echo {0..10000} - echo every number from zero to ten thousand
echo {42..-5} - Counts from 42 to negative 5
echo {a..z} - print out the alphabet

4 Likes

Not sure what’s going on there, but pkill might be handy.

1 Like

a (currently non working) one liner to look for a specific command and kill it if it uses more than a certain amount of cpu usage. That’s a bit specific, but you can change that to look for other things, if you want to monitor certain processes and kill them if they reach a certain threshold for example.

1 Like