Skip to content

Instantly share code, notes, and snippets.

@Atcold
Last active July 25, 2022 17:48
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save Atcold/8942379 to your computer and use it in GitHub Desktop.
Save Atcold/8942379 to your computer and use it in GitHub Desktop.
Unix shortcuts

My Unix shortcuts and tips

Bash

List fields in awk's first matching like

awk '/<pattern>/{for (i = 1; i <= NF; ++i) print i, $i; exit}' <fileName>

Find net dev, address, gateway, mask and DNS

To find out your current network devices' status

nmcli device status

To find out all the useful numbers about our <net dev> (say, eth0)

nmcli device show <net dev> | grep -e IP4.DNS -e IP4.GATEWAY -e IP4.ADDRESS

The IP4.ADDRESS[1] field carries information about the subnet mask too. If we prefer a more extended version

ifconfig <net dev> | awk '/Mask:/{print $4}'

Use Jupyter notebook remotely

After having run jupyter notebook on the remote machine R, keep track of the remote port R_port (e.g. 8888) and Jupyter token J_token (e.g. a1b2c3d4). On your local machine L bind a L_port (e.g. 9000) to where Jupyter is running at on R.

l@L ~ $ ssh -NfL L_port:localhost:R_port r@R

Finally, on L, go to http://localhost:L_port/?token=J_token, and you should be able to use the notebook remotely. To close the ssh tunnel, you can ps aux | grep ssh, take note of ssh_PID and kill ssh_PID.

Display a CSV file

column -ts, <fileName.csv>

Or, from Vim

:read !column -ts, <fileName.csv>

How to list disks and partitions

All users:

sudo lsblk -o NAME,FSTYPE,SIZE,MOUNTPOINT,LABEL

Current user (FSTYPE and LABEL available only with sudo):

lsblk -o NAME,SIZE,MOUNTPOINT

Automount a device (like Nautilus)

udisks --mount <deviceName>

or

udisksctl mount -b <deviceName>

Searching a string within a directory

grep -Inri "<keyword to search for>" .

Show all files' name and content

find . -type f -exec sh -c "echo '{}'; cat {}; echo '\n'" \;

Show how many files each folder contains

find . -maxdepth 1 -type d -exec sh -c "echo {} \`ls {} | wc -l;\`" \;

or, better

ls -d */ | while read dir; do
    echo "$dir: $(ls $dir | wc -l)"
done

Groups

List groups for user

groups <user>

Add new group

groupadd <group>

Add user to (secondary) group or to another user's user-group

usermod -aG <group> <user>

or

adduser <user> <group>

Group belongness inheritance

chmod g+s <folder>

Accessing permission for folders

find <folder> -type d -exec chmod g+s {} +

Ssh without password

a@A ~ $ ssh-keygen -t rsa
a@A ~ $ ssh b@B mkdir -p .ssh
a@A ~ $ cat ~/.ssh/id_rsa.pub | ssh b@B 'cat >> .ssh/authorized_keys'
a@A ~ $ ssh b@B

Killing a process

ps -a | grep <program to kill>
kill 9 <ID of program to kill>

Checking size

du -chs *

Check free space

df -h [<path>]

Get info on process (RAM, time)

/usr/bin/time -v <running instruction>

Tar and untar

tar -czf myTar.tar.gz <files>
tar -xf myTar.tar.gz

Add -v for verbose output.

Logout users

who -u
kill "pid"

Ubuntu packages

List packages apt knows about, starting with <packagePrefix>

apt-cache pkgnames <packagePrefix>

To know more, e.g. whether the packages are installed,

apt list <packagePrefix>*

Know what a package installs

dpkg -L <packageName>

Discover what shared objects are used

lld <library/executable>

Git

Reset HEAD to origin/master

git fetch origin
git reset --hard origin/master

Reset repo to HEAD

git reset --hard

Tracking history

git log --decorate --graph -p -- fileName
git blame fileName

Remove untracked files (and directories) (and ignored files -x)

git clean -df

If the repo has sumbodules, then

git submodule foreach git clean -df

Full diff, word by word

git diff --word-diff

or, on older versions,

git diff --color-words -U9999999

Pruning on-the-fly

git fetch -p

Forget/remember to track files

git update-index --assume-unchanged <file>
git update-index --no-assume-unchanged <file>

For example, to loose track of the status of your notebook you can git update-index --assume-unchanged notebook/*.ipynb. To list all assumed unchanged files type

git ls-files -v | egrep '^h'

Follow history of a file (even if renamed)

git log --follow <fileName>

Locally ignore files

Edit .git/info/exclude as you would for .gitignore.

The diff comand

  • git diff: what you can still add to the index;
  • git diff --cached: what you have on the index vs. what's committed;
  • git diff HEAD: everything that has not been committed (working dir + index);
  • git diff <commit>: working tree − <commit>;
  • git diff --cached <commit>: index − <commit>;
  • git diff <commitFrom> <commitTo>: <commitTo><commitFrom> (chronological order!);
  • git diff <commitFrom>..<commitTo>: same;
  • git diff <commitFrom> <commmitTo> --stat: summary of changes;
  • git diff <commitFrom> <commmitTo> -- <filename>: changes per <filename> only.

add, commit, reset and checkout commands

  • git add <filename>: copies a file from the working dir to the index;
  • git commit ...: copies a file from the index to (a new) HEAD;
  • git reset <filename>: copies a file from HEAD to the index (i.e. removes file from index);
  • git checkout <filename>: copies a file from the index to the working dir (i.e. removes local changes);
  • git checkout -- '*.c': copies all C files (even removed one) from the index to the working dir.

Vim

List all recognised syntaxes

:setfiletype CTRL-D

Removing trailing whitespaces

:%s/\s\+$

Pasting yanked text while in insert mode

<c>-R 0

Exploring file's location / split and explore

:Ex
:Sex

Show command and search history

q:
q/

Find within a project (excluding binaries)

:grep -rI "<text>" .
:cw

Vimdiff

Sourcing correct file during conflict

:diffg RE/BA/LO

Previous and next conflict

[c
]c

Tmux

Move windows around

move-window -t <nb>

Attach and disattach others

tmux attach -d

Mac

Show and hide hidden files in Finder

defaults write com.apple.finder AppleShowAllFiles TRUE
defaults write com.apple.finder AppleShowAllFiles FALSE
killall Finder

FFmpeg

Get a video out of images

ffmpeg -framerate 30 -i <path>/%03d.png -pix_fmt yuv420p -filter:v "crop=floor(iw/2)*2:floor(ih/2)*2" video.mp4

LaTeX

Documentation

texdoc <package name>

Update distribution

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