Skip to content

Instantly share code, notes, and snippets.

@ruandre
Last active February 8, 2022 05:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruandre/632eb8d5c6a1b2c38237eee1047c6b22 to your computer and use it in GitHub Desktop.
Save ruandre/632eb8d5c6a1b2c38237eee1047c6b22 to your computer and use it in GitHub Desktop.
Linux Shell Tips (Bash, etc.)

Shell Tips

Mostly "notes to self" as I occasionally forget...

System info

uname -a

Search for a process

ps aux | grep [something]

Immediately terminate a process

kill -9 [pid]

(For logs) cat a gzip file

zcat [fileName.gz]

Extract .tar.gz

Flags roughly: -x extract, -v verbose, -z gzip, -f file

tar -xvzf [fileName.tar.gz]

Count words in file

wc -w [fileName]

Count lines (pipe into)

cat [fileName] | wc -l

Sort lines (pipe into)

cat [fileName] | sort

Tree ignoring directories

tree -I '.git|node_modules'

Hostname

hostname # server domain
hostname -i # ip
hostname --fqdn # fully qualified domain name

Username associated with current effective user id

whoami

Delete specific folders recursively (node_modules in this case)

find . -name "node_modules" -type d -prune -exec rm -rfv '{}' +

Search file content, for example:

-Rin ~ recursive, ignore case, line number (-l instead of -n for file name only)

grep -Rin "text-to-find" --include=\*.js --exclude-dir={.git,node_modules} .

Can also use:

--exclude=\*.jpg
--include=\*.{css,html}
--exclude-dir={dir1,dir2}

Copy file into sub-directories (one level deep)

Credit: https://askubuntu.com/a/300763

for d in */; do cp someFile.txt "$d"; done

zip of latest commit on current branch

Excludes node_modules via .gitignore, for example

git archive -o latest.zip HEAD

Add to path

In ~/.profile or ~/.bashrc

export PATH="$HOME/.local/bin:$PATH" # adds to start
export PATH+=":$HOME/.local/bin" # adds to end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment