﻿---
title: Linux Commands
date: 2024-03-10
excerpt: Notes on common Linux commands, shortcuts, and plugins, plus recommended terminals and shells.
tags:
  - Linux
cover: https://assets.vluv.space/cover/Dev/linuxcmds.webp
updated: 2026-07-08 21:21:32
lang: en
i18n:
  cn: /linux_cmds
  translation: 2
---

## Command Categories

- CLI (Command Line Interface): flexible but hard to remember because of its verbosity; commands like `cd` and `ls`
- TUI (Terminal User Interface): tries to offer a friendly user interface on a character terminal, e.g. `vim`, `htop`
- GUI (Graphical User Interface): intuitive and easy to remember but less flexible, e.g. `explorer`, Finder

## File Operations

File operations are a fundamental part of a Linux system. Here are some common file operation commands and what they do:

### Directory Operations

- ls, list directory contents

  - Purpose: show the files and folders under a given directory.
  - Count files in a folder: `ls -A <folder_path> wc -l`

  In `-rwxrwxrwx`, the leading `-` means a regular file; `d` would mean a directory. The three `rwx` groups are the permissions of the file owner, the group, and other users. The first `efterklang` is the file owner, the second is the owner's group, 1093 is the file size in bytes, the timestamp is the last modified time, and the last field is the file name.

  ```shell
  $ ls -l ./
  total 4
  -rwxrwxrwx 1 efterklang efterklang 1093 Dec 20 17:02 LICENSE
  drwxrwxrwx 1 efterklang efterklang 4096 Jun  9 20:50 backdrops
  drwxrwxrwx 1 efterklang efterklang 4096 Apr 26 14:57 colors
  drwxrwxrwx 1 efterklang efterklang 4096 May 31 18:06 config
  drwxrwxrwx 1 efterklang efterklang 4096 May 25 16:57 events
  drwxrwxrwx 1 efterklang efterklang 4096 May 26 10:28 utils
  -rwxrwxrwx 1 efterklang efterklang  516 May 27 12:19 wezterm.lua
  ```

- cd, change the current directory
  - Purpose: switch the current working directory to a given path.
  - cd ~ goes to the home directory; cd / goes to the root; cd .. goes up one level; cd - goes back to the previous working directory
  - Modern cd replacements: zoxide, z
- pwd, print working directory
  - Purpose: show the full path of the current working directory.
- cp, copy files or directories
  - Purpose: copy files or directories from one location to another.
- mv, move or rename files
  - Purpose: move a file or directory to a new location, or rename it.
  - Rename/move a file: `mv <old_name> <new_name>` `mv <old_path> <new_path>`
  - Move all files from one folder into another: `mv <source_folder>/* <target_folder>`
- rm, delete files or directories
- touch, create an empty file or update a file's timestamp
- mkdir, create a directory
- rmdir, delete an empty directory

### Permissions

- chmod, change file or directory permissions
- chown, change the owner of a file or directory
- chgrp, change the group of a file or directory
- cat, concatenate files and print to standard output
  - Purpose: display the contents of a file.
- more, view file contents page by page
  - Purpose: page through a file's contents, handy for long files.
- less, view file contents with forward and backward paging
  - Purpose: display a file's contents with support for scrolling both ways.
- head, show the beginning of a file

  - Purpose: show the first few lines of a file, 10 by default.

  ```bash
  head filename.txt  # show the first 10 lines
  head -n 20 filename.txt  # show the first 20 lines
  ```

- tail, show the end of a file

  - Purpose: show the last few lines of a file, 10 by default.

  ```bash
  tail filename.txt  # show the last 10 lines
  tail -n 20 filename.txt  # show the last 20 lines
  tail -f filename.txt  # follow the file and show new content in real time
  ```

- find, search for files
  - Purpose: search a directory and its subdirectories for files matching given conditions.
- diff, compare files
  - Purpose: compare the differences between two files or directories.
- ln, create links
  - Purpose: create hard links or symbolic links to files or directories.
