Console snippets

Jan 29, 2024 · 4 min read
Table of Contents

The console offers a powerful way to interact with your system and increase your productivity.
Most snippets work in both Linux and MacOS terminals.
Here are some generic snippets to get you started:

Basic commands

  • cd <directory>: Change directory. (e.g. cd Documents)
  • pwd: Print Working Directory (shows your current location)
  • ls: List directory contents (-lah –color for details and color)
  • mkdir <directory>: Create a new directory

File manipulation

  • touch <filename>: Create an empty file
  • cat <filename>: Display file contents
  • cp <source> <destination>: Copy a file
  • mv <source> <destination>: Move/Rename a file
  • rm <filename>: Delete a file (use with caution!)

Permissions

  • chmod <permissions> <filename>: Change file permissions (owner, group, others)
  • chown <owner>:<group> <filename>: Change file owner (with optional group)
  • chgrp <group> <filename>: Change file group
  • sudo <command>: Run a command as superuser (root)
  • su - <username>: Switch to another user account

System Information

  • whoami: Shows your current username
  • w: Shows who is logged in and what they are doing
  • df: Show disk usage (-hT human-readable with filesystem type)
  • blkid: Show block device information
  • free -h: Show available memory
  • uname -a: Show detailed system information
  • lscpu: Show CPU information
  • lsb_release -a: Show Linux distribution information

Process Management

  • ps aux: List all running processes
  • top: Monitor system resources and processes
  • kill <PID>: Kill a process by its ID (-9 for force kill)

Help and Man Pages

  • man <command>: Get help on a specific command (detailed manual page)
  • help: Get a list of built-in shell commands
  • whatis <command>: Get a brief description of a command
  • alias : List all aliases

Bonus

  • history: Show command history (-c to clean)
  • !!: Repeat the last command (sudo !! to repeat last command as sudo)
  • exit: Exit the terminal (or CTRL-D)

These are just a few basic snippets. There’s a vast amount of functionality available in the Linux console. Explore the man pages for in-depth information on specific commands.

One liners

Console one-liners are powerful commands that condense complex tasks into a single line

File and directory manipulation

Search and replace text across all files in a directory:

find . -type f -exec sed -i 's/old_text/new_text/g' {} \;

Count lines in all files within a directory:

find . -type f -exec wc -l {} \; | awk '{sum += $1} END {print sum}'

Change permissions of all files in a directory (recursively):

find -type f -exec chmod 644  {} \;

Change permissions of all directories in a directory (recursively):

find -type d -exec chmod 755  {} \;

Convert all JPGs in current directory to PNGs:

for file in *.jpg; do convert "$file" "${file/.jpg/.png}"; done

Extract all ZIP files in a directory with correct folders

find -type f -name *.zip -execdir unzip -o '{}' \;

System Administration

List all processes using more than 10% CPU:

ps aux | awk '{if ($3 > 10) print $2}'

Find the largest file in a directory:

find . -type f -exec du -s {} \; | sort -nr | head -n 1

Network Tools

Ping a website and display response time:

ping -c 3 google.com | grep time | awk '{print $7}'

Download a file from a URL:

wget https://example.com/file.txt
#or
curl -O https://example.com/file.txt
  • Scan a network for active hosts:
nmap -sn 192.168.0.0/24
  • Scan many networks for active hosts and ports:
nmap  192.168.33.0/24 192.168.42.0/24

Remember: Be cautious when using one-liners, especially those involving deletion or modification. Test them on a small scale before applying them broadly.

These are just a few examples. Explore the web for more advanced one-liners using tools like grep, awk, and sed for powerful text manipulation.

Specific snippets

Find and delete .DS_Store files:

find . -type f -iname .DS_Store -exec rm -i {} \;

Resize tmpfs partition

mount -o remount,size=4G,noatime /tmp

Fix TMPDIR=/tmp cannot hold executables (partition possibly mounted with noexec)

sudo mount -o remount,exec /tmp

Update secureboot keys after NVIDIA driver upgrade (for Ubuntu with secureboot enabled / dual boot)

sudo update-secureboot-policy --enroll-key

Update outdated pip packages

pip list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1  | xargs -n1 pip install -U

Change display brightness with xrandr

xrandr --output eDP --brightness 0.6

Turn keyboard Scroll Lock on

xset led named "Scroll Lock" on

Update all git repositories under a folder, recursively

find . -mindepth 1 -maxdepth 1 -type d -exec git  --git-dir={}/.git --work-tree=$PWD/{} pull \; 

Miscellaneous tips

For the best terminal filemanager check the post:
MC: a commander like no other

Use Ctrl + R to search through your command history

Use Ctrl + C to cancel a running command

tmux and htop are great tools for managing terminal sessions and monitoring system resources

ncdu is a great tool for analyzing disk usage