Skip to content

Bash

Cheatsheet

View content of zip file

$ zipinfo foo.zip

Find total number of files in current directory

$ ls | wc -l

Install termite terminal

curl "https://raw.githubusercontent.com/Corwind/termite-install/master/termite-install.sh" | sh

Install Dropbox

$ cd ~
$ wget -O dropbox.tar.gz "http://www.dropbox.com/download/?plat=lnx.x86_64"
$ tar -tzf dropbox.tar.gz
$ tar -xvzf dropbox.tar.gz
$ ~/.dropbox-dist/dropboxd

Install Dropbox Cli

$ mkdir -p ~/bin
$ wget -O ~/bin/dropbox.py "https://www.dropbox.com/download?dl=packages/dropbox.py"
$ chmod +x ~/bin/dropbox.py

Find text in directory

$ grep -Ril "text-to-find-here"
  • i stands for ignore case (optional in your case)
  • R stands for recursive
  • l stands for "show the file name, not the result itself"
  • / stands for starting at the root of your machine

Make teamspeak run from dmenu

$ sudo ln -s $HOME/Downloads/TeamSpeak3-Client-linux_amd64/ts3client_runscript.sh /bin/teamspeak3

Download yt from list in file

#!/bin/bash
filename="$1"
while read -r line
do
  youtube-dl $line
done < "$filename"

Download youtube video as mp3

$ youtube-dl -x --audio-format mp3 "<link>"

Copy command contents to clipboard

$ cat file | xclip

To paste the copied text, you use:

$ xclip -o

Using sed to replace curly braces

$ sed -i 's#{test1}#test2#' file

You cannot escape curly braces in sed.

Recursively delete all files of specific extension in current directory

$ find . -name "*.bak" -type f -delete

To see which files will be deleted beforehand, you can use

$ find . -name "*.bak" -type f

Make sure -delete is the last argument. Otherwise, it will delete everything.

Reverse mouse scrolling

$ xmodmap -e "pointer = 1 2 3 5 4 7 6 8 9 10"

Watch file and re-compile

This requires entr to be installed.

$ ls *.tex | entr latex filename.tex

Turn of network connection

$ nmcli networking off

To enable it, replace off with on.

Remove everything except one file

$ ls | grep -v file.txt | xargs rm

-v, --invert-match Matches everything except the specifed file.

Run steam apps from the terminal

$ steam steam://rungameid/363890

The ID can be found https://steamdb.info/apps/.

Search and replace using grep and sed

$ grep -rl matchstring somedir/ | xargs sed -i 's/string1/string2/g'

Invert mouse scrolling

$ "xinput set-prop "USB Optical Mouse" "libinput Natural Scrolling Enabled" 1"

Refresh audio

$ sudo alsa force-reload´╗┐

Concat all files in folder

$ cat * > filename

Get word count in a file

$ wc -w filename

Filter out tree command

$ tree -I "*.pdf|*.log"

Logout from terminal

$ sudo pkill -KILL -u waynee95

Run command on all files

$ find ./**/*.nix | xargs nixfmt -c

Pipe result of command into Vim

$ echo "hello world!" | vim -

Find command

Find all .txt files recursively starting in current directory.

$ find . -name "*.txt"

To limit the depth use -maxdepth n, where n=1 would be current directory only.

To ignore casing, use -iname instead.

Find directory with certain name.

$ find -type d -name "gradle"

Find all haskell files and print the file contents.

$ find . -maxdepth 3 -name "*.hs" -exec cat {} \;

Download list of PDFs from website using wget

$ wget -m -p -E -k -K -np http://site/path/

Count all lines of code inside a folder

$ find . -name "*.js" | xargs wc -l

Last update: November 23, 2020