logo
eng-flag

Zsh Cheatsheet

Table of Contents

  1. Basic Commands
  2. File and Directory Operations
  3. Text Manipulation
  4. Process Management
  5. Job Control
  6. Shortcuts and Hotkeys
  7. Aliases and Functions
  8. History
  9. Globbing and Wildcards
  10. Redirection and Pipes
  11. Variables and Environment
  12. Customization
  13. Zsh-specific Features

Basic Commands

  • Print working directory: pwd
  • Change directory: cd [directory]
  • Go to home directory: cd or cd ~
  • Go to previous directory: cd -

Example:

$ pwd
/home/user/documents
$ cd ..
$ pwd
/home/user
$ cd -
/home/user/documents

Listing Files and Directories

  • List files: ls
  • List all files (including hidden): ls -a
  • List with details: ls -l
  • List with human-readable file sizes: ls -lh

Example:

$ ls -lh
total 20K
drwxr-xr-x 2 user user 4.0K Jul 19 10:00 Desktop
drwxr-xr-x 2 user user 4.0K Jul 19 10:00 Documents
drwxr-xr-x 2 user user 4.0K Jul 19 10:00 Downloads

File and Directory Operations

Creating Files and Directories

  • Create an empty file: touch [filename]
  • Create a directory: mkdir [dirname]
  • Create nested directories: mkdir -p [path/to/dirname]

Example:

$ touch newfile.txt
$ mkdir newdir
$ mkdir -p deeply/nested/directory

Copying, Moving, and Removing

  • Copy file: cp [source] [destination]
  • Copy directory recursively: cp -R [source] [destination]
  • Move/rename file or directory: mv [source] [destination]
  • Remove file: rm [filename]
  • Remove empty directory: rmdir [dirname]
  • Remove directory and contents: rm -r [dirname]

Example:

$ cp file1.txt file2.txt
$ mv file2.txt newname.txt
$ rm newname.txt
$ rm -r olddir

File Permissions

  • Change file permissions: chmod [permissions] [filename]
  • Change file owner: chown [user]:[group] [filename]

Example:

$ chmod 644 myfile.txt
$ chown user:group myfile.txt

Text Manipulation

Viewing File Contents

  • View entire file: cat [filename]
  • View file with paging: less [filename]
  • View first few lines: head [filename]
  • View last few lines: tail [filename]

Example:

$ cat myfile.txt
This is the content of myfile.txt
$ tail -n 5 logfile.log

Searching and Filtering

  • Search for pattern in file: grep [pattern] [filename]
  • Search recursively in directory: grep -r [pattern] [directory]
  • Count occurrences of pattern: grep -c [pattern] [filename]

Example:

$ grep "error" logfile.log
$ grep -r "TODO" ./src

Text Editing

  • Edit file with nano: nano [filename]
  • Edit file with vim: vim [filename]

Example:

$ nano config.txt
$ vim script.sh

Process Management

Viewing Processes

  • List all processes: ps aux
  • List processes for current user: ps
  • Display process tree: pstree

Example:

$ ps aux | grep nginx
$ pstree -p

Managing Processes

  • Kill process by PID: kill [PID]
  • Kill process by name: pkill [process_name]
  • Force kill process: kill -9 [PID]

Example:

$ kill 1234
$ pkill firefox

Job Control

Background and Foreground Jobs

  • Run command in background: [command] &
  • List background jobs: jobs
  • Bring job to foreground: fg [job_number]
  • Send job to background: bg [job_number]

Example:

$ long_running_command &
[1] 1234
$ jobs
[1]+  Running                 long_running_command &
$ fg 1

Shortcuts and Hotkeys

  • Interrupt current process: Ctrl + C
  • Stop current process: Ctrl + Z
  • Clear screen: Ctrl + L
  • Search command history: Ctrl + R
  • Move cursor to beginning of line: Ctrl + A
  • Move cursor to end of line: Ctrl + E
  • Delete word before cursor: Ctrl + W
  • Delete line: Ctrl + U

Aliases and Functions

Creating Aliases

  • Create temporary alias: alias [name]='[command]'
  • List all aliases: alias

Example:

$ alias ll='ls -lh'
$ alias
ll='ls -lh'

Creating Functions

  • Create a function:
function_name() {
    # Function body
}

Example:

$ greet() {
>     echo "Hello, $1!"
> }
$ greet Alice
Hello, Alice!

History

  • View command history: history
  • Run last command: !!
  • Run nth command from history: ![n]
  • Run last command starting with 'string': !string

Example:

$ history
1  ls
2  cd documents
3  grep "error" log.txt
$ !grep
grep "error" log.txt

Globbing and Wildcards

  • Match any number of characters: *
  • Match any single character: ?
  • Match any of the enclosed characters: [...]
  • Match any except the enclosed characters: [!...]

Example:

$ ls *.txt
file1.txt file2.txt
$ ls doc?ment.txt
document.txt
$ ls file[1-3].txt
file1.txt file2.txt file3.txt

Redirection and Pipes

Input/Output Redirection

  • Redirect stdout to file: command > file
  • Append stdout to file: command >> file
  • Redirect stderr to file: command 2> file
  • Redirect both stdout and stderr: command &> file

Example:

$ echo "Hello, World!" > greeting.txt
$ ls non_existent_file 2> error.log

Pipes

  • Pipe output of one command to another: command1 | command2

Example:

$ ls -l | grep ".txt"
$ cat large_file.txt | less

Variables and Environment

Setting and Using Variables

  • Set variable: variable_name=value
  • Use variable: $variable_name
  • Export variable to environment: export variable_name=value

Example:

$ name="John"
$ echo "Hello, $name!"
Hello, John!
$ export PATH="$PATH:/new/path"

Environment Variables

  • View all environment variables: env
  • View specific environment variable: echo $VARIABLE_NAME

Example:

$ echo $HOME
/home/user
$ echo $PATH
/usr/local/bin:/usr/bin:/bin

Customization

Configuring Zsh

  • Edit Zsh configuration: nano ~/.zshrc
  • Reload Zsh configuration: source ~/.zshrc

Example of adding to ~/.zshrc:

# Set custom prompt
PROMPT='%F{green}%n@%m%f:%F{blue}%~%f$ '

# Add custom alias
alias update='sudo apt update && sudo apt upgrade -y'

Zsh-specific Features

Extended Globbing

  • Enable extended globbing: setopt extended_glob
  • Match files with extension .txt or .log: *(.(txt|log))
  • Match all directories: *(/)
  • Match all symbolic links: *(@)

Example:

$ ls *(.(txt|log))
file1.txt file2.txt error.log

Autocomplete and Correction

  • Tab completion: Press Tab for suggestions
  • Autocorrect commands: setopt correct

Example:

$ grpe "pattern" file.txt
zsh: correct 'grpe' to 'grep' [nyae]?

Directory Stack

  • Push directory to stack: pushd [directory]
  • Pop directory from stack: popd
  • List directory stack: dirs -v

Example:

$ pushd /var/log
$ pushd /etc
$ dirs -v
0       /etc
1       /var/log
2       ~
$ popd
$ pwd
/var/log

2024 © All rights reserved - buraxta.com