Command Line Hacks
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, uselocate something
(but bear in mindupdatedb
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"), andrg
(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
anduniq
, including uniq's-u
and-d
options -- see one-liners below. See alsocomm
. - Know about
cut
,paste
, andjoin
to manipulate text files. Many people usecut
but forget aboutjoin
. - 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 inls -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, usedateadd
,datediff
,strptime
etc. fromdateutils
. - Use
zless
,zmore
,zcat
, andzgrep
to operate on compressed files. - Use
getfacl
andsetfacl
to save and restore file permissions. For example:
getfacl -R /some/path > permissions.txt
setfacl --restore=permissions.txt
System debugging
- For web debugging,
curl
andcurl -I
are handy, or theirwget
equivalents, or the more modernhttpie
. - To know current cpu/disk status, the classic tools are
top
(or the betterhtop
),iostat
, andiotop
. Useiostat -mxz 15
for basic CPU and detailed per-partition disk stats and performance insight. - For network connection details, use
netstat
andss
. - For a quick overview of what’s happening on a system,
dstat
is especially useful. For broadest overview with details, useglances
. - To know memory status, run and understand the output of
free
andvmstat
. 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 likedu -sh *
. - To find which socket or process is using bandwidth, try
iftop
ornethogs
. - The
ab
tool (comes with Apache) is helpful for quick-and-dirty checking of web server performance. For more complex load testing, trysiege
. - Know about
strace
andltrace
. 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
oruname -a
(general Unix/kernel info) orlsb_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 expressionsm4
: simple macro processoryes
: print a string a lotcal
: nice calendarenv
: 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 stringcut
,paste
andjoin
: data manipulationfmt
: format text paragraphspr
: format text into pages/columnsfold
: wrap lines of textcolumn
: format text fields into aligned, fixed-width columns or tablesexpand
andunexpand
: convert between tabs and spacesnl
: add line numbersseq
: print numbersbc
: calculatorfactor
: factor integersgpg
: encrypt and sign filestoe
: table of terminfo entriesnc
: network debugging and data transfersocat
: socket relay and tcp port forwarder (similar tonetcat
)slurm
: network traffic visualizationdd
: moving data between files or devicesfile
: identify type of a filetree
: display directories and subdirectories as a nesting tree; likels
but recursivestat
: file infotime
: execute and time a commandtimeout
: 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 byrm -f
logrotate
: rotate, compress and mail logs.watch
: run a command repeatedly, showing results and/or highlighting changeswhen-changed
: runs any command you specify whenever it sees file changed. Seeinotifywait
andentr
as well.tac
: print files in reversecomm
: compare sorted files line by linestrings
: extract text from binary filestr
: character translation or manipulationiconv
oruconv
: conversion for text encodingssplit
andcsplit
: splitting filessponge
: 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 passwordsxz
: high-ratio file compressionldd
: dynamic library infonm
: symbols from object filesab
orwrk
: benchmarking web serversstrace
: system call debuggingmtr
: better traceroute for network debuggingcssh
: visual concurrent shellrsync
: sync files and folders over SSH or in local file systemwireshark
andtshark
: packet capture and network debuggingngrep
: grep for the network layerhost
anddig
: DNS lookupslsof
: process file descriptor and socket infodstat
: useful system statsglances
: high level, multi-subsystem overviewiostat
: Disk usage statsmpstat
: CPU usage statsvmstat
: Memory usage statshtop
: improved version of toplast
: login historyw
: who's logged onid
: user/group identity infosar
: historic system statsiftop
ornethogs
: network utilization by socket or processss
: socket statisticsdmesg
: boot and system error messagessysctl
: view and configure Linux kernel parameters at run timehdparm
: SATA/ATA disk manipulation/performancelsblk
: list block devices: a tree view of your disks and disk partitionslshw
,lscpu
,lspci
,lsusb
,dmidecode
: hardware information, including CPU, BIOS, RAID, graphics, devices, etc.lsmod
andmodinfo
: List and show details of kernel modules.fortune
,ddate
, andsl
: um, well, it depends on whether you consider steam locomotives and Zippy quotations "useful".
Useful Resources
- awesome-shell: A curated list of shell tools and resources.
- awesome-osx-command-line: A more in-depth guide for the macOS command line.
- Strict mode for writing better shell scripts.
- shellcheck: A shell script static analysis tool. Essentially, lint for bash/sh/zsh.
- Filenames and Pathnames in Shell: The sadly complex minutiae on how to handle filenames correctly in shell scripts.
- Data Science at the Command Line: More commands and tools helpful for doing data science, from the book of the same name