Linux shell and essential commands

Topic: Servers linux

Summary

Use the Linux shell to navigate, inspect, and change the system: cd, ls, cat, grep, find, ps, systemctl, and redirection. Verify commands with exit codes and output so you can operate and debug servers without a GUI.

Intent: How-to

Quick answer

  • Navigate with cd, pwd, ls; read files with cat, less, head, tail; search with grep and find; run processes with ps, pgrep, and systemctl.
  • Use > and >> to redirect stdout; 2>&1 to merge stderr; | to pipe; check exit code with echo $? after a command.
  • Prefer absolute paths in scripts and cron; quote variables; test with set -e or explicit checks so failures are visible.

Prerequisites

Steps

  1. Navigate and list

    cd /path, pwd, ls -la; use tab completion and ls -d */ for directories; understand . and .. and ~.

  2. Read and search

    cat file, less file (scroll), head -n, tail -n -f; grep -r pattern dir; find dir -name '*.conf' -type f.

  3. Processes and services

    ps aux, pgrep -a name; systemctl status name, systemctl start/stop/restart; journalctl -u name -f for logs.

  4. Redirection and exit codes

    cmd > out 2>&1; cmd >> log; echo $? after a command (0 = success). Use these in scripts and cron to detect failures.

Summary

You will use the shell to move around the filesystem, read and search files, inspect and control processes and services, and use redirection and exit codes. Use this as the baseline for any server task and for writing reliable scripts.

Prerequisites

  • SSH or console access to a Linux system.
  • Default shell is bash or another POSIX shell.

Steps

Step 1: Navigate and list

cd /var/log
pwd
ls -la
ls -d */

Use cd - to switch back to the previous directory; ~ for home; .. for parent.

cat /etc/hostname
less /var/log/syslog
tail -f /var/log/app.log
grep -r "error" /etc/nginx
find /var -name "*.log" -mtime +7

Use less for large files; tail -f to follow logs; grep -E for regex; find with -type f and -mtime for cleanup.

Step 3: Processes and services

ps aux | grep nginx
pgrep -a nginx
systemctl status nginx
systemctl restart nginx
journalctl -u nginx -f --no-pager

Use systemctl list-units --type=service to see loaded services; journalctl -b for current boot.

Step 4: Redirection and exit codes

nginx -t > /tmp/nginx-test 2>&1
echo $?

Redirect stderr with 2>&1; append with >>. In scripts use set -e or check $? after critical commands so failures stop the script or are logged.

Verification

  • You can cd to a path, list files, read a log, grep for a string, find a process, and check a service status; you have run one command and confirmed its exit code.

Troubleshooting

Command not found — Binary not in PATH; use full path (e.g. /usr/sbin/nginx) or fix PATH; check if the package is installed.

Permission denied — Need to run as root or correct user; use sudo or fix ownership; for scripts ensure shebang and execute bit (chmod +x).

No space left on device — Disk full; free space with df -h and du; clear logs or caches; see disk-full recovery guide.

Next steps

Continue to