Tuesday, June 4, 2024

Shell Commands & Aliases

 -  Different names for the command line interface (CLI): shell, terminal, console []

-  The shell (e.g., bash, zsh) interprets commands []

-  `ls` lists directory contents, can take arguments for format (e.g., `ls -l`) []

-  `cd` changes directory, `pwd` shows current directory []

-  `echo` prints text, `cat` prints file contents []

-  `cp` copies files/directories, `mv` moves them [➡️]

-  `rm` deletes files, `rm -rf` forcefully deletes recursively (be careful!) []

-  `man` shows the manual for a command []


-  `sed` edits text streams (useful for find & replace) []

-  `awk` is for text processing (useful for extracting data) []

-  `sort` sorts text content (numeric, random sorting possible) [ ترتيب]

-  `head` shows the first few lines of a file  []

-  `tail` follows the end of a file (useful for logs) []

-  `|` pipes the output of one command to the input of another [➡️]

-  `xargs` splits piped content and uses it as arguments for another command [✂️]

-  `$()` subshells let you run commands within commands []



-  `compgen -c` lists possible completions for commands []

-  Combine `fzf` with `man` to search and quickly view manuals []

-  Create aliases to easily run complex pipelines [➡️➡️➡️]

-  Use `fzf` with `kubectl` to manage Kubernetes pods [☸️]

-  Find large files/directories with `du -h | sort -r | head` []

-  Use `fzf` to find and delete large Node.js project directories [♻️]

-  Shell history (`history`) lets you see past commands []

-  Common hotkeys: `ctrl+c` kill process, `ctrl+d` exit shell, `ctrl+l` clear screen []



-  `ctrl+a ctrl+e` opens the current line in your editor for complex editing []

- Use `!!` to run the previous command again []

- Use `!` followed by part of a command to run most recent similar command [🪄]

- Shell hotkeys are numerous, refer to documentation for specifics [] 

- Regularly use the shell to solidify your learnings  []

- Leverage `man` pages or `tldr` to understand commands []

- Pipes are powerful for chaining commands together [➡️➡️➡️]

Alias Creation in the Shell

An alias lets you create a shortcut for a frequently used command or sequence of commands in the shell. This can save you time and effort by making it easier to remember and execute complex commands.

Here's a breakdown of alias creation:

1. Syntax:

The basic syntax for creating an alias is:

alias <alias_name>="<command>"
  • <alias_name>: This is the name you choose for your shortcut. It should be something memorable and easy to type.
  • <command>: This is the actual command or sequence of commands that the alias will execute when you type the alias name. You can use quotes (single or double) to group the command if it includes spaces or special characters.

2. Examples:

Here are some examples of alias creation:

  • Create an alias named ll to run ls -l (long listing) for directories:
alias ll="ls -l"
  • Create an alias named update to run a sequence of commands to update your system:
alias update="sudo apt update && sudo apt upgrade -y"

3. Temporary vs. Permanent Aliases:

  • Temporary Aliases: These aliases only exist for the current shell session. Once you close the terminal window, the alias is gone.

  • Permanent Aliases: To make an alias persistent across sessions, you need to add the alias definition to your shell configuration file.

    • For Bash shells (like the default shell on Ubuntu/Debian): Edit the .bashrc file located in your home directory (~/.bashrc).
    • For Zsh shells: Edit the .zshrc file located in your home directory (~/.zshrc).

    Add the alias definition line (e.g., alias ll="ls -l") to your chosen configuration file. Then, save the file and source it for the changes to take effect (use the source ~/.bashrc or source ~/.zshrc command depending on your shell).

4. Additional Tips:

  • Choose clear and concise alias names that reflect the command's purpose.
  • Avoid using existing command names or reserved keywords as aliases.
  • Be cautious when creating aliases for complex commands, as mistakes can lead to unexpected behavior.

By creating aliases, you can streamline your workflow and become more efficient in the shell environment.

create 10 aliases for frequently used commands(with emojis)

10 Shell Aliases with Emojis ♻️ ⬆️ ⬇️ ⏱️ ⚙️

Here are 10 shell aliases with emojis for frequently used commands:

  1. List directory details ( ll):
alias ll="ls -l"
  1. Search current directory ( find-here):
alias find-here="grep --color=always"
  1. Clean up old files (♻️ purge):
alias purge="find . -type f -mtime +30 -delete"  # Adjust time (days) as needed
  1. Update and upgrade system ( sys-update):
alias sys-update="sudo apt update && sudo apt upgrade -y"  # For Debian/Ubuntu systems
  1. Go to previous directory (⬆️ up):
alias up="cd .."
  1. Go to home directory (⬇️ home):
alias home="cd ~"
  1. Show last 10 commands ( history):
alias history="history | tail 10"
  1. Check system uptime (⏱️ uptime):
alias uptime="uptime -p"  # Show uptime in human-readable format
  1. Edit shell configuration (⚙️ edit-config):
alias edit-config="nano ~/.bashrc"  # Adjust editor (e.g., vim) if needed
  1. Display manual page ( man-page):
alias man-page="man"

Remember: Replace nano in edit-config with your preferred text editor if you don't use nano. These are just examples, and you can customize the commands and aliases based on your needs.

how to make the aliases from emojis

Unfortunately, you can't directly create emojis within alias names on the command line. Alias names need to be formed from letters, numbers, underscores (_), and hyphens (-). Emojis are special characters not included in this allowed set.

However, you can still leverage the emojis you provided alongside your aliases to enhance your shell experience. Here are a couple of ways to do this:

  1. Use Emojis in Your Shell Prompt:
  • Configure your shell prompt to display the emojis before or after your username. This visually associates the emojis with the specific functionality of your aliases. There are various tools and guides available online for customizing your shell prompt.
  1. Create an Emoji Alias "Cheat Sheet":
  • Create a text file (e.g., alias_emojis.txt) that lists your aliases with their corresponding emojis. This cheat sheet serves as a quick reference for what each emoji represents. You can keep this file in your home directory for easy access.

Here's an example of an alias cheat sheet:

♻️ purge      - Clean up old files (find . -type f -mtime +30 -delete)
⬆️ up         - Go to previous directory (cd ..)
⬇️ home       - Go to home directory (cd ~)
... (list other aliases with emojis)

By using these methods, you can still benefit from the visual cues provided by the emojis while maintaining functional alias names on the command line