- du, disk usage
  - Purpose: show the disk space used by files or directories.
- df, disk free
  - Purpose: show disk space usage of file systems.
- tar, archive, compress, or extract files
  - Purpose: create, inspect, or extract tar archives.
- gzip, compress or decompress files
  - Purpose: compress or decompress files with the gzip program.

The nohup command keeps a process running after you exit the shell: `nohup <command> &`
For example: `nohup python ./train.py >> train.log 2>&1 &`

## User Management

### Logging In

The superuser is named root, with a password set during system installation. Once a user enters the correct username and password, they can log into the system.
A regular user can log in once a regular account has been created.

- `su root` temporarily switches to root. It asks for the password, keeps the current environment variables, grants part of root's privileges, and only allows commands from the current user's PATH, not commands exclusive to root's PATH
- `su - root` switches to root. It asks for the password, replaces the environment variables, and lets you do almost anything without restriction
- `su - <user_name>` switches to another user
- `su - username -c <command>`: execute a command as another user
- `exit/logout` exits the current user session

### Account Management

Managing accounts on Linux covers three things: adding, deleting, and modifying users.

**Adding a user**
`useradd <option> <user_name>`
-c comment, attach a descriptive comment.
-d directory, set the user's home directory; if it doesn't exist, add -m to create it.
-g group, set the user's default group.
-G group, usually combined with -a to add the user to other groups.
-s shell file, set the user's login shell.
-u user ID, set the user's UID; with -o, the ID of another user can be reused.

**Deleting a user**

Deleting a user account means removing the user's records from system files like /etc/passwd, and when necessary, removing the user's home directory too. Use the userdel command:

`userdel <option> <user_name>`
The most common option is -r, which also removes the user's home directory.

**Modifying a user**

Use the usermod command:
`usermod <option> <user_name>`

Common options include -c, -d, -m, -g, -G, -s, -u, and -o. They mean the same as in useradd and assign new values to the user.
Modifying a user account means changing its attributes as needed, such as the UID, home directory, group, or login shell.

### File Permissions

The `chmod` command modifies a file's access permissions.

- format: `chmod <mode> <file>`
- mode:
  - user: u (user, the owner), g (group, users in the same group), o (other users), a (all)
  - operation: + (add permission), - (remove permission), or = (set permission)
  - permission: r (read), w (write), x (execute), s (setuid, set user ID), t (setgid, set group ID)

## Regular Expression

`grep` is a powerful text search tool that lets you search files for lines matching a given pattern. Here are some `grep` use cases:
[RIPgrep GUIDE](https://github.com/BurntSushi/ripgrep/blob/master/GUIDE.md)

Options

- i: ignore case
- n: show line numbers
- v: invert match
- r/R: recursive search, i.e. search files in a directory and all its subdirectories (note: ripgrep searches recursively by default)
  - R also dereferences symbolic links

1. **Basic search**: search for specific text in a file. For example, to search for the word "hello" in `example.txt`:

   ```bash
   grep "hello" example.txt
   ```

2. **Recursive search**: `grep` can search directories recursively. For example, to search for "hello" in the current directory and all its subdirectories, use `-r` or `-R`:

   ```bash
   grep -r "hello" .
   ```

3. **Inverted match**: `grep` can also find lines that do not match a pattern. The following command shows all lines in `example.txt` that do not contain "hello":

   ```bash
   grep -v "hello" example.txt
   ```

4. **Anchoring the start/end of a line**

   ```bash
    grep "^May" dairy.md # match lines starting with May
    grep "hello$" example.txt # match lines ending with hello
   ```

## Emacs Mode Shortcuts

```wikitext
Ctrl + L clear
Ctrl + K delete from the cursor to the end of the line
Ctrl + U delete from the cursor to the beginning of the line
Ctrl + A: move the cursor to the beginning of the line
Ctrl + E: move the cursor to the end of the line
Ctrl + R: search command history
Ctrl + C: kill the process
Ctrl + D: exit the shell
Tab: autocomplete
```
