Ubuntu Cheat Sheet

Frequently Used Installation & Configuration Commands.

Introduction

There are things that I always forget how to do, but I use them a lot. This is a my cheat sheet on those useful commands. It's mainly related to installation and configuration on Ubuntu machines.


Commands For Displaying, Formatting, & Sending Text, Directory, & File Info

copy
# print path with escaped spaces
printf "%q\n" "$(pwd)"

# show data as columns, separated by tab delimiter
column -tn -s $'\t'

# create or replace file with text (change > to >> for appending)
cat << 'EOF' > FILE
FILE-CONTENT
EOF

# show directories
ls -F | grep -v "/"

# show symlinks
ls -F | grep -v "@"

# show executables
ls -F | grep -v "*"

# show sockets
ls -F | grep -v "=" 

# show whiteouts
ls -F | grep -v "%" 

# show FIFOs (named pipes)
ls -F | grep -v "|" 

Commands For Searching, Permissions, SSH Keys

copy
# recursively search files for string
grep -rnw '/PATH/' -e "REGEX"

# search for file by name
find /PATH -name "FILE-NAME"

# search for file by type (file, directory, symlink, socket, block device, character device, pipe FIFO)
find /PATH -type f
find /PATH -type d
find /PATH -type l
find /PATH -type s
find /PATH -type b
find /PATH -type c
find /PATH -type p

# execute command on found files, {} replaced by file name, \; is a escaped semicolon denoting end of command to run
find /PATH -type f -exec COMMMAND {} \;

# recursively set permissions on directories or files
find /PATH -type d -exec chmod 755 {} \;
find /PATH -type f -exec chmod 644 {} \;

# recursively replace spaces with underscores in file names
find -name "* *" -type f | rename 's/ /_/g'

# delete found files
find /PATH -type f -delete

# list all users with user id and group id
getent passwd | cut -d":" -f1,3,4

# list all groups with group id
getent group | cut -d":" -f1,3

change owner:group of file
chown USER:GROUP FILE

# give Other read permissions
chmod o+r FILE

# give Group write permissions
chmod g+w FILE

# give User execute permissions
chmod u+x FILE

# set SUID, binary executes with user permissions
chmod u+s FILE

# set GUID, binary executes with group permissions
chmod g+s FILE

# set sticky bit, only owner can delete this file
chmod +t FILE

# generate a key
ssh-keygen -t rsa -C "COMMENT-USUALLY-EMAIL-FOR-ID" -b 4096 -f ~/.ssh/id_rsa

# use this command to copy to a specific user's directory
cat /home/USERNAME/.ssh/id_rsa.pub | ssh USERNAME@IP-ADDRESS "mkdir -p /home/USERNAME/.ssh && cat >>  /home/USERNAME/.ssh/authorized_keys"

User & Group Commands

copy
# non-interactive create sudo user
useradd USERNAME
gpasswd -a USERNAME sudo
usermod -g sudo USERNAME
groupdel USERNAME
(echo 'PASSWORD'; echo 'PASSWORD') | passwd USERNAME

# run command as different user
su - USER -s /bin/bash -c "COMMAND"

Networking & Process Commands

copy
# display  -t tcp connections, -u udp connections, -l listening, -p program name, -n numerical address
netstat -tulpn

# display -r routing address with -n numerical addresses
netstat -rn

# display network interfaces
netstat -i

$ get DNS info on domain
dig DOMAIN

# list the process tree
pstree

# list processes, -A all, -o in output format (user:group pid tty command), remove first line, sort by user
ps -Ao "%u:%g    %p    %y    %c" | tail -n +2 | sort

OS Information

copy
# view kernel name
uname -s

# view userspace name / kernel name
uname -o

# view machine type (processor)
uname -m

# view os flavor, display only name, remove leading tab
lsb_release -i | cut -d":" -f2 | tr -d "\t"

# view os flavor version number, display only number, remove leading tab
lsb_release -r | cut -d":" -f2 | tr -d "\t"

# view os flavor version codename, display only number, remove leading tab
lsb_release -c | cut -d":" -f2 | tr -d "\t"

Handling Ubuntu Packages

copy
# search for package
apt-cache search PACKAGE

# reload package repos
apt-get update

# upgrade all packages to newest versions
apt-get upgrade

# install a package
apt-get install PACKAGE

# remove package, but keep config files
apt-get remove PACKAGE

# remove package config files
apt-get purge PACKAGE

Useful VIM Commands

copy
# display numbers
:set number

# move forward by word, then backward by word
w
b

# go to start of line, then go to end of line
0
$

# enter "insert mode" here, start of line, end of line
i
I
A

# select, "visual mode", copy (yank), paste, delete, go to end of file
v
y
p
d
G

# copy, paste, delete current line in "normal mode"
yy
pp
dd

# undo, redo
:u
:r

# search for pattern, search backwards, repeat previous search, repeat backwards
:/PATTERN
:?PATTERN
:n
:N

# sort the document
:sort

BASH Commands & Variables

copy
# do not save this command in history
history -d $((HISTCMD-1)) && COMMAND

# print all environment variables
printenv

# command name
$0

# first three arguments
$1
$2
$3

# number of arguments
$#

# all arguments, with expansion
$*

# all arguments, without expansion
$@