Bash Basics
Arguments
short arguments usually have a single dash followed by a single character. An example is ls -a, where -a is an additional flag/attribute that indicates to the ls command that you should list all files, even if hidden by a .
long arguments usually have two dashes followed by a word or phrase (connected by dashes). An example is ls --all, where --all has the same meaning as -a above.
Getting help
<command> -h: display help<command> --help: display help (long form of argument)man <command>- or you can go to https://explainshell.com
Most Common Commands
cd <absolute or relative path>: change directory to the path indicatedls: list files in current directoryls -la: list all files(a) in list format(-l)chown <username>:<groupname> <file or folder>: change the owner of the file or folderchown -R <username>:<groupname> <file or folder>: change the owner of the folder recursivelychmod <0-7><0-7><0-7> <file or folder>: change the owner:group:anyone permissions for a filechmod -R <0-7><0-7><0-7> <file or folder>: change the owner:group:anyone permissions for a folder recursivelychmod +x <file or folder>: add execution permission to a file/foldersudo+<command>mkdir <path>: make a directory at path indicatedmv <path1> <path2>: move a file from first path indicated to second path indicatedcp <path1> <path2>: copy files from first path indicated to second path indicatedcp -r <path1> <path2>: copy files recursively from first path indicated to second path indicatedrmdir <path>: delete a folder at a given path (folder must be empty)rm <path1> ...: delete a file at a given path(s)rm -rf <path> ...: delete a file/folder at a given path(s) using theforceandrecursiveoption (removes folders too, be careful!) | *
Keystrokes
| key combineation | meaning |
|---|---|
ctrl+c |
stops execution of whatever is running in bash |
ctrl+alt+t |
opens up a new bash terminal window |
ctrl+shift+t |
opens up a new bash tab |
ctrl+shift+w |
closes the current bash tab |
ctrl+shift+q |
closes all bash tabs |
Common environment variables
$HOME$USERNAME$PWD$USER$PATH-
$PYTHONPATH -
printenvlist all current environment variables exportwill save an environment variable across sessions
Other common commands
chownchmodsudopskillkillallhtopgreppwdprintenvexportcatloginshutdownrebootdiffrclonecdmvrsyncscpsshechomkdirrmrmdircptouchlnchgrp
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
#chown -R <owner> <folder_1> <folder_2> ... <folder_n>
chown -R user /home/user
Misc
Count files in bash
ls -l1 | wc -l
octal file permissions
stat /path/to/filename
stat -c '%A %a %n' /path/to/filename
Print your environment variables
printenv | grep ROS
tail
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
tree /path/to/dir
Awk
awk '{print $1}' <filename>
cat <filename> | awk '{print $1}'
awk -F , '{print $1}' <filename.csv>
close bash without saving history
unset HISTFILE && exit
or
history -c && history -w && exit
External Resources
- https://cheatography.com/davechild/cheat-sheets/linux-command-line/
- from here
- From here
- source: https://devconnected.com/how-to-count-files-in-directory-on-linux/
- from here
- https://www.cyberciti.biz/faq/clear-the-shell-history-in-ubuntu-linux/
- https://www.geeksforgeeks.org/linux-commands-cheat-sheet/