Commands by tyzbit (14)

  • A quick alias to check if a domain is already registered or if it's available for purchase. Show Sample Output


    5
    function canibuy { whois "$1" 2>/dev/null | grep -q 'Registrant' && echo "taken" || echo "available" }
    tyzbit · 2023-06-21 15:01:25 180
  • This command will take the output of a command and color any STDERR output as a different color (red outline in this case) Show Sample Output


    3
    ./errorscript.sh 2> >(echo "\e[0;41m$(cat)\e[0m")
    tyzbit · 2021-08-08 14:28:50 240
  • Tails a log and replaces it line-by-line according to whatever you want to replace. Useful if the file writing to the log can't be modified, so you need to modify its output instead. Show Sample Output


    1
    tail -F logfile|while read l; do sed 's/find/replace/g' <<< $l; done
    tyzbit · 2020-08-07 12:26:39 438
  • Colorify colors input by converting the text to a number and then performing modulo 7 on it. This resulting number is used as the color escape code. This can be used to color the results of commands with complex outputs (like "482279054165371") so if any of the digits change, there's a good chance the color will change too. I say good chance because there's only 7 unique colors here, so assuming you were watching random numbers, there would be a 6/7 chance that the color would change when the number changed. This should really only be used to help quickly identify when things change, but should not be the only thing relied upon to positively assert that an output has not changed. Show Sample Output


    2
    function colorify() { n=$(bc <<< "$(echo ${1}|od -An -vtu1 -w100000000|tr -d ' ') % 7"); echo -e "\e[3${n}m${1}\e[0m"; }
    tyzbit · 2020-08-06 15:17:45 394
  • Get the total RESIDENT memory used by processes of a specific name. This means this is the MINIMUM used by a process, but some memory could be paged out to swap. Show Sample Output


    0
    pids=$(pidof chrome); for p in ${pids[@]}; do cat /proc/$p/status | grep -i vmrss | awk '{print $2}'; done | while read m; do let t=$t+$m; echo $t; done | echo "$(tail -n 1) kB"
    tyzbit · 2018-04-08 16:43:35 154
  • simple jq one-liner to convert from configmaps to secrets (which require the values to be base64 encoded). To automatically pull the config map, convert it, and re-upload the corresponding secret: kubectl get --export -o json cm [configmap name] | jq 'with_entries(if .key == "data" then .value=(.value | to_entries | map( { (.key): (.value|@base64) } ) | add ) elif .key == "kind" then .value="Secret" else . end)' > secret.json; kubectl create -f secret.json Show Sample Output


    0
    cat configmap.json | jq 'with_entries(if .key == "data" then .value=(.value | to_entries | map( { (.key): (.value|@base64) } ) | add ) elif .key == "kind" then .value="Secret" else . end)'
    tyzbit · 2017-12-11 19:18:25 23
  • This is a bit of a bash hack to catch STDERR and append a log level to it. So for example, if your script has pseudo loglevels like so: echo "INFO - finding files" [ -f ${files} ] || echo "WARN - no files found" Any subcommands that write to STDERR will screw that up Adding 2> >(fb=$(dd bs=1 count=1 2>/dev/null | od -t o1 -A n); [ "$fb" ] && err=$(printf "\\${fb# }"; cat) && echo "ERROR - $err") to the command does the following: 2> Redirect STDERR >( Spawn a subshell (STDERR is then redirected to the file descriptor for this subshell) fb=$(....) get the first byte of input [ "$fb" ] test if there's a first byte && err=$(printf....) save the output to the $err variable && echo "ERROR - $err" append your pseudo loglevel and the error message Heavily borrowed from https://unix.stackexchange.com/questions/33049/check-if-pipe-is-empty-and-run-a-command-on-the-data-if-it-isnt Show Sample Output


    0
    [command] 2> >(fb=$(dd bs=1 count=1 2>/dev/null | od -t o1 -A n); [ "$fb" ] && err=$(printf "\\${fb# }"; cat) && echo "ERROR - $err")
    tyzbit · 2017-10-16 22:22:42 21
  • Ever needed to test firewalls but didn't have netcat, telnet or even FTP? Enter /dev/tcp, your new best friend. /dev/tcp/(hostname)/(port) is a bash builtin that bash can use to open connections to TCP and UDP ports. This one-liner opens a connection on a port to a server and lets you read and write to it from the terminal. How it works: First, exec sets up a redirect for /dev/tcp/$server/$port to file descriptor 5. Then, as per some excellent feedback from @flatcap, we launch a redirect from file descriptor 5 to STDOUT and send that to the background (which is what causes the PID to be printed when the commands are run), and then redirect STDIN to file descriptor 5 with the second cat. Finally, when the second cat dies (the connection is closed), we clean up the file descriptor with 'exec 5>&-'. It can be used to test FTP, HTTP, NTP, or can connect to netcat listening on a port (makes for a simple chat client!) Replace /tcp/ with /udp/ to use UDP instead.


    15
    exec 5<>/dev/tcp/time.nist.gov/13; cat <&5 & cat >&5; exec 5>&-
    tyzbit · 2015-07-30 21:12:38 25
  • Most distributions alias cp to 'cp -i', which means when you attempt to copy into a directory that already contains the file, cp will prompt to overwrite. A great default to have, but when you mean to overwrite thousands of files, you don't want to sit there hitting [y] then [enter] thousands of times. Enter the backslash. It runs the command unaliased, so as in the example, cp will happily overwrite existing files much in the way mv works. Show Sample Output


    2
    \[command]
    tyzbit · 2015-01-15 18:31:50 8
  • Use this command to watch apache access logs in real time to see what pages are getting hit. Show Sample Output


    0
    tail -f access_log | awk '{print $1 , $12}'
    tyzbit · 2014-12-24 14:15:52 11
  • This is not exhaustive but after checking /etc/cron* is a good way to see if there are any other jobs any users may have set. Note: this is a repost from a comment "flatcap" made on http://www.commandlinefu.com/commands/view/3726/print-crontab-entries-for-all-the-users-that-actually-have-a-crontab#comment, for which I am grateful and I take no credit.


    0
    for USER in /var/spool/cron/*; do echo "--- crontab for $USER ---"; cat "$USER"; done
    tyzbit · 2014-12-11 19:48:46 15
  • This checks the system load every second and if it's over a certain threshold (.8 in this example), it spits out the date, system loads and top 4 processes sorted by CPU. Additionally, the \a in the first echo creates an audible bell.


    1
    while sleep 1; do if [ $(echo "$(cat /proc/loadavg | cut -d' ' -f1) > .8 " | bc) -gt 0 ]; then echo -e "\n\a"$(date)" \e[5m"$(cat /proc/loadavg)"\e[0m"; ps aux --sort=-%cpu|head -n 5; fi; done
    tyzbit · 2014-12-08 15:44:40 8
  • Wakes up a computer on your LAN with a Wake-On-LAN packet. MAC Address must match the NIC MAC, computer must have WOL enabled in the BIOS. Show Sample Output


    0
    wakeonlan 00:00:DE:AD:BE:EF
    tyzbit · 2014-06-13 16:16:33 8
  • Use this command to watch video files on the terminal using VLC. prerequisite: VLC and cvlc sudo apt-get install vlc cvlc Show Sample Output


    0
    cvlc /path/to/file.avi -V caca
    tyzbit · 2014-06-13 16:10:36 12

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands


Check These Out

This will take the last two commands from bash_history and open your editor with the commands on separated lines

Don't like the cut command? Tired of typing awk '{print $xxx}', try this

Generate a Random MAC address
Use the following variation for FreeBSD: $ openssl rand 6 | xxd -p | sed 's/\(..\)/\1:/g; s/:$//'

Find unused IPs on a given subnet
Somewhat shorter version.

List files in directory tree with newest last

grep apache access.log and list IP's by hits and date - sorted

Exclude inserting a table from a sql import
Starting with a large MySQL dump file (*.sql) remove any lines that have inserts for the specified table. Sometimes one or two tables are very large and uneeded, eg. log tables. To exclude multiple tables you can get fancy with sed, or just run the command again on subsequently generated files.

list files recursively by size

Merge files, joining each line in one line

delay execution of a command that needs lots of memory and CPU time until the resources are available
[ 2000 -ge "$(free -m | awk '/buffers.cache:/ {print $4}')" ] returns true if less than 2000 MB of RAM are available, so adjust this number to your needs. [ $(echo "$(uptime | awk '{print $10}' | sed -e 's/,$//' -e 's/,/./') >= $(grep -c ^processor /proc/cpuinfo)" | bc) -eq 1 ] returns true if the current machine load is at least equal to the number of CPUs. If either of the tests returns true we wait 10 seconds and check again. If both tests return false, i.e. 2GB are available and machine load falls below number of CPUs, we start our command and save it's output in a text file. The ( ( ... ) & ) construct lets the command run in background even if we log out. See http://www.commandlinefu.com/commands/view/3115/ .


Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: