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.
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:
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
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.
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.
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.
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.
Great stuff in here so far! Love the different shell tips. Maybe we can add some ksh stuff, something I’m trying to master
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.
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.
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.
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
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
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.