Bash Basics

Common environment variables

$HOME $USERNAME $PWD $USER $PATH $PYTHONPATH

Common commands

  • chown
  • chmod
  • sudo
  • ps
  • grep
  • pwd
  • printenv
  • export
  • cat
  • login
  • shutdown
  • reboot
  • diff
  • rclone
  • cd
  • mv
  • rsync
  • scp
  • ssh
  • echo

close bash without saving history

unset HISTFILE && exit

or

history -c && history -w && exit

https://www.cyberciti.biz/faq/clear-the-shell-history-in-ubuntu-linux/

sudo

to run something as root

sudo <command> [command options]

to run interactively as root

sudo -i

to run something as someone else

sudo -i -u <username>

run exit to leave that session

Find/Replace

To change the file in place:

sed -i "s/regex/replace/" file

or

sed -i "s|regex|replace|" file

To copy output to a new file

sed "s/regex/replace/" filein > fileout

Recursively chown

from here

#chown -R <owner> <folder_1> <folder_2> ... <folder_n>
chown -R user /home/user

Misc

Count files in bash

source: https://devconnected.com/how-to-count-files-in-directory-on-linux/

ls | wc -l

octal file permissions

stat /path/to/filename
stat -c '%A %a %n' /path/to/filename
printenv | grep ROS

tail

From here

see the last few lines

#tail -100 <filename>
tail -100 history.txt

see real-time changes to files as they get appended:

tail -f history.txt

see line 196-200 of a file

head -200 history.txt | tail -5

Recursively list files

from here

tree /path/to/dir

Awk

awk '{print $1}' <filename>
cat <filename> | awk '{print $1}'
awk -E , '{print $1}' <filename.csv>