Skip to content
IT related

bashrc

Updated 1 min read

Here is a bashrc I have been working on slowly. Im trying to make one that i can use on all of my systems with usefull commands. Im a fan of the Dracula theme and use it when I can.

# .bashrc

# Set nano as the default editor for command-line based editing
export EDITOR=nano

# Uncomment the next line to enable a colored command prompt
# force_color_prompt=yes

# Customize the command prompt appearance
# PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '

# Alias for detailed list view
# alias ll='ls -l'

# Alias for listing files with detailed information, including hidden files
alias l="ls -lAh --color=auto"

zip() {
    if [ $# -lt 2 ]; then
        echo "Usage: zip <output_filename_with_format> <file1> [<file2> ...]"
        echo "For example: zip archive.zip file1 file2"
        return 1
    fi

    output_filename="$1"
    shift
    files=("$@")

    # Determine the format from the output filename
    case "$output_filename" in
        *.zip)
            format="zip" ;;
        *.rar)
            format="rar" ;;
        *.7z)
            format="7z" ;;
        *.tar.gz)
            format="tar.gz" ;;
        *.tar.xz)
            format="tar.xz" ;;
        *.tar)
            format="tar" ;;
        *)
            echo "Unsupported format for file: $output_filename"
            return 1 ;;
    esac

    # Execute the appropriate command based on the format
    case "$format" in
        zip)
            command zip "$output_filename" "${files[@]}" ;;
        rar)
            command rar a "$output_filename" "${files[@]}" ;;
        7z)
            command 7z a "$output_filename" "${files[@]}" ;;
        tar.gz)
            command tar -czvf "$output_filename" "${files[@]}" ;;
        tar.xz)
            command tar -cJvf "$output_filename" "${files[@]}" ;;
        tar)
            command tar -cvf "$output_filename" "${files[@]}" ;;
        *)
            echo "Error: Unrecognized format '$format'"
            return 1 ;;
    esac

    echo "Created: $output_filename"
}


unzip() {
    if [ $# -lt 3 ]; then
        echo "Usage: unzip <archive_filename>... -o <output_directory>"
        echo "Supported formats: zip, rar, 7z, tar, tar.gz, tar.xz"
        return 1
    fi

    local output_dir=""
    local archives=()

    # Process arguments
    while [[ $# -gt 0 ]]; do
        case "$1" in
            -o)
                output_dir="$2"
                shift 2 ;;
            *)
                archives+=("$1")
                shift ;;
        esac
    done

    # Check if output directory is specified
    if [ -z "$output_dir" ]; then
        echo "Error: Output directory not specified"
        return 1
    fi

    # Create output directory if it doesn't exist
    mkdir -p "$output_dir"

    for archive_filename in "${archives[@]}"; do
        case "$archive_filename" in
            *.zip)
                command unzip -d "$output_dir" "$archive_filename" ;;
            *.rar)
                # For RAR, 7z, and TAR files, the extraction directory is handled differently
                command unrar x "$archive_filename" "$output_dir" ;;
            *.7z)
                command 7z x -o"$output_dir/" "$archive_filename" ;;
            *.tar|*.tar.gz|*.tar.xz)
                command tar -xvf "$archive_filename" -C "$output_dir" ;;
            *)
                echo "Unsupported file format: $archive_filename" ;;
        esac

        echo "Extracted into folder: $output_dir"
    done
}

# Define the ports function
ports() {
    # Determine whether to use ss or netstat based on their availability
    local cmd
    if command -v ss > /dev/null 2>&1; then
        cmd="sudo ss -tulnp"
    elif command -v netstat > /dev/null 2>&1; then
        cmd="netstat -tuln"
    else
        echo "Error: Neither 'ss' nor 'netstat' command is available."
        return 1
    fi

    # Print the header with tabs for column spacing
    echo -e "Protocol\t\tPort\tPID/Program name\t\t\t\tAddress"
    echo -e "--------\t\t----\t----------------\t\t\t\t-------"

    # Execute the selected command and pipe the output to awk for processing
    $cmd | awk 'NR>1 {  # Process each line except the header (NR>1)
        split($5, addr, ":");  # Split the address field at the colon
        port = addr[length(addr)];  # Get the port number
        proto = $1;  # First field: Protocol (tcp/udp)

        # Set pidprog based on whether ss or netstat is used
        pidprog = ($0 ~ /users:/) ? $7 : "N/A";

        # Print the line with colored output and formatted columns
        printf "\033[1;34m%-8s\033[1;32m%-8s\033[0;31m%-40s\033[0;37m\t%s\n",
               proto,    # Protocol in blue
               port,     # Port in green
               pidprog,  # PID/Program name in default color
               $5;       # Address
    }'
}




#
# Dracula Theme for Bash Prompt with color codes
PROMPT_BLACK="\[\033[0;30m\]"
PROMPT_RED="\[\033[0;31m\]"
PROMPT_GREEN="\[\033[0;32m\]"
PROMPT_YELLOW="\[\033[0;33m\]"
PROMPT_BLUE="\[\033[0;34m\]"
PROMPT_PURPLE="\[\033[0;35m\]"
PROMPT_CYAN="\[\033[0;36m\]"
PROMPT_WHITE="\[\033[0;37m\]"
PROMPT_BRIGHT_BLACK="\[\033[0;90m\]"
PROMPT_BRIGHT_RED="\[\033[0;91m\]"
PROMPT_BRIGHT_GREEN="\[\033[0;92m\]"
PROMPT_BRIGHT_YELLOW="\[\033[0;93m\]"
PROMPT_BRIGHT_BLUE="\[\033[0;94m\]"
PROMPT_BRIGHT_PURPLE="\[\033[0;95m\]"
PROMPT_BRIGHT_CYAN="\[\033[0;96m\]"
PROMPT_BRIGHT_WHITE="\[\033[0;97m\]"
PS1="${PROMPT_BRIGHT_PURPLE}\u@\h${PROMPT_BRIGHT_GREEN} \w ${PROMPT_BRIGHT_BLUE}\$ ${PROMPT_WHITE}"

# Function to create a directory and then change into it
mkd() {
    # Checks if a directory name is provided
    if [ $# -eq 0 ]; then
        echo "Usage: mkd <directory>"
        return 1
    fi

    # Creates the directory and changes into it
    mkdir -p "$1" && cd "$1"
}

# Function to create a file and open it in nano
mkf() {
    # Checks if a file name is provided
    if [ $# -eq 0 ]; then
        echo "Usage: mkf <filename>"
        return 1
    fi

    # Creates the file and opens it in nano
    mkdir -p "$(dirname "$1")" && touch "$1" && nano "$1"
}
IT related RSS feed Published with Oblish

Keyboard shortcuts

/
Search
[
Previous page
]
Next page
?
Show this list
Esc
Close