basforge

Search titles, text and the “If it doesn’t work” cases.

    RU

    ~/.bash_aliases block by block: everyday aliases

    A ready-to-use ~/.bash_aliases for servers and your own machine: navigation, confirmation before deleting, open ports, service logs, containers. For standard commands (rm, cp, df), the aliases only add a prompt or human-readable units — the outcome is the same as without the file.

    How the file is built

    A command belongs here if you type it on a server dozens of times a day or keep forgetting its flags: listening ports, disk usage, service journals, containers. For standard commands — rm, cp, mv, df, free — the aliases only add a confirmation prompt or sizes in G and M. On a server without this file, the same commands behave identically, minus the prompt and in kilobytes.

    Save the file to ~/.bash_aliases and reload it with source ~/.bash_aliases. How Bash loads it and how to copy it to a server is covered in Bash aliases. Part two is in Lesser-known aliases: tail -F, dmesg -T, certificate expiry, and other lines fewer people know about.

    The file, block by block

    ~/.bash_aliases
    # ~/.bash_aliases — sourced by ~/.bashrc on Ubuntu
    alias ..='cd ..'
    alias ...='cd ../..'

    Go up one or two levels. Dots are valid in alias names. To jump back to the previous directory, use cd - — no alias needed.

    # ll, la, l are already in Ubuntu's ~/.bashrc
    alias lt='ls -lhtr'

    Newest files land at the bottom, right above the prompt: -t sorts by modification time and -r reverses the order. In a log directory, the latest file is visible without scrolling, and -h turns sizes into K, M, G. Color in lt and ll comes from Ubuntu’s ls='ls --color=auto': Bash checks the first word of an alias’s expansion for another alias.

    alias rm='rm -I'
    alias cp='cp -i'
    alias mv='mv -i'
    Key part

    rm asks once — when removing more than three files or recursing: rm: remove 40 arguments?. Why -I instead of -i is explained in rm -I instead of rm -i. cp and mv prompt only if the destination exists: mv: overwrite 'nginx.conf'?. For rm and mv, -f cancels the prompt; for cp, it doesn’t — overwrite without asking via \cp.

    alias sudo='sudo '
    Key part

    The trailing space tells Bash to check the next word for an alias too: sudo ports and sudo dc up -d work. This applies only to the word immediately after sudo: in sudo -u www-data ll, -u comes next, so ll doesn’t expand.

    alias df='df -h'
    alias free='free -h'

    Without -h, df and free report in kilobytes. The flag changes formatting only. To show real disks without in-memory tmpfs mounts (/run, /dev/shm), run df -x tmpfs: extra arguments go after -h.

    alias ports='ss -tulpn'
    alias ips='ip -br a'

    ports shows what the server is listening on: TCP and UDP (-tu), listening sockets only (-l), owning program (-p), numeric ports instead of service names (-n): :53 rather than :domain. Programs owned by other users show only for root — sudo ports. ips prints one line per interface: name, state, addresses, e.g. eth0 UP 10.0.0.11/24.

    alias failed='systemctl --failed'
    alias jf='journalctl -f -u'

    failed lists failed services — the first check after a reboot. When everything is healthy, it prints 0 loaded units listed. jf nginx tails a service’s journal live; press Ctrl+C to exit.

    alias dc='docker compose'
    alias dps='docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"'

    Run dc up -d or dc logs -f --tail 100 inside a stack directory. Tab after dc suggests filenames until you wire up alias completion — Tab after an alias. dps prints containers in three columns: name, status, ports. Plain docker ps with a single nginx container outputs a 136-character line that wraps in most terminals. Bash leaves double quotes inside single quotes alone when defining the alias: they stay intact at runtime so the format string reaches docker as one argument.

    alias update='sudo apt update && sudo apt upgrade'

    No -y: apt upgrade lists packages and waits for confirmation, so you see what is about to update before it happens. Security updates can also run unattended — unattended-upgrades.

    alias path='tr ":" "\n" <<< "$PATH"'

    Prints PATH one directory per line, so you can see whether ~/.local/bin is included and in what order Bash searches for programs. Outer single quotes defer $PATH expansion until path runs, rather than freezing it at login.

    alias myip='curl -s -4 https://icanhazip.com'

    Your public IP — handy when a server sits behind NAT or you need to check which address outbound traffic uses. It queries an external service, so treat this line as optional. -4 forces IPv4. ifconfig.me responds without a trailing newline, sticking your prompt to the address; icanhazip.com includes one.

    duh() { du -h -d1 "${1:-.}" 2>/dev/null | sort -h; }
    mkcd() { mkdir -p "$1" && cd "$1"; }

    Functions live in the same file, below aliases: Bash expands aliases inside a function when it reads the definition, so an alias declared further down won’t take effect inside it. Use a function when an argument goes in the middle of a command or you need cd; how to write them and three more daily functions are in Bash functions. duh /var lists subdirectories of /var sorted by size with the largest at the bottom; with no argument, it checks the current directory. Unreadable directories are skipped silently and excluded from the total. mkcd creates a directory and moves into it.

    These show up often in shared .bashrc files. Don’t copy them blindly: some duplicate shortcuts Bash already has, and others change how standard commands behave. Add them only when you have a specific need and know the side effects.

    Alias What to keep in mind
    alias c='clear' Ctrl+L clears the screen in any Bash shell, no config file needed
    alias h='history | grep' Ctrl+R searches history as you type; press Ctrl+R again for the next match. history | grep is useful when you want every match at once with line numbers for !number
    alias mkdir='mkdir -p' a typo mid-path silently creates stray directories: mkdir /opt/trafik/data creates trafik too
    alias grep='grep -rn' ps aux | grep sshd searches files in the current directory and ignores ps output (Bash aliases)
    alias web1='ssh [email protected]' use Host web1 in ~/.ssh/config instead: scp, rsync, and git understand it too (~/.ssh/config)

    If it doesn’t work

    cp -f still asks cp: overwrite 'nginx.conf'?

    Cause
    For cp, -f doesn’t override -i from the alias: it means removing the destination file if it can’t be opened for writing. For rm and mv, -f does cancel the prompt.
    Fix
    Overwrite without the alias: \cp nginx.conf /etc/nginx/.

    sudo duh /var prints sudo: duh: command not found

    Cause
    duh is a function, and sudo runs programs only. The trailing space in alias sudo='sudo ' expands aliases, not functions.
    Fix
    For root-owned directories, run the full command: sudo du -h -d1 /var | sort -h. Or switch to a root shell if root has the file too: sudo -i, then duh /var.

    ports lists open ports, but the Process column is empty

    Cause
    ss shows which process owns a socket only to that process’s owner and root.
    Fix
    Run sudo ports: the alias expands after sudo because of alias sudo='sudo '.

    jf nginx prints No journal files were opened due to insufficient permissions.

    Cause
    System journal files are readable by root and the adm and systemd-journal groups — the Hint: line above the error says the same. Ubuntu’s initial user is in adm; users added later aren’t.
    Fix
    Run sudo jf nginx, or add the user to the group: sudo usermod -aG adm example-user and log in again.

    How to check

    Confirm the aliases are loaded:

    type rm sudo dps
    # rm is aliased to `rm -I'
    # sudo is aliased to `sudo '
    # dps is aliased to `docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"'

    Confirm alias expansion works after sudo — the output includes the owning process:

    sudo ports | grep sshd
    # tcp   LISTEN 0      4096         0.0.0.0:22         0.0.0.0:*    users:(("sshd",pid=1213,fd=3),("systemd",pid=1,fd=94))