Command Line Hacks

Hassan Raza
9 min readJul 10, 2020

--

A command-line interface (CLI) processes commands to a computer program in the form of lines of text. The program which handles the interface is called a command-line interpreter or command-line processor. Operating systems implement a command-line interface in a shell for interactive access to operating system functions or services. (Wikipedia)

In this story, we will talk about some useful commands of Command-Line.

Everyday usage

  • In Bash, use Tab to complete arguments or list all available commands and ctrl-r to search through the command history (after pressing, type to search, press ctrl-r repeatedly to cycle through more matches, press Enter to execute the found command, or hit the right arrow to put the result in the current line to allow editing).
  • In Bash, use ctrl-w to delete the last word, and ctrl-u to delete the content from the current cursor back to the start of the line. Use alt-b and alt-f to move by word, ctrl-a to move the cursor to the beginning of the line, ctrl-e to move the cursor to end of the line, ctrl-k to kill to the end of the line, ctrl-l to clear the screen.
  • To see recent commands, use history. Follow with !n (where n is the command number) to execute again. There are also many abbreviations you can use, the most useful probably being !$ for the last argument and !! for the last command.
  • Go to your home directory cd. Access files relative to your home directory with the ~ prefix (e.g. ~/.bashrc ). In sh scripts refer to the home directory as $HOME.
  • To go back to the previous working directory: cd -.
  • If you are halfway through typing a command but change your mind, hit alt-# to add a # at the beginning and enter it as a comment (or use ctrl-a, #, enter). You can then return to it later via command history.

File Management

Nano: When working on the command line, quite often you will need to create or edit text files. nano is a simple text editor for command line when compared to others like vim, Emacs.
Nano text editor is pre-installed on macOS and most Linux distros. To check if it is installed on your system type

nano --version
// Output
GNU nano, version 4.5
(C) 1999-2011, 2013-2019 Free Software Foundation, Inc.
(C) 2014-2019 the contributors to nano
Email: nano@nano-editor.org
Web: https://nano-editor.org/
Compiled options: --enable-utf8
// Usage
nano filename

LS: Basic file management: ls and ls -l (in particular, learn what every column in ls -l means),
Chown: The chown command is used to change the owner and group of files, directories and links. By default, the owner of a filesystem object is the user that created it. The group is a set of users that share the same access permissions (i.e., read, write and execute) for that object.

Chmod: chmod is the command and system call which is used to change the access permissions of file system objects (files and directories). It is also used to change special mode flags. The request is filtered by the umask. The name is an abbreviation of change mode.

DU: du command, short for disk usage, is used to estimate file space usage. The du command can be used to track the files and directories which are consuming excessive amounts of space on the hard disk drive.

DF: df (abbreviation for disk free) is a standard Unix command used to display the amount of available disk space for file systems on which the invoking user has appropriate read access. df is typically implemented using the statfs or statvfs system calls.

Mount: On Linux and UNIX operating systems, you can use the mount command to attach (mount) file systems and removable devices such as USB flash drives at a particular mount point in the directory tree.

sudo mount /dev/sdb1 /mnt/media

FDisk: fdisk also known as format disk is a dialog-driven command in Linux used for creating and manipulating the disk partition table. It is used for the view, create, delete, change, resize, copy, and move partitions on a hard drive using the dialog-driven interface.

MKFS: In computer operating systems, mkfs is a command used to format a block storage device with a specific file system. The command is part of Unix and Unix-like operating systems.

LSBLK: lsblk lists information about all of the specified block devices. The lsblk command reads the sysfs filesystem to gather information. The command prints all block devices (except RAM disks) in a tree-like format by default. Use lsblk — help to get a list of all available columns.

Network Management

IP: Use this command to display and configure the network parameters for host interfaces
IfConfig: ifconfig in short “interface configuration” utility for system/network administration in Unix/Linux operating systems to configure, manage and query network interface parameters via command-line interface or in a system configuration scripts. The “ifconfig” command is used for displaying current network configuration information, setting up an ip address, netmask or broadcast address to a network interface, creating an alias for the network interface, setting up hardware address and enable or disable network interfaces.

Dig: Dig stands for (Domain Information Groper) is a network administration command-line tool for querying Domain Name System (DNS) name servers. It is useful for verifying and troubleshooting DNS problems and also to perform DNS lookups and displays the answers that are returned from the name server that was queried. dig is part of the BIND domain name server software suite. dig command replaces older tools such as nslookup and the host. dig tool is available in major Linux distributions.
traceroute.

Route: The route command allows you to make manual entries into the network routing tables. The route command distinguishes between routes to hosts and routes to networks by interpreting the network address of the Destination variable, which can be specified either by symbolic name or numeric address.

Processing files and data

  • To locate a file by name in the current directory, find . -iname '*something*' (or similar). To find a file anywhere by name, use locate something (but bear in mind updatedb may not have indexed recently created files).
  • For general searching through source or data files, there are several options more advanced or faster than grep -r, including (in rough order from older to newer) ack, ag ("the silver searcher"), and rg (ripgrep).
  • To convert HTML to text: lynx -dump -stdin
  • For Markdown, HTML, and all kinds of document conversion, try pandoc. For example, to convert a Markdown document to Word format: pandoc README.md --from markdown --to docx -o temp.docx
  • For Excel or CSV files, csvkit provides in2csv, csvcut, csvjoin, csvgrep, etc.
  • Know about sort and uniq, including uniq's -u and -d options -- see one-liners below. See also comm.
  • Know about cut, paste, and join to manipulate text files. Many people use cut but forget about join.
  • Know about wc to count newlines (-l), characters (-m), words (-w) and bytes (-c).
  • Know about tee to copy from stdin to a file and also to stdout, as in ls -al | tee file.txt.
  • Use shuf to shuffle or select random lines from a file.
  • Date and time: To get the current date and time in the helpful ISO 8601 format, use date -u +"%Y-%m-%dT%H:%M:%SZ" (other options are problematic). To manipulate date and time expressions, use dateadd, datediff, strptime etc. from dateutils.
  • Use zless, zmore, zcat, and zgrep to operate on compressed files.
  • Use getfacl and setfacl to save and restore file permissions. For example:
getfacl -R /some/path > permissions.txt
setfacl --restore=permissions.txt

System debugging

  • For web debugging, curl and curl -I are handy, or their wget equivalents, or the more modern httpie.
  • To know current cpu/disk status, the classic tools are top (or the better htop), iostat, and iotop. Use iostat -mxz 15 for basic CPU and detailed per-partition disk stats and performance insight.
  • For network connection details, use netstat and ss.
  • For a quick overview of what’s happening on a system, dstat is especially useful. For broadest overview with details, use glances.
  • To know memory status, run and understand the output of free and vmstat. In particular, be aware the "cached" value is memory held by the Linux kernel as file cache, so effectively counts toward the "free" value.
  • Use mtr as a better traceroute, to identify network issues.
  • For looking at why a disk is full, ncdu saves time over the usual commands like du -sh *.
  • To find which socket or process is using bandwidth, try iftop or nethogs.
  • The ab tool (comes with Apache) is helpful for quick-and-dirty checking of web server performance. For more complex load testing, try siege.
  • Know about strace and ltrace. These can be helpful if a program is failing, hanging, or crashing, and you don't know why, or if you want to get a general idea of performance. Note the profiling option (-c), and the ability to attach to a running process (-p). Use trace child option (-f) to avoid missing important calls.
  • Know about ldd to check shared libraries etc — but never run it on untrusted files.
  • Know how to connect to a running process with gdb and get its stack traces.
  • Check what OS you’re on with uname or uname -a (general Unix/kernel info) or lsb_release -a (Linux distro info).
  • Use dmesg whenever something's acting really funny (it could be hardware or driver issues).
  • If you delete a file and it doesn’t free up expected disk space as reported by du, check whether the file is in use by a process: lsof | grep deleted | grep "filename-of-my-big-file"

Obscure but useful

  • expr: perform arithmetic or boolean operations or evaluate regular expressions
  • m4: simple macro processor
  • yes: print a string a lot
  • cal: nice calendar
  • env: run a command (useful in scripts)
  • printenv: print out environment variables (useful in debugging and scripts)
  • look: find English words (or lines in a file) beginning with a string
  • cut, paste and join: data manipulation
  • fmt: format text paragraphs
  • pr: format text into pages/columns
  • fold: wrap lines of text
  • column: format text fields into aligned, fixed-width columns or tables
  • expand and unexpand: convert between tabs and spaces
  • nl: add line numbers
  • seq: print numbers
  • bc: calculator
  • factor: factor integers
  • gpg: encrypt and sign files
  • toe: table of terminfo entries
  • nc: network debugging and data transfer
  • socat: socket relay and tcp port forwarder (similar to netcat)
  • slurm: network traffic visualization
  • dd: moving data between files or devices
  • file: identify type of a file
  • tree: display directories and subdirectories as a nesting tree; like ls but recursive
  • stat: file info
  • time: execute and time a command
  • timeout: execute a command for specified amount of time and stop the process when the specified amount of time completes.
  • lockfile: create semaphore file that can only be removed by rm -f
  • logrotate: rotate, compress and mail logs.
  • watch: run a command repeatedly, showing results and/or highlighting changes
  • when-changed: runs any command you specify whenever it sees file changed. See inotifywait and entr as well.
  • tac: print files in reverse
  • comm: compare sorted files line by line
  • strings: extract text from binary files
  • tr: character translation or manipulation
  • iconv or uconv: conversion for text encodings
  • split and csplit: splitting files
  • sponge: read all input before writing it, useful for reading from then writing to the same file, e.g., grep -v something some-file | sponge some-file
  • units: unit conversions and calculations; converts furlongs per fortnight to twips per blink (see also /usr/share/units/definitions.units)
  • apg: generates random passwords
  • xz: high-ratio file compression
  • ldd: dynamic library info
  • nm: symbols from object files
  • ab or wrk: benchmarking web servers
  • strace: system call debugging
  • mtr: better traceroute for network debugging
  • cssh: visual concurrent shell
  • rsync: sync files and folders over SSH or in local file system
  • wireshark and tshark: packet capture and network debugging
  • ngrep: grep for the network layer
  • host and dig: DNS lookups
  • lsof: process file descriptor and socket info
  • dstat: useful system stats
  • glances: high level, multi-subsystem overview
  • iostat: Disk usage stats
  • mpstat: CPU usage stats
  • vmstat: Memory usage stats
  • htop: improved version of top
  • last: login history
  • w: who's logged on
  • id: user/group identity info
  • sar: historic system stats
  • iftop or nethogs: network utilization by socket or process
  • ss: socket statistics
  • dmesg: boot and system error messages
  • sysctl: view and configure Linux kernel parameters at run time
  • hdparm: SATA/ATA disk manipulation/performance
  • lsblk: list block devices: a tree view of your disks and disk partitions
  • lshw, lscpu, lspci, lsusb, dmidecode: hardware information, including CPU, BIOS, RAID, graphics, devices, etc.
  • lsmod and modinfo: List and show details of kernel modules.
  • fortune, ddate, and sl: um, well, it depends on whether you consider steam locomotives and Zippy quotations "useful".

Useful Resources

--

--

Hassan Raza
Hassan Raza

Written by Hassan Raza

Experienced Elixir Engineer | Looking for work contract.

No responses yet