Source

less tips

less [file]
# type "F" - similar to `tail -f`
# type "v" - open in $EDITOR
# type &pattern: show only lines matching "pattern"

Shell redirection

  • Linux Sysadmin Basics 04

  • STDIN = 0

  • STDOUT = 1

  • STDERR = 2

  • > redirects STDOUT to file - OVERWRITES

  • >> redirects STDOUT to file - APPENDS

  • | redirects STDOUT from one program to STDIN of another program

  • < redirects file or output to STDIN

# redirect STDOUT - can shortcut with > only
echo "this should be in a file" 1> file.txt
 
# redirect STDERR
ls thisfiledoesntexist 2> errfile.txt
 
# silent - redirect both STDOUT and STDERR to /dev/null
find . -name somename 2&>1 /dev/null
 
# redirect contents of a file to STDIN
mail -s "subject" user[@domain] <message.txt

Changing sudo group

Changed admin group from sudo to wheel - to get rid of the annoying home directory file (.sudo-as-admin-successful)

getent group wheel # should get no output
sudo groupadd -r wheel
sudo usermod -aG wheel p10
# logout then log back in
id # should be part of wheel group now
sudoedit /etc/sudoers
# add the following line: %wheel ALL=(ALL:ALL) NOPASSWD: ALL
# save and exit
sudo gpasswd --delete p10 sudo
# logout then log back in
id # should not be part of sudo group now
rm .sudo_as_admin_successful
sudo apt update
# .sudo_as_admin_successful file should no longer be automatically created
sudoedit /etc/sudoers
# comment out the following line: %sudo ALL=(ALL:ALL) NOPASSWD: ALL

SSH tunneling / port forwarding

ssh -L XXXX:localhost:YYYY username@ZZZ.ZZZ.ZZZ.ZZZ
  • where to run command: local machine
  • XXXX: port on local host
  • localhost: local ip you’ll type into address bar (just use localhost)
  • YYYY: listening port on ssh server / remote machine
  • username@ZZZ.ZZZ.ZZZ.ZZZ: remote server user and ip / domain name

example:

# opens a connection to remote Syncthing web GUI on localhost port 9001
# run this on the local machine (-vvv for verbose to troubleshoot)
ssh [-vvv] -L 9001:localhost:8384 user@domain
 
# connect via web browser: 127.0.0.1:9001

Learning git

# list all tracked files
git ls-files
 
# list committed files
git ls-tree [-r master] [--name-only]
 
# push to remote
git push --set-remote origin master

fixing “repo within a repo”:

git rm -f --cached [dir-with-subrepo]`

Using spaces in bash aliases

you have to escape slashes that escape spaces within the alias quotes:

  • e.g. obs="cd /mnt/c/Users/User/OneDrive\\ -\\ Company\\ Inc/..." \

EOF