Linux Pro Tip Thread

Is the issue that %cpu isn’t an integer?

1 Like

yeah ps outputs it with a space. so it comes out as

 0.0

not

0.0

i can strip the space but i was going to look for a nicer way of doing it.

edit: I dont think bash strictly has variable “types” but a non int character seems to stop comparisons from working that to differentiate between strings and integers.

i essentially just want to check if the process is over a certain cpu threshold.

2 Likes

White space isn’t a problem for me, but the decimal is:

[ 1 -gt " 0" ]  && echo yes
yes
[ 1 -gt 0.0 ]  && echo yes
-bash: [: 0.0: integer expression expected
4 Likes

Need to quickly find out your external ip address?

dig +short myip.opendns.com @resolver1.opendns.com

3 Likes

It sometimes blows my mind that people know about ‘screen’ and ‘tmux’ but don’t know how to background shell command.

Did you realize halfway through a transfer that you didn’t run it in a multiplexer? CTLR+z to interrupt it; ‘bg’ to resume it in the background.

1 Like

This is kinda not quite the same as using screen or tmux, as you can actually disconnect your session entirely with screen or tmux, and aren’t spammed with output (if any) from the background task if you’re trying to do something else.

But yes, backgrounding tasks is useful if they aren’t too spammy…

Disconnecting a session and leaving running jobs via screen or tmux is quite useful, it’s also useful if you’re on a connection prone to disconnection due to network conditions - you can ensure your jobs keep running even if your login is disrupted.

1 Like

They wouldn’t know about kill -1 either

1 Like

Kill BF
iptables -I INPUT 1 -p tcp –dport <TUNNEL> -i <ETH> -m state –state NEW -m recent –set

iptables -I INPUT 2 -p tcp –dport <TUNNEL> -i <ETH> -m state –state NEW -m recent –update –seconds 20 – hitcount 4 -j DROP

Kill IOT
iptables -A OUTPUT -p tcp –sport <TUNNEL> -o <ETH> -m state –state RELATED,ESTABLISHED

Kill ARP poison
sudo arp -s <IP> <MAC>

1 Like

Linux has been in my job title for over 15 years, and I just gave myself a nice long kick in the ass for not knowing that. Something something “forest” and “trees”.

I parsed that “remove everything” example quite literally, but had never actually thought it through.

Great tip!

Thanks. Is one of those things that you have to google to find out. Has saved me much time and many keystrokes when doing shit like clearing out the downloads folder when i still need a couple of files in it already

4 Likes

I just had an epiphany. It would be neat if you could have a “prefix” for terminal output.

e.g, if you run bash, have # prefixed to every line of output from a command

That way, along with my prompt hack above, you could basically copy/paste entire lines, output included to repeat a command.

Sure, the output would explode if you copied it again, and again… but often you just want to copy/paste into an editor, tweak slightly and maybe re-paste.

Yes, you can get funky with shell expansion etc to avoid doing that but the dumb brute force way works for things you only want to do a few times quickly… or to cut and paste into another host.

2 Likes

The script command comes to mind.

1 Like

you mean like a custom PS1 ?

edit… nevermind that would only affect the command… not the output

1 Like

Here’s my favorate bash ‘prompt’. I often have multiple screens of output from running command line stuff. I like my prompt to stand out with colors, so that I can find where the command began:

term

Here’s my .bashrc which sets this up. I’m a bit old school, I use xterm, which has the ability to alter the title and show colors in the prompt. The $PROMPT_COMMAND var alters the title and the $PS1 alters the colors of the prompt. I also like to have the OS version in the prompt, so I can tell which container I’m currently in.

if test -t 1 ; then
  ncolors=$(tput colors)
  if test -n "$ncolors" && test $ncolors -ge 8; then
    export rl_cyan='\001\e[96m\002'
    export rl_yellow='\001\e[93m\002'
    export rl_green='\001\e[32m\002'
    export rl_reset='\001\e[0m\002'
  fi
fi
export PROMPT_COMMAND='echo -ne "\033]0;${USER}.${osver}@${HOSTNAME%%.*}:${PWD/#$HOME/\~}\007"'
export PS1="${rl_cyan}${USER}.${osver}${rl_reset}@${rl_yellow}\\h${rl_reset}:${rl_green}\\w${rl_reset}\\$ "
2 Likes

You need to execute a command on a bunch of servers and don’t wanna waste precious time?

cat host_list_or_something | xargs -I %REPL ssh -tt login@%REPL your_sexy_command

Summary

Since everybody here has a vast linux knowledge:

I used a gnu command a couple of years ago that generates personal info like name, adress, email and phonenumber to fill up a DB to fiddle around with it. It was so damn usefull, does anybody know what it was called? I couldn’t google it and im too hungover to remember it.

2 Likes

adduser ?

Yeah coloured prompts are worth it.

I used to use green for non-root and blue or red for root.

Using TMUX

2 Likes

ctrl+x ctrl-e

When you have long commend lines and you want to do some complicated edits, you can press ctrl+x ctrl-e and launch your usual editor to help you with that command.

This is useful when you have a complicated shell pipeline possibly with xargs and sed and awk, or when you have something long that you need to paste into the terminal but it needs prior edits.

Also, from you editor you can probably judge whether it’s worth making your command into a short script.

3 Likes

Yeah, tmux has really taken off lately. I still use screen.

How many of you all use tmux?

  • Use tmux
  • Use screen
  • Use emacs (and emacs only)
  • Use vim (and vim only)
  • Powershell, FTW!
  • Something else

0 voters