Tag Archives: CLI

CLI – find

serveimage

CLI – We all find ourselves searching our Mac looking for a particular file or files that satisfy certain criteria. Certainly, you can use Spotlight for that, but there are also methods of searching through the command line. Those options give you much better control over your search criteria. The find command is a very flexible way of searching your machine for files.

The description of find in the macOS man page is:

NAME

find — walk a file hierarchy

SYNOPSIS

find [-H | -L | -P] [-EXdsx] [-f path] path … [expression]

find [-H | -L | -P] [-EXdsx] -f path [path …] [expression]

DESCRIPTION

The find utility recursively descends the directory tree for each path listed, evaluating an expression (composed of the “primaries” and “operands” listed below) in terms of each file in the tree.

I didn’t attempt to list the switches, primaries and operands. There are nine switches and over 60 primaries. I suggest checking out the man  page for find on your macOS machine (or on the web) to see all of your options.

Screen Shot 2019-02-28 at 5.24.37 PM

One of the ways I have used find is to look for large files. In the example above, I searched a directory for all files larger than 10 megabytes. This could easily be used to search your entire disk for large files (use +1G instead of +10M to find files larger than 1 Gagabyte in length).

Screen Shot 2019-02-28 at 5.36.43 PM

You can also search for files older than (or newer than) a specific date. I have used this feature of the find command many times. In the example above I have used cd to move to a directory that contains Science Fiction images I use for one of my Desktops. I am searching that directory looking for files that were modified (in my use case this means added) more than 10 days ago.

Those ae just two simple examples of using the find command. There are many other examples of how find can be used in the references listed at the end of this article. Because of the flixibility of the find command, it can be put to use for many tasks by the SysAdmin as well as the macOS/Linux/UNIX user.

To learn a lot more about how to use find, take a look at “Finding Files On The Command Line“. Yes, this is a Linux article, but the find command as implemented on macOS is fundamentally the same.

Check the macOS man page for cd for all of the available options.

References

  1. 25 simple examples of Linux find command
  2. 14 Practical Examples of Linux Find Command for Beginners
  3. Finding Files On The Command Line

See my other CLI and macOS articles


Weather at Your Terminal Command Line

screen shot 2019-01-18 at 6.17.34 pm

Web Tools – There are lots of way of getting the weather on your computer. Most of those will open in your browser with lots of content, animations, and probably advertisements. But wttr.in is different. It is text only. What is wttr.in? From their website:

wttr.in is a console-oriented weather forecast service that supports various information representation methods like terminal-oriented ANSI-sequences for console HTTP clients (curl, httpie, or wget), HTML for web browsers, or PNG for graphical viewers

screen shot 2019-01-18 at 6.11.46 pm

I used curl wttr.in from the terminal command line to get the image shown at the top of this page. This should work on any Linux or macOS system. When I enter http://wttr.in into my Safari browser I get similar output as shown in the image immediately above.

This site provides many options to choose from in their weather report. While it takes your current location as the default, other locations around the world can be specified. Queries from the US appear in USCS units, while most of the rest of the world get values in the Metric System. Optionally you can specify the units.

Reports can be returned in ANSI for the terminal, HTML for the browser, or as a .PNG image. All of the options are explained on their GitHub site.

I like this minimalist weather report and have added the URL to my Safari Favorites.

CLI – dd

serveimage

CLI – If you are wanting to copy a file from one place to another then you may want to look at the dd command. This command is particulary useful when you want to copy the file while making edits to it on the fly.

The command is also useful when copying a disk image to other media. This can be very useful in creating a bootable SD card for a Raspberry Pi. The Wikipedia page on the dd command gives a good overview of how the command can be used.

The description of dd in the macOS man page is:

The dd utility copies the standard input to the standard output.  Input data is read and written in 512-byte blocks.  If input reads are short, input from multiple reads are aggregated to form the output block.  When finished, dd displays the number of complete and partial input and output blocks and truncated input records to the standard error output.

To learn a lot more about how to use dd, take a look at “Linux dd command explained for beginners (8 examples)“. Yes, this is a Linux article, but the dd command as implemented on macOS is fundamentally the same. Check the macOS man page for dd for all of the available options.


See my other CLI and macOS articles


 

CLI – time

serveimage

CLI – Have you ever run a command from the command line and afterwards wondered how long it took to run? If so, the time command will give you that answer.

This command measures the real, user, and system time used by a command during its execution.

The description of time in the macOS man page is:

The time utility executes and times utility. After the utility finishes,
time writes the total time elapsed, the time consumed by system overhead,
and the time used to execute utility to the standard error stream. Times
are reported in seconds..

As an example I ran a three ping against the site capmac.org and timed it.

Screen Shot 2019-01-02 at 9.45.13 AM

To learn a lot more about how to use time, take a look at “Linux time Command Tutorial for Beginners (with Examples)“. Yes, this is a Linux article, but the time command as implemented on macOS is fundamentally the same. Check the macOS man page for time for all of the available options.


See my other CLI and macOS articles


 

CLI – file

serveimage

CLI – If you encounter a file and are not sure what type of file it is (often because the extension is missing), the the file command is what you are looking for.

This command allows you to lets you see the type of file you’re dealing with. The description of file in the macOS man page is:

file tests each argument in an attempt to classify it.  There are three sets of tests, performed in this order: filesystem tests, magic tests, and language tests.  The first test that succeeds causes the file type to be printed.

The type printed will usually contain one of the words text (the file contains only printing characters and a few common control characters and is probably safe to read on an ASCII terminal), executable (the file contains the result of compiling a program), or data meaning anything else (data is usually “binary” or non-printable).  

To learn a lot more about how to use file, take a look at “Linux file Command Tutorial for Beginners (5 Examples)“. Yes, this is a Linux article, but the file command as implemented on macOS is fundamentally the same. Check the macOS man page for file for all of the available options.


See my other CLI and macOS articles


 

CLI – whatis

serveimage

CLI – As mentioned before, every command line command has a descriptive man page entry that you can access. That is often a very long and detailed description of the command and all of the possible options available to it.

Sometimes you just want a quick explanation as to what a command does. This is where the whatis command is useful. It will return a one line description of the command being referenced.

The description of whatis in the macOS man page is:

whatis searches a set of database files containing short descriptions of system commands for keywords and displays the result on the standard output.  Only complete word matches are displayed.

To learn a lot more about how to use whatis, take a look at “Linux whatis Command Tutorial for Beginners (5 Examples)“. Yes, this is a Linux article, but the whatis command as implemented on macOS is fundamentally the same. Check the macOS man page for whatis for all of the available options.


See my other CLI and macOS articles


 

CLI – tcpdump

serveimage

CLI – If you are having network connectivity issues tcpdump is a utility that you may want to try. It is also handy when you want to examine unusual network traffic.

This command allows you to capture and analyze network traffic. Using this command is probably going to require you to put on your ‘propeller hat’ and drop further down ‘into the weeds’ of tech than you are typically used to. It is good to know that the ability is there if you need it.

The description of tcpdump in the macOS man page is:

Tcpdump prints out a description of the contents of packets on a network interface that match the boolean expression; the description is preceded by a time stamp, printed, by default, as hours, minutes,  seconds, and fractions of a second since midnight.  It can also be run with the -w flag, which causes it to save the packet data to a file for later analysis, and/or with the -r flag, which causes it to read from a saved packet file rather than to read packets from a network interface. It can also be run with the -V flag, which causes it to read a list of saved packet files. In all cases, only packets that match expression will be processed by tcpdump.

To learn a lot more about how to use tcpdump, take a look at “An introduction to using tcpdump at the Linux command line“. Yes, this is a Linux article, but the tcpdump command as implemented on macOS is fundamentally the same. Check the macOS man page for tcpdump for all of the available options.


See my other CLI and macOS articles


CLI – bzip2

CLI – If you have a file you want to compress, you have options as to how you will do it. The bzip2 command is one of the options that is open to you on a macOS system. Likewise, if you come across a previously compressed file with the .bz2 extension, you will want to use bzip2 to decompress it.

The description of bzip2 in the macOS man page is:

bzip2  compresses  files  using  the Burrows-Wheeler block sorting text compression algorithm, and Huffman coding. Compression is generally considerably better than   that achieved by more conventional LZ77/LZ78-based compressors, and approaches the performance of the PPM family of statistical compressors.

To learn a lot more about how to use bzip2, take a look at “Linux bzip2 Command Tutorial for Beginners (6 Examples)“. Yes, this is a Linux article, but the bzip2 command as implemented on macOS is fundamentally the same. Check the macOS man page for bzip2 for all of the available options.


See my other CLI and macOS articles


 

CLI – sed

Screen Shot 2018-08-06 at 7.16.16 PM.png

CLI – Sometimes you have a file that you wish to scan for every instance of a phrase and replace it with another phrase. The sed or stream editor command is just what you are looking for. In the above screen shot I first display a file with the cat command, then use se to replace all appearances of the word ‘Emma’ with the word ‘Bambi’. The sed command syntax for that is:

sed  ‘s/Emma/Bambi/g’ Review_Four\ Dominions\ copy.txt

The ‘s/Emma/Bambi/g’ says to do a global search and replace of Bambi for every instance of Emma. The name of the file being operated on is ‘Review_Four\ Dominions\ copy.txt’.

The description of sed in the macOS man page is:

The sed utility reads the specified files, or the standard input if no files are specified, modifying the input as specified by a list of commands.  The input is then written to the standard output.

A single command may be specified as the first argument to sed.  Multiple commands may be specified by using the -e or -f options.  All commands are applied to the input in the order they are specified regardless of  their origin.

This can be a very powerful and useful command when processing text files. In the SysAdmin world it can be very useful for updating configuration files.

To learn a lot more about how to use sed, take a look at “‘Sed’ Command In Linux: Useful Applications Explained“. Yes, this is a Linux article, but the sed command as implemented on macOS is fundamentally the same.


See my other CLI and macOS articles


 

CLI – zcat

Screen Shot 2018-07-24 at 11.06.57 AM

CLI – Sometimes you want to view the content of a compressed file. The zcat  Command Line  command allows you to do that. What does zcat do:

zcat uncompresses either a list of files on the command line or its standard
input and writes the uncompressed data on standard output.

The zcat command is like cat, but for compressed files.

To learn a lot more about how to use zcat, take a look at “Linux zcat Command Tutorial for Beginners (5 Examples)“. Yes, this is a Linux article, but the zcat command as implemented on macOS is fundamentally the same.


See my other CLI and macOS articles