Unix
POSIX
POSIX is a subset of Unix, standardized by the Institute of Electrical and Electronics Engineers (IEEE). POSIX is an acronym for "Portable Operating System Interface for Unix."
Basic User Management
date
The date command lets you get the date in your terminal. It contains the -I flag which allows you to get the date in ISO8601 format. -Is to print the ISO8601 date as well as the current time up to seconds.
Examples
Print the date in ISO8601 format
Print the local date & time in ISO8601 format
Print the UTC date & time in ISO8601 format
disown
If you have a process running in the background of your terminal, you can release it. The process will continue, retaining the original PID, but it will no longer be part of your terminal session.
# disown the process with job id %2
disown %2
stat
stat() is a Unix system call that returns file attributes about an inode. The semantics of stat() vary between operating systems. As an example, Unix command ls uses this system call to retrieve information on files that includes:
atime: time of last access (ls -lu)mtime: time of last modification (ls -l)ctime: time of last status change (ls -lc)
You can use stat to get information about a file.
Get the octal permission code
Get the owner's user name
Get the owner's user ID
Get the owner's group name
Get the owner's group ID
du
The du command stands for disk usage. It can tell you information about how much space a given file on your computer is taking up.
Viewing the size of a directory
In human-readable format
In kilobytes
du -sBK ~/Desktop
du -sk ~/Desktop
In megabytes
du -sBM ~/Desktop
du -sm ~/Desktop
In gigabytes
In terabytes
You can also set the environment variable DU_BLOCK_SIZE to have a default setting for the size of blocks reported by du
lsof
The lsof command stands for list open files. On the surface, this doesn't seem useful, you'd know what files are open on your computer. However, Unix thinks of everything as a file. This includes sockets, ports, etc.
Check which program is running on a given port with the command below:
# Check for processes running on port 4000
lsof -i :4000
# Check for processes running on a range of ports, from 400 to 500
lsof -i :400-500
# Returns only the PID
lsof -ti :4000
Check for connections to a specific host
# Check for connections to a specific host
lsof -i@172.16.12.5
# Check for connections to a specific host, but only on port 22
lsof -i@172.16.12.5:22
# Check for connections to a specific host on a range of ports, from 1249 to 65535
lsof -i@172.16.12.5:1249-65535
Check which files, sockets, ports are opened by a given PID
Get all information about files opened by processes run by command ruby
Get just the process id corresponding with processes run by command ruby
Get the process id of just processes run by user tommy
mkdir
The -m flag allows you to specify the octal permissions code for the files inside the directory to be created.
head
By default, head will return the first 10 lines of the specified file. You can use the -n flag to specify the number of lines that should be returned.
Print the first 5 lines of the file
Print the first 5 lines of output
Print everything except the last 5 lines of the file
tail
By default, tail will return the last 10 lines of the specified file. You can use the -n flag to specify the number of lines that should be returned.
Print the last 5 lines of the file
Print everything except the first 5 lines of the file
Print the last 5 lines of the output
gzip
Although more people are familiar with the .zip extention written by microsoft, there also exists the GNU zip .gz extension, which has a slightly higher performance benchmark in comarison to .zip.
Compress file.txt into file.txt.gz
gzip file.txt
# => Removes file.txt and creates file.txt.gz
Decompress file.txt.gz into file.txt
Use the -k flag to --keep the original file.txt when creating the compressed file.txt.gz
gzip -k file.txt
# => Creates file.txt.gz without deleting file.txt
Use the -l flag to --list some statistics about the compression, such as the percentage smaller the compressed file is compared to the original.
Use the -f flag to --force the archive to be created, removing the pre-existing output file if it already exists.
gzip -cf file.txt
# => Deletes file.txt.gz if it already exists
dig
The Unix dig command, which stands for "Domain Information Groper" is a program used to query DNS servers for the records held by domain names.
dig @1.1.1.1 helpful.wiki A +short
# => 185.199.108.153
The @1.1.1.1 specifies the Domain Name Service provider that we want to use. In this case, it's the IP address owned by Cloudflare. If you'd like to specify a nameserver to use by default, you can edit the file /etc/resolv.conf
The helpful.wiki is the domain that we want the records for.
The +short is the flag that removes all information except the answer provided by the domain name service. Other useful flags include +trace which will show the path taken.
The A is the argument that specifies we'd like the IP address returned to us. Other valid options include NS for the nameservers used by this domain, MX for mail exchanges, and TXT for text annotations.
tar
tar is a tool used for archiving files onto tape drives, which is why the program stands for tape archive. Today it is still used to create a compressed copy of a directory. tar is used to create tarballs, a term used to denote an archive .tar file.
- The
.tar filetype denotes a tarball - The
.tgz filetype denotes a compressed tarball gzip will understand how to decompress a file with the .tgz extension, and will return the .tar version of the file after it is uncompressed.
Useful flags
| Short form | Long form | Function |
|---|
-c | --create | Create a new .tar tarball |
-r | --append | Append files to a existing tarball |
-t | --list | List the contents of a tarball |
-u | --update | Append a file to the archive, even if an existing version of that file is already in the archive |
-x | --extract | Extract the files contained within an archive |
| N/A | --delete | Delete the file from the archive |
-k | --keep-old-files | Don't replace existing files when extracting files from an archive |
-f | --file | Specify the archive file to be created, or the one to be extracted from |
-z | --gzip | Filter the archive command through gzip |
-a | --auto | Use the archive file's suffix to automatically determine which compression program to use |
-v | --verbose | Verbosely list files as they are being processed. |
-w | --interactive | Ask for confirmation for every action |
-h | --dereference | Follow any symlinks and archive the files they point to |
-C | --dir | Change to a different directory before performing any operations |
-O | --to-stdout | Extract files to standard output |
-P | --absolute | Don't strip leading slashes from file names when creating an archive |
-N | --newer | Only store files newer than DATE, which, if starting with a / or a . will be calculated in reference to the file specified as DATE. |
| N/A | --utc | Print file modification times in UTC |
| N/A | --exclude-vcs | Exclude version control systems like .git |
| N/A | --excluse-vcs-ignores | Excludes files from VCS ignore files like .gitignore |
| N/A | --overwrite | Overwrite existing files when extracting |
| N/A | --remove-files | Remove files from the disk after adding them to the archive |
| N/A | --skip-old-files | Don't replace existing files when extracting, silently skip over them |
| N/A | --xform / --transform | Use sed to replace the following expression when transforming the file names |
| N/A | --strip n | Strip n leading components from the file names on extraction |
| N/A | --quoting | Specify the style of quoting when listing directory contents, accepts shell as a valid argument |
Creating an archive
When creating an archive, the -C or --dir option is very useful. It allows you to specify which directory to be in for the subsequent operations. In the case of archiving a file, by default tar will create the archive including every directory specified in the path. If you tried to type tar -cf archive.tar ~/example then it would actually create an archive of /Users/tommy/example where the root directory is /Users, containing a single directory /tommy, which itself contained a single directory /example. To avoid this, you must specify which directory tar should operate from. In this case, it should be the parent directory of example which is /Users/tommy.
Examples
tar -f archive.tar -t
tar --file archive.tar --list
# -> example/spaced out.txt
tar --file archive.tar --list --quoting shell
By default, tar will use escapes to denote file paths, but can optionally specify file paths using literal strings, the same way sh does, by adding the --quoting shell argument to the command.
Unpack a tarball
tar --extract --verbose --file archive.tar
tar -xvf archive.tar
Unpack a compressed tarball (exactly the same)
tar --extract --verbose --file archive.tgz
tar -xvf archive.tgz
Unpack the tarball archive.tgz inside directory ~/Documents
tar -xvf archive.tgz -C ~/Documents
tar --extract --verbose --file archive.tgz --dir ~/Documents
tree
Getting Started
View the directory structure up to 4 layers deep
View only directories, not files
View only files/folders matching a wildcard pattern
View only files/folders that don't match a wildcard pattern
# 1 directory
tree -I 'node_modules'
# >1 directory
tree -I 'lib|bin|node_*'
wc
Count the number of characters in file.txt
wc -m file.txt # the number of characters (supports utf-8)
wc --chars file.txt
Count the number of words in file.txt
wc -w file.txt # the number of words
wc --words file.txt
Count the number of lines in file.txt
wc -l file.txt # number of lines
wc --lines file.txt
Count the number of words using heredoc style notation
wc -w << EOF
one
two
three
EOF
# => 3
Count the number of chars using herestring style notation
rs and datamash
awk
FS
By default, awk delimits entries by runs of whitespace. If FS contains more than one character, it will delimit the line based on every occurance of the pattern.
# "./pattern.awk"
BEGIN {
# Delimited once per comma
FS = ","
# Delimited once per tab
FS = "\t"
# Delimited once per consecutive run of tabs
FS = "\t+"
# Delimited on every comma, tab, and underline
FS = "[,\t_]"
}
Matching a pattern to a specific column in each row
# "./pattern.awk"
# If the first entry is "me"
$1 ~ /^me$/ {
print "person: " $1 " blog: fire"
}
man
Typically, a manpage for a given command is located at /usr/share/man/man1/command.1
View the location of the man page associated with printf
# Short form
man -w printf
# Long form
man --path printf
Configuring DNS nameserver resolutions
sudo vi /etc/resolv.conf
nameserver 1.1.1.1
nameserver 1.0.0.1
nameserver 2606:4700:4700::1111
nameserver 2606:4700:4700::1001
Adding a symlink for an executable file
```shell
sudo update-alternatives /usr/bin/yarn yarn /usr/bin/yarnpkg 1
```
You can also do the same for python3
```shell
sudo apt install python3
sudo apt install python3-pip
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 1
sudo update-alternatives --install /usr/bin/pip pip /usr/bin/pip3 1
```
File Systems
File System Hierarchy
Volumes & Drives
The collection of non-volatile memory stored on a computer is kept in one or more Volumes. The collection of data is the Volume, and the device this volume exists on is what's known as the Drive. Back in the day, these drives were implemented as discs, hence the term Floppy Disk Drive and Hard Disk Drive. These days, we're moving on from disc drives, instead transitioning to Solid State Drives, which use transistors and circuits to store the volume (which contains the file system).
The commands for performing various operations are different on macOS and Linux. I'll try to include an example of how to do each in the relevant section. (I ran out of time while writing this, so this is to be continued for now...)
curl
PAC Files
A Proxy Auto-Config PAC file contains a JavaScript function that decides which proxy a given network connection (URL) should use. curl does not support PAC files, but I'm putting this note here for now.
Authentication
Article: Authentication: Everything curl
curl --anyauth \
--user USERNAME:PASSWORD \
URL
cron
Example crontab using zsh globbing
SHELL=/bin/zsh
# When the computer reboots,
# delete any files in ~/tmp that haven't been accessed in over a day
@reboot rm ~/tmp/**/*(.ad+1) 2> ~/tmp/cron.log
# When the computer reboots,
# delete any directories in ~/tmp that are empty
@reboot rm -rf ~/tmp/**/*(/^F) 2> ~/tmp/cron.log
# Month, Hour, Day, Month, Weekday
# * * * * *
# @yearly
0 0 1 1 * echo 'Once a year' &> ~/log.txt
# @monthly
0 0 1 * * echo 'Once a month' &> ~/log.txt
# @weekly
0 0 * * 0 echo 'Once a week' &> ~/log.txt
# @daily
0 0 * * * echo 'Once a day' &> ~/log.txt
# @hourly
0 * * * * echo 'Once an hour' &> ~/log.txt
Edit crontab file for the user tommy
on Linux, each user's crontab file can be found at /var/spool/cron/crontabs
on macOS, each user's crontab file can be found at /usr/lib/cron/tabs
zip
less
- On Unix systems, the system-wide binary lesskey file is stored in
/usr/local/etc/sysless
Useful Flags
-S: Truncate any lines that extend beyond the length of the terminal-R: Render any ANSI escape sequences that specify SGR formatting-s: Squeeze consecutive blank lines into a single blank line
ln
Useful Flags
-s: Create a symbolic link-v: Write output to stdout when links are made-n: If the destination file exists, and is a symbolic link, don't follow it-i: Interactively prompt the user for a response before removing a file-f: Forcefully remove existing destination files
coreutils exclusive flags
-b: If there is an existing file at the destination, make a backup of it-T: Treat the destination supplied as a file, never a target directory
sudo
Useful Flags
-v: Escalate permissions during a script, so that sudo commands can be run inside a script
-n: Only run the command if you don't have to prompt for a password
-S: Read the password from stdin and write the prompt to stderr
-s: Run the shell specified by the SHELL environment variable
Ask for permission to run sudo commands in a script
#!/bin/zsh
# Ask for elevated privileges with -v
sudo -v
sudo -n echo "Success, I have root access!"
sudo -n echo "I am $(whoami)"
# Discard elevated privileges with -k
sudo -k
Logging into the root user
# Using the user `root`'s password
sudo -i
# Using the root user's password
su
Logging into the user tommy's account
Execute a sudo command using a plaintext password
sudo -S <<< "letmein" 2> /dev/null echo "No password"
Allow the user tommy to type sudo commands without having to input his password
# As the root user
<<-EOF > /etc/sudoers.d/tommy
tommy ALL = NOPASSWD: ALL
EOF
sudo chmod 644 /etc/sudoers.d/tommy
sudo chown root:wheel /etc/sudoers.d/tommy
who
diff
chsh
uname
sysctl
arch
i386
tr
visudo & vipw
These are two special commands to manage the super user and password files on your computer
/etc/localtime
The file that determines the timezone of the computer is located at /etc/localtime
To change what time it is, make a symbolic link from another timezone, and store it at this location.
motd
Disable standard message banners that appear when logging into a UNIX-like system (tested on raspbian and ubuntu)
sudo chmod -x /etc/update-motd.d/*
You can add a custom script to be run when an interactive shell is starting up by adding an executable file to /etc/update-motd.d/*
dns-sd
The dns-sd diagnostic tool is useful for network scanning. If you are trying to find users to connect to via ssh, this is a great way to find out what their multicast DNS (mDNS) name is, such as tommy.local
Browse the local network for .local domains to connect to via ssh
Lookup detailed information about Tommy's MacBook Pro
dns-sd -L "Tommy's MacBook Pro" _ssh._tcp
Look up all advertised bonjour services
dns-sd -B _services._dns-sd._udp .
Filesystem Hierarchy Standard
/bin Binary files
/etc: Host specific system configurations (config files)
/lib: Shared libraries
/sbin: System binaries
/tmp: Temporary files
/include: C/C++ header files
/lib: Object files and libraries
/libexec: Executable binaries that are only run by other commands, not by the user
/man: Manual pages
/src: Source code
/share: Architecture independent hierarchy
/mnt: Temporary mount point for a filesystem
Disk Utility Functions
Encoding / Decoding Text
You can use the uuencode and uudecode commands to encode and decode text files.
These are a couple of archiving tools I am interested in exploring later. Both of these
tools boast high compression rates, and seem useful.