Commands by lig0n (2)

  • Scrap everything and use `gawk` to do all the magic, since it's like the future or something. gawk 'match($11, /[a-z]{3}$/) && match($9, /^ata-/) { gsub("../", ""); print $11,"\t",$9 }' Yank out only ata- lines that have a drive letter (ignore lines with partitions). Then strip ../../ and print the output. Yay awk. Be sure to see the alternatives as my initial command is listed there. This one is a revision of the original. Show Sample Output


    2
    ls -l /dev/disk/by-id |gawk 'match($11, /[a-z]{3}$/) && match($9, /^ata-/) { gsub("../", ""); print $11,"\t",$9 }' |sort
    lig0n · 2015-05-18 15:42:33 10
  • This is much easier to parse and do something else with (eg: automagically create ZFS vols) than anything else I've found. It also helps me keep track of which disks are which, for example, when I want to replace a disk, or image headers in different scenarios. Being able to match a disk to the kernels mapping of said drive the disks serial number is very helpful ls -l /dev/disk/by-id Normal `ls` command to list contents of /dev/disk/by-id grep -v "wwn-" Perform an inverse search - that is, only output non-matches to the pattern 'wwn-' egrep "[a-zA-Z]{3}$" A regex grep, looking for three letters and the end of a line (to filter out fluff) sed 's/\.\.\/\.\.\///' Utilize sed (stream editor) to remove all occurrences of "../../" sed -E 's/.*[0-9]{2}:[0-9]{2}\s//' Strip out all user and permission fluff. The -E option lets us use extended (modern) regex notation (larger control set) sed -E 's/->\ //' Strip out ascii arrows "-> " sort -k2 Sort the resulting information alphabetically, on column 2 (the disk letters) awk '{print $2,$1}' Swap the order of the columns so it's easier to read/utilize output from sed 's/\s/\t/' Replace the space between the two columns with a tab character, making the output more friendly For large ZFS pools, this made creating my vdevs immeasurably easy. By keeping track of which disks were in which slot (spreadsheet) via their serial numbers, I was able to then create my vols simply by copying and pasting the full output of the disk (not the letter) and pasting it into my command. Thereby allowing me to know exactly which disk, in which slot, was going into the vdev. Example command below. zpool create tank raidz2 -o ashift=12 ata-... ata-... ata-... ata-... ata-... ata-... Show Sample Output


    0
    ls -l /dev/disk/by-id |grep -v "wwn-" |egrep "[a-zA-Z]{3}$" |sed 's/\.\.\/\.\.\///' |sed -E 's/.*[0-9]{2}:[0-9]{2}\s//' |sed -E 's/->\ //' |sort -k2 |awk '{print $2,$1}' |sed 's/\s/\t/'
    lig0n · 2015-01-25 19:29:40 8

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

Remount root in read-write mode.
Saved my day, when my harddrive got stuck in read-only mode.

most used unix commands

Convert CSV to JSON
Replace 'csv_file.csv' with your filename.

Which processes are listening on a specific port (e.g. port 80)
swap out "80" for your port of interest. Can use port number or named ports e.g. "http"

Which processes are listening on a specific port (e.g. port 80)
swap out "80" for your port of interest. Can use port number or named ports e.g. "http"

how to export a table in .csv file
Exports the result of query in a csv file

List Threads by Pid along with Thread Start Time
This command will list all threads started by a particular pid along with the start time of each thread. This is very valuable when diagnosing thread leaks.

Create a tar archive using xz compression
Compress files or a directory to xz format. XZ has superior and faster compression than bzip2 in most cases. XZ is superior to 7zip format because it can save file permissions and other metadata data.

Which processes are listening on a specific port (e.g. port 80)
swap out "80" for your port of interest. Can use port number or named ports e.g. "http"

Create a QR code image in MECARD format
Add the QR code image on your webpage, business card ., etc, so people can scan it and quick add to their Contact Address Book. Tested on iPhone with QRreader.


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: