Command-line utilities

A collection of notes on commands is listed here. As I moved forward in my engineering career, these served well for getting things done.

man - manual

A lot of this can be found in man - short for manual - which is also another commandline tool.

time based utils

date

date
date --date "40 days"
date --date "40 days ago"

cal - calendar

cal
cal 3
cal 7 1977 # Shows you calendar of July 1977

ntpd

Force date/time sync with OS

sudo ntpd

text based utils

> - redirect

Redirect output to file.

Command Description
> redirection + overwrite.
>> redirection + append to file.

Both creates new files, if the target does not exist.

| - pipe

Pipe - pipes output to next command.

cat README.md | grep "test" # show contents of file, use it to search for text

&1 &2

File descriptor 1 is stdout and 2 is stderr.

2>&1 means redirect stderr to stdout

2>1 means redirect stderr to a file called “1”, so the & is a way of telling that whatever that follows is the file descriptor. & is only interpreted to mean “file descriptor” in the context of redirections, so it shouldn’t be &2>&1 - this would be interpreted as command & and 2>&1

wc - word count

wc -l  # word count lines of a file
wc -l /etc/system-release/ # returns 1

$

Get previous commands

Command Description
$-1 whole arg of command
$! last arg of command
$? exit status from previous command execution

An example of retrieving error codes from previous executions:

grep x1y2z3 somefile.txt
echo $?
# returns 0 if no error

file system utils

du - disk usage

File system usage

du -ck * | sort -rn | head -11

mount

$ sudo mount -a to mount all the things

fstab

The configuration file /etc/fstab contains the necessary information to automate the process of mounting partitions.

lsblk

Show all information about your disk.

sudo lsblk -o name,mountpoint,label,size,uuid

ls

Lists things.

ls -l $(tty)

Find the location of the code from a running process. To find the path to the source code of a running process, in root, try this:

ls -l /proc/8505/exe

Or if you don’t want to parse the output of ls, just do readlink

readlink /proc/8505/exe  # or
realpath /proc/8505/exe

find

Count how many files are in a directory

find <target_dir> -type f | wc -l
find cache -type f | wc -l # example
find - # searches for files in a directory hierarchy

chmod

chmod allows you to change access to the files xyz x is for root, y is for group, z is for user, all integers. 7 is maximum giving all access.

chmod xyz fileName

All files basically point to the contents’ inode.

Creating a symlink means creating another reference to the same inode. If the inode/file is removed, the symlinks do not dissappear, but depending on soft or hard, the symlinks may point to non-existent content or a copy of the content respectively. Hard symlinks can only be created in the same file system, whereas the soft one can cross different file systems.

ln -s <target> <link_file>

ln <target> <link_file>

compression

tar

compressing with tar and zipping files or directories

$ tar -czvf res00.tar.gz results

zip

Compress everything in a folder:

for file in _; do zip -r ${file%._}.zip ${file}; done

packages

rpm

Similar to apm, it is a package manager for Red Hat. Resource manager is yum. rpm itself is a collection of 4 sections:

  1. lead (file format to determine rpm),
  2. signature (who issued it),
  3. header (metadata),
  4. payload.
Terminologies Description
noarch.rpm means that the package is not dependent on a certain computer architecture.
SPEC recipe for creating an RPM package
SRPM rpm that is pre-compiled and ready for installation

Query

Instead of specifying the package name, you can use the following options with -q to specify the package(s) you want to query.

options description
-a queries all currently installed packages.
-f <file> queries the package which owns <file>. When specifying a file, you must specify the full path of the file (for example, /usr/bin/ls).
-p <packagefile> queries the package <packagefile>.

There are a number of ways to specify what information to display about queried packages. The following options are used to select the type of information for which you are searching.

These are called Information Selection Options.

options description
-i displays package information including name, description, release, size, build date, install date, vendor, and other miscellaneous information.
-l displays the list of files that the package contains.
-s displays the state of all the files in the package.
-d displays a list of files marked as documentation (man pages, info pages, READMEs, etc.).
-c displays a list of files marked as configuration files. These are the files you change after installation to adapt the package to your system (for example, sendmail.cf, passwd, inittab, etc.)

rpm2cpio and cpio

This command is useful to see how the RPM will be installed on the machine. If, for example, one wanted to know how logrotate will create its directories, simply type:

rpm2cpio logrotate-1.0-1.i386.rpm | cpio -t

Getting directories and files that will be installed by rpm with cpio

rpm2cpio bind.rpm | cpio -idmv

tty

Tells you where your pseudo terminal attached to.

yum

yum whatprovides <> gives you which RPM installed that file.

issues

In order to resolve Yum issues, make sure you have updated yum. You can do this by: sudo yum clean all, and then sudo yum update

yum.repos.d

Additionally all your yum information is in /etc/yum.repos.d

If you want to change what you can install, you can enable installs from all the files in the yum.repos.d

test

Man test

processes

strace

Use to find information about programs being executed

strace -eexecve -f make test

The third variable is the environment variables

The option -c is useful to get time and statistics

pstree

pstree shows running processes as a tree. The tree is rooted at either pid or init if pid is omitted. If a user name is specified, all process trees rooted at processes owned by that user are shown.

kill

Kill something with ease

kill -9 $(ps aux | grep python | grep server | cut -d ' ' -f 7) && systemctl start cosmos-status && curl localhost:8080/summarise

network and communications

cURL

httPIE

wget

ssh

socat

socat is a command line based utility that establishes two bidirectional byte streams and transfers data between them. Because the streams can be constructed from a large set of different types of data sinks and sources (see address types), and because lots of address options may be applied to the streams, socat can be used for many different purposes.

Eavesdropping on Unix Sockets

  1. mv collectd-unixsock collectd-unixsock.original
  2. use socat like so: socat -t100 -x -v UNIX-LISTEN:collectd-unixsock,mode=777,reuseaddr,fork UNIX-CONNECT:collectd-unixsock.original

simplified versions of tools

Most of it is written in Rust. All these tools are better, faster and more sensible in some ways. All of them can be installed via brew on Mac.

rg (grep)

Tool Command Line count Time
ripgrep rg -w 'Sherlock [A-Z]\w+' 5268 2.108s
GNU grep LC_ALL=C egrep -w 'Sherlock [A-Z]\w+' 5268 7.014s

sd (sed awk)

More intuitive!

Tool Command File size Time
sed sed -E "s/\"/'/g" *.json >/dev/null 1.5GB 2.358
sed sed "s/\"/'/g" *.json >/dev/null 1.5GB 2.378
sd sd "\"" "'" *.json >/dev/null 1.5GB 1.007

fd (file)

Faster!

Tool Command Time
find find ~ -iregex '.*[0-9]\.jpg$' 7.385 s
fd fd -HI '.*[0-9]\.jpg$' ~ 870.7 ms
# Convert all jpg files to png files:
fd -e jpg -x convert {} {.}.png

# Unpack all zip files (if no placeholder is given, the path is appended):
fd -e zip -x unzip

# Convert all flac files into opus files:
fd -e flac -x ffmpeg -i {} -c:a libopus {.}.opus

# Count the number of lines in Rust files (the command template can be terminated with ';'):
fd -x wc -l \; -e rs

Bash

Handy to use in command line also.

set

If you are writing a script, set some options. -eu is for failing fast - read more here.

For example:

#! /bin/bash
set -eu

...

errors

Remember to send errors to std.err rather than std.out. This is standard practice for script writing.

Example: getting file line by line

while read line ; do echo $line ; done < poolDNS.text