Unix general tips

Tips and reminders that apply to Unix and similar operating systems.
Unless otherwise specified, these should work in most situations. Use manpages to verify.

Notes


Package managers

Lists package managers for common distributions.

Vi editor cheat sheet

A quick cheat sheet for the vi editor

Here are a few reminders for quick reference.

Basic file operations

% ls                         // list files - compact, horizontal
% ls -l                      // list files - detail list
% ls -la                     // list all files, incl hidden, symlinks
% ls -la -t                  // sort list by last file update, descending
% ls -la -t | pbcopy         // sort list & copy to Clipboard

% ls -l . | wc -l            // Count files in a folder (wc lowercase L)
% cat <filename> | wc -l     // Count lines in a file (wc lowercase L)

% cp <source> <target>       // Copy file
% cp -R <source>/ <target>/  // Copy files, recursively
% mv <source> <target>       // Move file (rename)
% rm <filename>              // Remove file (delete)
% rmdir <folder>             // Remove empty directory
% rm -rf                     // Remove non-empty directory & children (Careful!)
% df                         // Disk free (available storage space)
% du -ah d                   // Disk used - show all files
% du -hc                     // Disk used - Folder summary

// Create a tarball (create, view files as added, compress, next value is tarball filename)
% tar -cvzf <tar_filename> <filespec>

// Take ownership of a file
% chown -R (whoami) <filename>   

Dates

  • Current date:
    % date

  • Current date as Epoch (milliseconds since 1970):
    % date +'%s'

  • Convert Epoch time to local time:
    % date -R <input value>

    If your result seems highly inaccurate, your input value is likely expressed in milliseconds.

    When this happens, try removing last three characters:

    $ date -r 1722865314420   // Wed Jun 5 11:27:00 PDT 56565 (wrong)
    $ date -r 1722865314      // Mon Aug 5 06:41:54 PDT 2024  (right)
    
  • Current month:
    % cal

Shell

  • What shell am I using?
    % echo $0

  • Which file is the shell resource file?

    • bash: ~/.bashrc (~/.profile on earlier Macs)
    • zsh: ~/.zshrc
  • Script debug info?
    -xv <scriptname> <options>

Certificates

DNS

Other resources

  • ag: The Silver Searcher: Fast alternative to grep that ignores dot files (such as .gitignore, .DS_Store, and so on), which means it’s useful for maintenance scripts and cleanup.