Command-Line Tricks You'll Fall-in-Love With
Interacting with a computer through a command-line interface (CLI) is a powerful technique as it greatly improves productivity during development. In-fact, mastering the command-line is a must for every programmer or data scientist. I have been using CLI since my early programming days primarily for installing libraries or executing python commands. But as I learn newer commands, especially outside the programming realm, I'm falling in love with CLI more than ever. Below is a curated list of my FIVE favorite commands I'm sure you all with love too:
1. VIDEO DOWNLOAD
Download video+audio either single files or entire playlists. youtube-dl is a command-line program that allows you to download videos from YouTube and other sites such as Vimeo. It requires the Python interpreter, version 2.6, 2.7, or 3.2+, and it is not platform-specific, and works on your Unix box, on Windows or on macOS.
Installation:
You can download youtube-dl and copy it to /usr/local/bin/. It is released to the public domain on github, which means you can modify it, redistribute it or use it however you like.
sudo curl -L https://yt-dl.org/downloads/latest/youtube-dl -o /usr/local/bin/youtube-dl sudo chmod a+rx /usr/local/bin/youtube-dl
Alternatively, you can also use pip:
sudo -H pip install --upgrade youtube-dl
Syntax:
youtube-dl [OPTIONS] URL [URL...] Options -F, --list-formats List all available formats. -f, --format FORMAT Video format code, see example below. -i, --ignore-errors Continue on download errors, for example to skip unavailable videos in a playlist. --abort-on-error Cancel downloading of further videos (in the playlist or the command line) if an error occurs. --no-playlist Download only the video, if the URL refers to a video and a playlist. --yes-playlist Download the playlist, if the URL refers to a video and a playlist. -a, --batch-file FILE File containing URLs to download ('-' for stdin.) --restrict-filenames Restrict filenames to only ASCII characters, and avoid "&" and spaces in filenames. -w, --no-overwrites Do not overwrite files. --write-description Write video description to a .description file. --write-info-json Write video metadata to a .info.json file. --write-annotations Write video annotations to a .annotations.xml file. -q, --quiet Activate quiet mode. --no-mtime Set the file last modified date/time to the download date/time. --no-warnings Ignore warnings. --console-title Display progress in console titlebar. --sleep-interval SECONDS Number of seconds to sleep before each download. -h, --help Print this help text and exit. -U, --update Update this program to latest version. Make sure that you have sufficient permissions (run with sudo if needed.) --version Print program version and exit.
Usage:
First list the available formats with -F
$ youtube-dl -F https://www.youtube.com/watch?v=VG1VVFfOnYQ 140 m4a audio only DASH audio 127k , m4a_dash container, aac @128k (44100Hz), 3.77MiB 141 m4a audio only DASH audio 255k , m4a_dash container, aac @256k (44100Hz), 7.57MiB *** 160 mp4 256x144 DASH video 113k , 12fps, video only, 3.24MiB 133 mp4 426x240 DASH video 269k , 24fps, video only, 7.27MiB 134 mp4 640x360 DASH video 272k , 24fps, video only, 6.55MiB 244 webm 854x480 DASH video 504k , 24fps, video only, 7.95MiB 135 mp4 854x480 DASH video 540k , 24fps, video only, 13.33MiB 136 mp4 1280x720 DASH video 1155k , 24fps, video only, 26.62MiB 248 webm 1920x1080 DASH video 1797k , 24fps, video only, 30.81MiB 137 mp4 1920x1080 DASH video 2750k , 24fps, video only, 57.97MiB *** 43 webm 640x360 18 mp4 640x360 22 mp4 1280x720 (best)
Using the codes in the very first column, you can select a specific size of Video and Audio.
For example, let's download 1920x1080 resolution video (137) plus 255K audio (141) so the command becomes:
youtube-dl -f 137+141 https://www.youtube.com/watch?v=VG1VVFfOnYQ
If you just want the highest possible quality, this is the default so all you need is:
youtube-dl https://www.youtube.com/watch?v=VG1VVFfOnYQ
By default the date/time of the downloaded file will be equal to the upload date/time, to disable this and return the downloaded date use the option --no-mtime.
2. STAY AWAKE
Prevent the system from sleeping on behalf of a utility.
Syntax:
caffeinate [-disu] [-t timeout] [-w pid] [utility arguments...] Key -d Create an assertion to prevent the display from sleeping. -i Create an assertion to prevent the system from idle sleeping. -m Create an assertion to prevent the disk from idle sleeping. -s Create an assertion to prevent the system from sleeping. This assertion is valid only when system is running on AC power. -u Create an assertion to declare that user is active. If the display is off, this option turns the display on and prevents the display from going into idle sleep. If a timeout is not specified with '-t' option, then this assertion is taken with a default of 5 second timeout. -t Specifies the timeout value in seconds for which this assertion has to be valid. The assertion is dropped after the specified timeout. Timeout value is not used when an utility is invoked with this command. -w Waits for the process with the specified pid to exit. Once the the process exits, the assertion is also released. This option is ignored when used with utility option.
Usage:
Prevent sleep for 1 hour (3600 seconds)
caffeinate -u -t 3600
Make caffeinate fork a process, exec "make" in it, and hold an assertion that prevents idle sleep as long as that process is running:
caffeinate -i make
3. SCREENSHOT
Capture an image of the whole, or part of the screen.
Syntax:
screencapture [options] [file] Key -c Force screen capture to go to the clipboard. -C Capture the cursor as well as the screen. Only allowed in non-interactive modes. -d Display errors to the user graphically. -i Capture screen interactively, by selection or window. The control key = copy screen to the clipboard. The space key will toggle between mouse selection and window selection modes. The escape key will cancel the screen shot. -m Only capture the main monitor, undefined if -i is set. -M Open the taken picture in a new Mail message. -o In window capture mode, do not capture the shadow of the window. -P Open the taken picture in a Preview window. -s Only allow mouse selection mode. -S In window capture mode, capture the screen instead of the window. -t format Image format to create, default is png (other options include pdf, jpg, tiff and others). -T seconds Take the picture after a delay of seconds, default=5 Handy for arranging windows/menus before taking the screenshot. -w Only allow window selection mode. -W Start interaction in window selection mode. -x Do not play sounds. -l windowid Capture a specific windowsid. -R x,y,w,h Capture a screen rectangle, top,left,width,height. file Where to save the screen capture, 1 file per screen. -help Display brief syntax summary.
Usage:
Use the command below to take a screen shot of the select area. The mouse will turn into crosshairs, hit the space bar for camera mode, now click the window, a file called "example.png" will appear on your desktop.
screencapture -i ~/Desktop/example.png
4. TEXT TO SPEECH
Convert text to audible speech.
This tool uses the Speech Synthesis manager to convert input text to audible speech and either play it through the sound output device chosen in System Preferences or save it to an AIFF file.
Syntax:
say [-v voice] [-o out.aiff | -n name:port ] [-f file.in | string ...] Key string The text to speak on the command line. This can consist of multiple arguments, which are considered to be separated by spaces. --input-file=file -f file A file to be spoken. If file is - or neither this parameter nor a message is specified, read from standard input. --file-format=format The format of the file to write (AIFF, caff, m4af, WAVE). Generally, it's easier to specify a suitable file extension for the output file. To obtain a list of writable file formats, specify '?' as the format name. --data-format=format The format of the audio data to be stored. default=linear PCM. --progress Display a progress meter during synthesis. --rate=rate -r rate Speech rate to be used, in words per minute. --voice=voice -v voice The voice to be used: English language = Alex, Daniel, Fiona, Fred, Samantha or Victoria Default is the voice selected in System Preferences | Speech Other voices are available for foreign languages. --voice=? List all available voices. --output-file=fileout.aiff -o fileout.aiff An AIFF file to be written, some voices support other file formats. --channels=channels The number of channels. Most synthesizers produce mono audio only. --bit-rate=rate The bit rate for formats, default=AAC.specify '?' as the rate. --quality=quality The audio converter quality level between 0 (lowest) and 127 (highest). --network-send=name -n name --network-send=name:port -n name:port --network-send=:port -n :port --network-send=: -n : Specify a service name (default "AUNetSend") and/or IP port to be used for redirecting the speech output through AUNetSend. specify '?' as the device name to obtain a list of audio output devices.
Usage:
say -f myfile.txt cat myfile.txt | say?
5. SYSTEM SETUP
Configure certain per-machine settings typically configured in the System Preferences application.
Syntax:
sudo systemsetup
The systemsetup command requires at least "admin" privileges to run.
Usage:
sudo systemsetup -getcomputername sudo systemsetup -setdate 12:31:17 Key: [-getdate] [-setdate mm:dd:yy] [-gettime] [-settime hh:mm:ss] [-gettimezone] [-listtimezones] [-settimezone timezone] [-getusingnetworktime] [-setusingnetworktime on | off] [-getnetworktimeserver] [-setnetworktimeserver timeserver] [-getsleep] [-setsleep minutes] [-getcomputersleep] [-setcomputersleep minutes] [-getdisplaysleep] [-setdisplaysleep minutes] [-getharddisksleep] [-setharddisksleep minutes] [-getwakeonmodem] [-setwakeonmodem on | off] [-getwakeonnetworkaccess] [-setwakeonnetworkaccess on | off] [-getrestartpowerfailure] [-setrestartpowerfailure on | off] [-getrestartfreeze] [-setrestartfreeze on | off] [-getallowpowerbuttontosleepcomputer] [-setallowpowerbuttontosleepcomputer on | off] [-getremotelogin] [-setremotelogin on | off] [-getremoteappleevents] [-setremoteappleevents on | off] [-getcomputername] [-setcomputername computername] [-getstartupdisk] [-liststartupdisks] [-setstartupdisk path] [-getwaitforstartupafterpowerfailure] [-setwaitforstartupafterpowerfailure value] [-getdisablekeyboardwhenenclosurelockisengaged] [-setdisablekeyboardwhenenclosurelockisengaged yes | no] [-getkernelbootarchitecturesetting] [-setkernelbootarchitecture i386 | x86_64 | default] [-version] [-help] [-printCommands]