Getting status from dd when writing disk images on MacOS

dd is a pretty useful tool for creating and writing disk images from a source to a destination, for example writing disk .img files to SD Cards for your Raspberry Pi (see here, and here).

The trouble is if you’re writing images that are several GB that can run for 20mins or so, you don’t get any feedback on the progress until it’s complete. Well turns out if you send a ‘kill -INFO’ signal to the PID of the process, it will output the current status of bytes written and bytes remaining. Found this tip here.

Appending values to the end of a file from a Unix shell

Some things you do repeatedly from a *nix shell are incredibly useful and time-saving that you do them without thinking about it. Say for example you need to add a file or directory name to a file like .gitignore. On Windows you might open an editor and add the new lines to the end of the file and save it, but in a *nix shell (I imagine there’s a comparable approach maybe using something like Windows Powershell too), you can do:

echo "newfile" >> .gitignore

and you’re done.

Inline text replacement with sed

Replacing values in files is incredibly easy with sed. Here’s some examples:

 

sed 's/match/replace/g' file.txt

Find match, replace with replace, globally (all matches), in file.txt

 

sed 's/match/replace/g' file.txt > file2.txt

Same as before, but write results to new file, file2.txt

 

sed -n 's/match/replace/p' file.txt

-n suppresses output of the results, but /p prints out just the matching patterns that are replaced.

 

sed -i.old 's/match/replace/g' file.txt

Replace matches in the input file with inline replace (-i), renaming original file file.txt.old and writing inline replace results to file.txt

 

Linux shell history

Useful tips:

  • history – list history of all recorded shell history
  • history n – list last n statements
  • !n – execute nth statement from history
  • !! execute last statement