{"id":41141728,"url":"https://github.com/lazyoracle/linux-cheatsheet","last_synced_at":"2026-01-22T18:50:29.087Z","repository":{"id":51355788,"uuid":"289402658","full_name":"lazyoracle/linux-cheatsheet","owner":"lazyoracle","description":"Cheatsheet of frequently used commands in Linux","archived":false,"fork":false,"pushed_at":"2023-08-10T21:49:46.000Z","size":65,"stargazers_count":5,"open_issues_count":0,"forks_count":5,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-10-26T11:04:17.148Z","etag":null,"topics":["awesome","cheatsheet","docker","git","linux-commands","rsync"],"latest_commit_sha":null,"homepage":"https://lazyoracle.github.io/linux-cheatsheet","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lazyoracle.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-08-22T02:14:29.000Z","updated_at":"2024-12-20T15:50:13.000Z","dependencies_parsed_at":"2024-07-27T20:13:36.970Z","dependency_job_id":null,"html_url":"https://github.com/lazyoracle/linux-cheatsheet","commit_stats":{"total_commits":50,"total_committers":3,"mean_commits":"16.666666666666668","dds":"0.040000000000000036","last_synced_commit":"641ee081a88d6befd04c64e990d4623dbf1b4999"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/lazyoracle/linux-cheatsheet","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lazyoracle%2Flinux-cheatsheet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lazyoracle%2Flinux-cheatsheet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lazyoracle%2Flinux-cheatsheet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lazyoracle%2Flinux-cheatsheet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lazyoracle","download_url":"https://codeload.github.com/lazyoracle/linux-cheatsheet/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lazyoracle%2Flinux-cheatsheet/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28668511,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-22T17:07:18.858Z","status":"ssl_error","status_checked_at":"2026-01-22T17:05:02.040Z","response_time":144,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["awesome","cheatsheet","docker","git","linux-commands","rsync"],"created_at":"2026-01-22T18:50:28.408Z","updated_at":"2026-01-22T18:50:29.079Z","avatar_url":"https://github.com/lazyoracle.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Collection of frequently used Linux commands\n\nHandy list of oft-used Linux commands that I will never remember. Not intended to be an all-purpose cheatsheet, highly specific to my limited usage. These commands work in `bash`. For `fish`, the variable substitutions and wildcards need to be changed.\n\n- [Collection of frequently used Linux commands](#collection-of-frequently-used-linux-commands)\n  - [Shell 101](#shell-101)\n    - [List directory contents with size](#list-directory-contents-with-size)\n    - [Count number of items in directory](#count-number-of-items-in-directory)\n    - [Find all files larger than a given size](#find-all-files-larger-than-a-given-size)\n    - [Run command on every file](#run-command-on-every-file)\n    - [Monitor GPU usage](#monitor-gpu-usage)\n    - [File permissions](#file-permissions)\n    - [Symbolic Links](#symbolic-links)\n    - [Process Management](#process-management)\n    - [fish](#fish)\n    - [wget](#wget)\n    - [Save current command to clipboard](#save-current-command-to-clipboard)\n    - [entr](#entr)\n  - [Networking](#networking)\n    - [Port Monitoring](#port-monitoring)\n    - [SSH 101](#ssh-101)\n    - [rsync](#rsync)\n  - [System Setup](#system-setup)\n    - [Essential Installs](#essential-installs)\n    - [Increasing swap](#increasing-swap)\n  - [Session management using screen](#session-management-using-screen)\n  - [Docker 101](#docker-101)\n  - [Git 101](#git-101)\n  - [make Basics](#make-basics)\n  - [Data Wrangling](#data-wrangling)\n    - [grep](#grep)\n    - [awk and sed](#awk-and-sed)\n      - [awk to print all columns from nth to last](#awk-to-print-all-columns-from-nth-to-last)\n      - [sed for removing and adding characters](#sed-for-removing-and-adding-characters)\n  - [Machine Learning Toolchain](#machine-learning-toolchain)\n    - [Install Tensorflow GPU](#install-tensorflow-gpu)\n    - [Disable Tensorflow debug messages](#disable-tensorflow-debug-messages)\n  - [Rust Toolchain](#rust-toolchain)\n    - [Adding Rust to Fish](#adding-rust-to-fish)\n    - [Adding Rust to VS Code](#adding-rust-to-vs-code)\n    - [Tab Completion](#tab-completion)\n  - [Youtube Downloader](#youtube-downloader)\n\n## Shell 101\n\n### List directory contents with size\n\n```bash\ndu -sch * .[!.]* | sort -rh\n```\n\n### Count number of items in directory\n\n```bash\nls \u003cdirectory path\u003e -1 | wc -l\n```\n\n### Find all files larger than a given size\n\n```bash\n# find all files \"-type f\" in the current directory \".\"\n# of size greater than 20MB and list the files using \"ls -lh\"\n# print the file size \"$5\" and the whole file name \"$0,2\"\nfind . -type f -size +20000k -exec ls -lh {} \\; | awk '{print $5 \":\"; $1=$2=$3=$4=$5=$6=$7=$8=\"\"; print substr($0,2)}' | sed -r 's .{9}  '\n```\n\n### Run command on every file\n\n```bash\nfor i in ./*txt; do echo \"File $i\"; done\n```\n\n### Monitor GPU usage\n\n```bash\n# Check GPU usage and update every 1 s\nwatch -n1 nvidia-smi\n```\n\n### File permissions\n\n```bash\n# 400 read by owner\n# 040 read by group\n# 004 read by anybody\n# 200 write by owner\n# 020 write by group\n# 002 write by anybody\n# 100 execute by owner\n# 010 execute by group\n# 001 execute by anybody\n\nsudo chmod XXX \u003cfilepath\u003e\n\n# ssh public key file 644 \n# ssh private key file 600\n# ssh directory 700\n```\n\n### Symbolic Links\n\n```bash\n# ln -s \u003cpath-to-target\u003e \u003cpath-to-link\u003e\n$ ln -s ~/Documents ./docs  \n$ ls -alh\ntotal 8.0K\ndrwxrwxr-x  2 anurag anurag 4.0K Mar 26 00:34 ./\ndrwxrwxr-x 24 anurag anurag 4.0K Mar 26 00:33 ../\nlrwxrwxrwx  1 anurag anurag   23 Mar 26 00:34 docs -\u003e /home/anurag/Documents/\n$ python docs/hello.py\nHello!\n```\n\n### Process Management\n\n```bash\n# Kill all processes with process name matching \u003cpattern\u003e\nps -ef | grep 'pattern' | grep -v grep | awk '{print $2}' | xargs -r kill -9\n```\n\n### fish\n\n```bash\n# Set password of user account on GCP VM\nsudo passwd $USER\n# set fish as default shell\nchsh -s `which fish`\n```\n\n### wget\n\n```bash\n# recursive no-parent Reject no-Host robots.txt X_no_of_dirs\nwget -r -np -R \"index.html\" -nH -e robots=off --cut-dirs=X \u003cdownload_URL\u003e\n```\n\n* Nifty recipes for common commands - [tldr.sh](https://tldr.sh/)\n* Some basic shell usage [examples](https://missing.csail.mit.edu/2020/shell-tools/)\n\n### Save current command to clipboard\n\nIn `zsh`: \u003ckbd\u003eCtrl\u003c/kbd\u003e + \u003ckbd\u003eU\u003c/kbd\u003e will cut the whole line (irrespective of your position). In `bash` and `fish`, it will cut backwards from your current position. \u003ckbd\u003eCtrl\u003c/kbd\u003e + \u003ckbd\u003eY\u003c/kbd\u003e will paste it back to the terminal. \n\n### entr\n\nUseful for watching file changes and running commands on files, e.g, \n\n```bash\nls *.md | entr -c glow todo.md\n```\n\nwill display the `todo.md` document in the terminal with `glow` and refresh every time the file is changed on disk.\n\n## Networking\n\n### Port Monitoring\n\n```bash\n# Get PID of a process running on port YYYY\nsudo netstat -lpn | grep :YYYY\n# OR\nfuser 8080/tcp (add -k to kill the process too)\n```\n\n### SSH 101\n\n```bash\n# connect to hostname with key and port forward\nssh -L \u003cremote_port\u003e:localhost:\u003clocal_port\u003e -i /path/to/key username@hostname\n```\n\n### rsync\n\n```bash\n# -avzPn --stats -h --\u003e archive verbose compressed progress-bar dry-run human-readable-stats\n\n# pull files from remote server\n\nrsync -avzPn --stats -h username@remote_host:/home/username/directory_we_want ~/local/directory\n\n# push files to remote\nrsync -avzPn ~/local/directory username@remote_host:/home/username/destination_dir\n\n# using ssh key\nrsync -avzPn -e \"ssh -i ~/ec2_keyfile.pem\" user@remote:/home/folder /tmp/local_system/\n\n# read file names from a list stored in another file - https://serverfault.com/a/212441\n# works better when source directory is set to root \"/\" and file names are stored with absolute path\n# source directory gets appended to the file paths inside the listfile\nrsync -avzP /source/directory --files-from=/full/path/to/listfile /destination/directory\n\n# with xargs for parallel execution - https://stackoverflow.com/a/25532027\n# This will list all folders in ~/data, pipe them to xargs, \n# which will read them one-by-one and and run 4 rsync processes at a time. \n# The % char replaces the input argument for each command call.\nls ~/data | xargs -n1 -P4 -I% rsync -Pa % myserver.com:~/data\n```\n\nMore details on [rsync cheatsheet](https://www.danielms.site/blog/rsync-cheatsheet/)\n\n## System Setup\n### Essential Installs\n\n```bash\nsudo apt install -y fish wget bzip2 curl git gcc g++ python3-dev build-essential vim nano rsync htop tree screen libatlas-base-dev libboost-all-dev libopenblas-dev software-properties-common\n```\n\n### Increasing swap \n\n```bash\n# create swap file\nsudo fallocate -l 1G /swapfile\n# permissions set to root\nsudo chmod 600 /swapfile\n# setup a linux swap area\nsudo mkswap /swapfile\n# activate swap\nsudo swapon /swapfile\n# verify\nsudo swapon --show\n```\n\n## Session management using screen\n\n```bash\n# detach a session\nCtrl + a + d\n\n# list all sessions\nscreen -ls\n\n# resume session\nscreen -r\n\n# run command and detach\nscreen -d -m \u003ccommand\u003e\n\n# add session name\nscreen -S \u003csession name\u003e\n\n# add name to attached session\nCtrl + a + : + sessionname name\n\n# attach to named session\nscreen -xS name\n\n# detach and attach to session (useful when stuck in ssh)\nscreen -d -r\n\n# attach to session without detaching\nscreen -x\n```\n\n## Docker 101\n\n```bash\n# docker-compose\ndocker-compose up --build\ndocker-compose down\n\n# build a docker image tagged my_image with context in the current working directory\ndocker build --tag my_image:1.0 .\n\n# run a detached container on 8080, remove after shutdown, with interactive pseudo-tty, named container_name\n# with $HOME/data mounted on $HOME/app/data, shared memory increased to 10GB, from image ubuntu:18.04\ndocker run -d -p 8080:8080 --rm --tty -i --name container_name --volume $HOME/data:$HOME/app/data --shm-size 10G ubuntu:18.04\n\n# remove all containers\ndocker container rm $(docker container ls -aq)\n\n# force remove all images older than 12 hours/until 2020-08-04\ndocker image prune -a --force --filter \"until=12h\"\ndocker image prune -a --force --filter \"until=2020-08-04T00:00:00\"\n\n# check docker resource usage\ndocker stats\n\n# Execute command in a running container in an interactive terminal\ndocker exec -it \u003ccontainer name\u003e \u003ccommand\u003e\n```\nMore at: [Docker Official Cheatsheet](https://www.docker.com/sites/default/files/d8/2019-09/docker-cheat-sheet.pdf) and [DevTools](https://phase2.github.io/devtools/)\n\n## Git 101\n\n```bash\n# setup local project with remote\ngit init\ngit add .\ngit commit -m \"init commit\"\ngit remote add origin \u003cremote repo url\u003e\ngit push --set-upstream origin master\n\n# setup local branch with remote\ngit checkout -b cool-feature\ngit add .\ngit commit -m \"branch init commit\"\ngit push --set-upstream origin cool-feature\n\n# change remote for your local repo, useful when synching your fork\ngit remote set-url origin \u003crepo url\u003e\n\n# set a new remote and use it to push/fetch \ngit remote add \u003cremote-name\u003e \u003cremote-url\u003e\ngit pull \u003cremote-name\u003e\n# for the first push\ngit push --set-upstream \u003cremote-name\u003e \u003cbranch-name\u003e \n\n# https://www.atlassian.com/git/tutorials/undoing-changes\n# https://stackoverflow.com/a/24569160\n\n# git reset\n# undo 2 commits and unstage files\ngit reset HEAD~2 # git reset --mixed HEAD~2\n\n# undo 2 commits and leave changes staged\ngit reset --soft HEAD~2\n\n# undo 2 commits and delete all changes\ngit reset --hard HEAD~2\n\n# git revert\n#Undo last 2 commits with new commit (without altering history)\ngit revert HEAD~2\n\n# git checkout\n# move around to a branch/commit\ngit checkout \u003cbranch\u003e or git checkout \u003ccommit-SHA\u003e\n\n# remove staged file from index\ngit rm \u003cfile-name\u003e\n\n# revert merge\ngit log #check the commit hash and parent id\n# revert working tree to commit-hash on parent branch 1\ngit revert \u003ccommit-hash\u003e -m 1\n\n# move commits to another existing branch\ngit checkout existingbranch\ngit merge master\ngit checkout master\ngit reset --hard HEAD~3 # Go back 3 commits. You *will* lose uncommitted work.\ngit checkout existingbranch\n\n# move commits to another new branch\ngit branch newbranch      # Create a new branch, saving the desired commits\ngit reset --hard HEAD~3   # Move master back by 3 commits or put a commit hash\ngit checkout newbranch    # Go to the new branch that still has the desired commits\n\n# alias for pretty log\ngit config --global alias.graphlinelog \"log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)\u003c%an\u003e%Creset' --abbrev-commit\"\ngit graphlinelog\n\n# Generating reports with git\ngit shortlog -sne --after=\"28 Feb 2021\" --before=\"01 Apr 2021\" # short list of commits sorted by contributors during a given period\ngit shortlog --after=\"28 Feb 2021\" --before=\"01 Apr 2021\" --merges # merge commits, replace shortlog by log for commit messages\ngit log --format=%B -n 1 \u003ccommit-hash\u003e # show commit message\ngit log --merges --first-parent master --pretty=format:\"%h %C(blue)%\u003c(15,trunc)%aN %C(white)%\u003c(22)%ar%Creset %C(green)%s\" # log of merge commits to master, with commit hash \u0026 author name \n\n# Saving changes without committing \n# https://stackoverflow.com/q/11269256\ngit stash push -m \"stash name\" # make a new stash\ngit stash list # list all available stashes\ngit stash show # show diff between stash and current state\ngit stash pop # apply stash and remove from stack\ngit stash apply # apply and keep stash in stack\ngit diff \u003e path-name.patch # save WIP diff as a patch\ngit apply --3way some.patch # apply a patch like a stash\n\n# worktree: Create a parellel directory to work on a separate feature\n# https://opensource.com/article/21/4/git-worktree\ngit worktree add -b hotfix ~/code/hotfix master\ngit worktree list\ngit worktree remove hotfix\n\n# Use delta for better git diff outputs\n# https://github.com/dandavison/delta\nbrew install git-delta\ngit config --global core.pager \"delta --line-numbers --dark\"\ngit config --global delta.side-by-side true\n\n# Find which commit added a specific file eg foo.cpp\n# https://stackoverflow.com/q/11533199\ngit log --follow --diff-filter=A --find-renames=40% foo.cpp\n\n# Squash all commits in a branch to a single commit without git rebase\n# https://stackoverflow.com/a/25357146\ngit checkout my_branch\ngit reset $(git merge-base main $(git rev-parse --abbrev-ref HEAD))\ngit add .\ngit commit -m \"one commit to rule them all\n\n```\n\n[Atlassian Git Tutorials](https://www.atlassian.com/git/tutorials)\n\nRepository restructuring without losing commit history:\n- [git-filter-repo](https://github.com/newren/git-filter-repo) - [Examples](https://htmlpreview.github.io/?https://github.com/newren/git-filter-repo/blob/docs/html/git-filter-repo.html#EXAMPLES)\n- [Merging repositories](https://gist.github.com/lazyoracle/9738dfccec631d94692a7d2300837b27)\n\n## make Basics\n\nMake `target` for dependencies `dep1` and `dep2` using recipe specified by `command`\n```\ntarget: dep1 dep2\n\tcommand arguments\n\ndep%: subdep1 subdep2\n\tcommand arguments\n```\n* [Metaprogamming - MIT Missing Semester](https://missing.csail.mit.edu/2020/metaprogramming/)\n* [`make` Standard Targets](https://www.gnu.org/software/make/manual/html_node/Standard-Targets.html#Standard-Targets)\n\n## Data Wrangling\n\n### grep\n\n```bash\n# find pattern in files and include NUM lines after and before search result\ngrep --include=\\*.{py, c, h} \\\n\t--exclude=\\*.o \\\n\t--exclude-dir={dir1,dir2,*.dst} \\\n\t-rnw '/path/to/somewhere/' \\\n\t-e \"pattern\" \\\n\t--after-context=NUM \\\n\t--before-context=NUM\n```\nMore `grep` funda [here](https://www.grymoire.com/Unix/Grep.html)\n\n### awk and sed\n\n#### awk to print all columns from nth to last\n\n```bash\n# print 5th column, set columns 1 to 8 as empty, then print all columns\n# https://stackoverflow.com/a/2961994\n\u003ccommand output with list of formatted strings\u003e | awk '{print $5 \":\"; $1=$2=$3=$4=$5=$6=$7=$8=\"\"; print substr($0,2)}'\n```\n\n#### sed for removing and adding characters\n\n```bash\n# add in the beginning of every line in file - https://stackoverflow.com/q/2099471\nsed -i -e 's/^/\\/home\\/users\\/anurag\\//' file\n\n# remove last character of every line in file\nsed -i 's/.$//' file\n\n# add :8080 at the end of every line in portsfile\nsed -i 's/$/:8080/' portsfile\n\n# REPLACE last character of each line in file with p3\nsed -i 's/.$/p3/' file\n\n# remove last character of every line in file\nsed -i 's/.$//' file\n\n# remove first two characters of every line in file\nsed -r 's .{2}  ' file\n```\n\nFor more tips, refer [here](https://missing.csail.mit.edu/2020/data-wrangling/).\n\n## Machine Learning Toolchain\n\n### Install Tensorflow GPU\nFor `tensorflow 2.3+`, NVIDIA driver `450.x` and CUDA `11.0`\n* Make sure the NVIDIA drivers and CUDA are already installed\n* Create a `conda` environment\n* `conda install -c anaconda cudatoolkit=11.0`\n* Follow instructions [here](https://docs.nvidia.com/deeplearning/cudnn/install-guide/index.html#package-manager-ubuntu-install) for CUDNN installation. Check [here](https://askubuntu.com/a/1050824) for errors with package keys.\n* `pip install tensorflow`\n* Verify as mentioned [here](https://www.tensorflow.org/guide/gpu#setup)\nMore details [here on Tensorflow Install](https://www.tensorflow.org/install/gpu#linux_setup)\n\n### Disable Tensorflow debug messages\n\nAdd the following **before** importing `tensorflow`\n```python\nimport os\nos.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'\n```\n\n## Rust Toolchain\n\n### Adding Rust to Fish\n```bash\n# https://github.com/rust-lang/rustup/issues/478#issuecomment-330467741\nexport PATH=\"$HOME/.cargo/bin:$PATH\" # add to ~/.config/fish/config.fish\n```\n\n### Adding Rust to VS Code\nAdd to `settings.json` as discussed [here](https://stackoverflow.com/a/66001600)\n\n```json\n{\n    \"rust-client.rustupPath\": \"~/.cargo/bin/rustup\"\n}\n```\n\n### Tab Completion\n```bash\n$ mkdir -p ~/.config/fish/completions\n$ rustup completions fish \u003e ~/.config/fish/completions/rustup.fish\n```\n\n## Youtube Downloader\n\n### Download a Playlist\n```bash\n# Download YouTube playlist videos as mp4 in separate directory indexed by video order in a playlist from start-index to end-index\nyoutube-dl -f mp4 -o '%(playlist)s/%(playlist_index)s - %(title)s.%(ext)s' \u003cplaylist-URL\u003e --playlist-start \u003cstart-index\u003e --playlist-end \u003cend-index\u003e\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flazyoracle%2Flinux-cheatsheet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flazyoracle%2Flinux-cheatsheet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flazyoracle%2Flinux-cheatsheet/lists"}