Skip to content

Instantly share code, notes, and snippets.

@absorber
Last active November 7, 2016 14:52
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 absorber/31bcf90793474118120c4c6859462103 to your computer and use it in GitHub Desktop.
Save absorber/31bcf90793474118120c4c6859462103 to your computer and use it in GitHub Desktop.
Random personal cheat sheets and notes about various Linux activities.

My Linux cheatsheets and other handy notes


  • If vim paste is messed up, then do :set paste

Oneliners, commands and scripts which are pretty cool but I don't understand yet (fully)

Get the output of grep --color=auto -ril "keyword" . and sort it according to modify date.

find . -type f -exec grep -q keyword {} \; -printf '%T@/%p\0' | sort -zn | while IFS=/ read -rd '' tsamp file; do printf 'timestamp: %s, file: %s\n' "$tsamp" "$file"; done

find -type f -print0 | sudo xargs -0 md5sum | grep -v isolinux/boot.cat | sudo tee md5sum.txt - As seen here - https://help.ubuntu.com/community/LiveCDCustomization#Assembling_the_file_system

man tmux | grep -B1 -E '\(alias:' | grep -vE '^-' | tr '()' ' ' | perl -pe 's/\s*\z/ / unless /alias:/' | awk '{printf "%s %s\n", $1, $NF}' | column -t | sort - See all tmux commands and aliases.

Prepending a timestamp to each line of output from a command

command | while IFS= read -r line; do printf '[%s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$line"; done
Attention: Sometimes however, you have to redirect stderr into it (using something like command 2>&1 | timeloop ...) - (Source)


Directory permissions in UNIX

  • read determines if a user can view the directory's contents, i.e. do ls in it.
  • write determines if a user can create new files or delete file in the directory. (Note that this essentially means that a user with write access to a directory can delete files in the directory even if he/she doesn't have write permissions for the file! So be careful with this.)
  • execute determines if the user can cd into the directory.

(Bash) Shell related

List only dirs and files starting with a . (dotfiles)

ls -d .[^.]*

diff

diff <(ls /tmp/folder1) <(ls /tmp/folder2) # diff output of 2 commands
diff -r /tmp/folder1 /tmp/folder2 # diff 2 dirs

CLI pastebin aliases

alias apaste='curl -F '\''paste=<-'\'' http://apaste.info/store'
echo just testing! | nc termbin.com 9999 - Needs nc to be present on the system.

Mounting (iso) images using CLI

mount -o loop disk.iso /mnt/disk - Source

badblocks -nsv -p2 -o ./badblocksresult.txt /dev/sdX

Badblocks is not perfect though. - (archive)

Watch progress of dd

watch -n 1 killall -USR1 dd

Find word1 and word2 regardless of order.

grep -i 'w1.*w2\|w2.*w1' <filename>

How behind is your git repo?

git fetch && git status | grep behind

tmux

  • Source .tmux.conf even when prefix key doesn't work tmux source-file ~/.tmux.conf
  • Add a UTF8 character to tmux window indicator tmux setw -g window-status-format "#(perl -C -E'\$_=shift, say chr( \$_ ? 0x245f + \$_ : 0x24ea)' #I) :#W"
  • tmux 2.0 vs. 2.1

Start a simple HTTP server using Python

python -m SimpleHTTPServer <port>
If port is unspecified, 8000 is default.

Print HTTP status code of a page

curl --head example.com | awk 'NR==1{ print $2 }'

Check filesize of URLs before downloading them

wget --spider URL
curl --head URL

SO source

Check weather in terminal only using curl (and wttr.in)

curl wttr.in/amsterdam curl wttr.in ### Automatically determines location based on IP address

Use nmap to scan all live hosts on your network:

sudo nmap -sP "192.168.1.*" - Works only if ICMP packets are allowed.

tar

Archive directory

tar -cvWf archive_name.tar dir_name

Extract archive

tar -xvf archive_name.tar

If you don't want to memorize all of that stuff, there's also dtrx (Do The Right Extract).

GPG

GPG encrypt

gpg -ac archive_name.tar

GPG decrypt

gpg -d archive_name.tar.asc > archive_name.tar


rsync

What's the best rsync command?

rsync -avPHSxh

How to rsync stuff from remote to local (requires rsync to be on both sides)

rsync -avPHSxh -e 'ssh -p 2222 -i .ssh/privkey' user@remote:/full/path /local/path/

The -p argument is if you're on a custom port, which you should be on.
Also, watch out for rsync over FUSE.

SSH

Generate secure SSH client keys.

ssh-keygen -t rsa -b 4096 -a 100 -f ~/.ssh/name_of_key -C <optional_comment> - partially modified from Stribika's Secure Secure Shell article

Find out the server's fingerprint info

ssh-keygen -lf /etc/ssh/ssh_host_rsa_key.pub - Source

The find command

This might be best illustrated with an example. Let's say that find turns up these files:

file1
file2
file3

Using -exec with a semicolon (find . -exec ls '{}' \;), will execute

ls file1
ls file2
ls file3

But if you use a plus sign instead (find . -exec ls '{}' \+), all filenames will be passed as arguments to a single command: ls file1 file2 file3

More explanation

Substitute string1 to string2 in every file in a given path

find path -type f -exec sed -i 's/string1/string2/' {} \;

How to exclude stuff from the find command.

Suppose you wanted to exclude 2 directories from searching:

find . -iname "*searchterm*" ! -path "./excluded_dir/*" ! -path "./excluded_part_of_dirname*"

Say you want to exclude some files ending in a format:

find . -iname "*searchterm*" ! -iname "*.html" ! -iname "*.js"



Distro / package manager specific

RPM / Yum

See changelog of a package

rpm -q --changelog glibc | head

See all history (what packages installed and when)

sudo yum history packages-info "*" | less

Specify packages installed for given repo

  • On most yum versions: yum list installed | grep @repo
  • On more recent yum versions: yumdb search from_repo repoid - Explanation

Clean package caches

sudo yum -v clean all

Deb / apt

For when add-apt-repository is not installed

Case: sudo: add-apt-repository: command not found

Solution: sudo apt-get install software-properties-common

How to check install size of packages (debian based)

dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -n | less

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment