linux:nice-to-know

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
linux:nice-to-know [2018/01/05 18:08] michaellinux:nice-to-know [2019/05/06 17:33] (current) michael
Line 1: Line 1:
 ====== Nice to know Stuff ====== ====== Nice to know Stuff ======
  
----- 
  
 <WRAP center round box 100%> <WRAP center round box 100%>
Line 69: Line 68:
 ---- ----
  
 +<WRAP center round box 100%>
 +''<wrap hi>**"grep" the available space from "df" output:**</wrap>''
  
 +<code># df | grep -oP '/sda1.* \K\d+(?=\s+\d+%)'</code>
  
 +<sxh plain; gutter: false; highlight: [1,4]>
 +michael@backup-server:~$ df | grep -oP '/sda1.* \K\d+(?=\s+\d+%)'
 +994425716
 +
 +michael@backup-server:~$ df
 +Filesystem                            1K-blocks        Used   Available Use% Mounted on
 +/dev/root                              15039728     1152500    13243704   9% /
 +devtmpfs                                 470184                470184   0% /dev
 +tmpfs                                    474792                474792   0% /dev/shm
 +tmpfs                                    474792        6308      468484   2% /run
 +tmpfs                                      5120                  5116   1% /run/lock
 +tmpfs                                    474792                474792   0% /sys/fs/cgroup
 +/dev/mmcblk0p1                            41853       21327       20526  51% /boot
 +tmpfs                                     94956                 94956   0% /run/user/1001
 +/dev/sda1                            1441091564   373256712   994561592  28% /mnt/backup-disk
 +</sxh>
 +
 +**Explanation:**
 +Here, we match /sda3, then as many characters as possible until we find a stretch of numbers (\d+) which is followed by one or more spaces (\s+), then one or more numbers (\d+) and a %. The foo(?=bar) construct is a positive lookahead, it allows you to search for the string foo only if it is followed by the string bar. The \K is a PCRE trick that means "discard anything matched up to this point". Combined with -o, it lets you use strings that precede your pattern to anchor your match but not print them.
 +
 +''<wrap em>Without -P, things are trickier. You would need multiple passes. For example:</wrap>''
 +
 +<code>df | grep -o '/sda3.*%' | grep -Eo '[0-9]+ *[0-9]+%' | grep -Eo '^[0-9]+'</code>
 +</WRAP>
 +
 +----
 +
 +<WRAP center round box 100%>
 +''<wrap hi>**Split text on whitespace in terminal output**</wrap>''
 +
 +To split text on whitespace you can use grep. There’s an infinite amount of ways to do this. This is one of them.
 +
 +<code># echo 'string --with ###ALLKINDS### 0f ::outputs' | grep -oP '[^\s]+'</code>
 +
 +<sxh plain; gutter: false; highlight: [1]>
 +# echo 'string --with ###ALLKINDS### 0f ::outputs' | grep -oP '[^\s]+'
 +string
 +--with
 +###ALLKINDS###
 +0f
 +::outputs
 +</sxh>
 +</WRAP>
 +
 +----
 +
 +<WRAP center round box 100%>
 +''<wrap hi>**curl only write to file if successful status 200**</wrap>''
 +
 +Make curl get the contents of a URL and write to file, but only write to file if the response is successful:
 +
 +<code># curl -s -S -f -o blackgate-feed.json "$blackgate_rz"</code>
 +
 +  * -s keeps curl quiet by hiding progress meter and error messages
 +  * -S shows an error message if it fails (stderr)
 +  * -f Fail silently (no output at all) on server errors, keeping stdout clean
 +  * -o specifies an output file
 +
 +</WRAP>
 +
 +----
 +
 +<WRAP center round box 100%>
 +''<wrap hi>**Pipe video stream from raspberry pi to local computer with ffplay**</wrap>''
 +
 +I use this to get a live video stream from my Raspberry Pi with Camera attached
 +
 +Execute this on the Pi, where TARGET_IP is my local computer where I will watch the stream, and PORT is an arbitrary port number.
 +
 +<code># raspivid -t 999999 -o - | nc -u $TARGET_IP $PORT</code>
 +
 +Execute this on the local computer where you will watch the video stream
 +
 +<code># nc -ul $PORT | ffplay -</code>
 +
 +----
 +
 +  * https://www.raspberrypi.org/blog/camera-board-available-for-sale/
 +  * https://blog.philippklaus.de/2013/06/using-the-raspberry-pi-camera-board-on-arch-linux-arm/
 +
 +</WRAP>
 +
 +----
 +
 +<WRAP center round box 100%>
 +''<wrap hi>**Generating a pseudorandom password or string in Linux bash**</wrap>''
 +
 +''Define a function in e.g. **~/.bashrc**''
 +
 +<sxh plain; gutter: false;>
 +genpasswd() {
 +  tr -dc A-Za-z0-9 < /dev/urandom | head -c ${1:-36} | xargs
 +}
 +</sxh>
 +
 +Where 36 is default length if no parameter is given
 +
 +----
 +
 +**Usage**:
 +
 +<code># genpasswd
 +GVQ3ZHqrBRDzB1QwASA9uk6YsZPto2GWeRWR
 +
 +# genpasswd 7
 +qvPWx7N</code>
 +</WRAP>
  
  • linux/nice-to-know.1515172093.txt.gz
  • Last modified: 2018/01/05 18:08
  • by michael