Keyboard shortcuts
ctrl + u: delete everything behind cursor
ctrl + w: delete word behind cursor
ctrl + k: delete everything in front of cursor
ctrl + a: move cursor to beginning
ctrl + e: move cursor to end
ctrl + c: cancel
ctrl + i: autocomplete command options (ex: grep ——<press ctrl + i>)
alt+left/right arrow: jump back/forward one word
Commands
cd –: go back to previous path
cd: (no directory) goes to home folder
apropos < keyword >: search for command (ex: apropos “locate the binary” to find “whereis” command)
man < command >: manual for command (ex: man “apropos”) opens in less
< command > ——help: summary for most commands (ex: file ——help)
su < username >: switch to < username >
sudo -i: switch to root
sudo -k: end/expire sudo session
which < command >: find where < command > is located
head -n#: displays first # lines of file (# defaults to 10 if not specified)
tail -n#: displays last # lines of file (# defaults to 10 if not specified)
grep -in “needle” haystack.txt: search for “needle” (ignore case) in haystack.txt and print line numbers
-i: ignore case
-v: only print lines that match results
-n: print line numbers
less +G: open less at the last line
less +F: open less with forward forever (live update)
ctrl + c then q to exit
sort -u < file >: lists unique lines from < file > (if there are duplicates it will only show once)
df -h: get free space (-h for human readable)
nano
nano is a text editor. It’s a more user friendly than vi(m). There’s no wrong answer.
Open files with nano < filename >
ctrl + w: search (“where”)
ctrl + v: move down 1 screen
ctrl + y: move up 1 screen
ctrl + x: exit
less
less is a file viewer based on more (seriously)
Open files with less < filename >
Navigating in less
h: help
q: quit
e: forward one line
y: backward one line
f: forward one window
b: backward one window
shift + F: forward forever (live update)
g: go to first line
shift + G: go to last line
Permissions in ls
– < rwx for user > < rwx for group > < rwx for others >
– can be D for directory, L for link, – for files
aaron@ubuntu-test:~$ ls -l file.sh -rwxr-xr-- 1 aaron aaron 0 Oct 23 17:11 file.sh
–rwxr-xr——: user can read write and execute
-rwxr-xr——: group can read and execute
-rwxr-xr——: others can read
Setting Permissions
You can set permissions with chmod < permissions > < filename > .
The chmod command only sets permissions, it can not set the group or owner. Use chown for that.
You can use -R to set permissions recursively.
chmod < permissions > < filename >
There are multiple ways to state the permissions with chmod.
Both methods accomplish the same goal. Use whichever one you find easier.
Symbolic
Symbolic uses characters for the class(es), an operator, and mode(s).
chmod < class > < operator > < mode > < filename >
Classes
u: the user that owns the file
g: the group that owns the file
o: other users not in the classes above
a: all users
(blank): changes will apply to all classes if missing or blank
Operators
+: add mode to classes
–: subtract mode from classes
=: set (replace) mode to classes
Modes
There are a few other modes but these are the most common.
r: read
w: write
x: execute
Symbolic Examples
Set (replace) permissions for user (file owner) to read (no write/execute)
chmod u=r test-file.txt
chmod u=r test-file.txt
Add write permission for user (file owner)
chmod u+w test-file.txt
chmod u+w test-file.txt
Add execute permission for user (file owner) and group
chmod u+x,g+x test-file.txt
chmod u+x,g+x test-file.txt
You could also do it this way.
chmod ug+x test-file.txt
chmod ug+x test-file.txt
Leaving the class blank by using “+x” will add execute to all classes
chmod +x test-file.txt
chmod +x test-file.txt
Numerical
There are 3 (technically 4) digits that represent each class.
This can be confusing because there is no space between the numbers.
Think of 0754 as zero seven five four instead of seven hundred fifty four.
From left to right: 0 user group others
7 for the user
5 for the group
4 for others
The next part requires some math. Each class can have a mode with the maximum value of up to 7. You get this value by adding up each mode’s value.
Mode values
Execute: 1
Write: 2
Read: 4
Let’s start with an easy one. 7 is the maximum value and gives read write execute.
4 (read) + 2 (write) + 1 (execute) = 7 (rwx)
5 is read and execute
4 (read) + 1 (execute) = 5 (wx)
If you only want one mode (usually read), you’d just use that number.
4 (read) = 4 (r)
Note that I didn’t add 0s in that example for the other modes. If you don’t want the mode included leave it out of your math.
Numerical cheat sheet
I’ll never remember those numbers either. Here’s a cheat sheet.
Once you get to 4 you can appreciate why they chose these values.
2 + 2 = 4 but you can’t have write permissions twice. There is no 3 so there’s only one way to get 5.
7: 4 (read) + 2 (write) + 1 (execute) = 7 (rwx)
6: 4 (read) + 2 (write) = 6 (rw)
5: 4 (read) + 1 (execute) = 5 (rx)
4: 4 (read) = 4 (r)
3: 2 (write) + 1 (execute) = 3 (wx)
2: 2 (write) = 2 (w)
1: 1 (execute) = 1 (x)
0: none (—)
Final Value
Once you have your 0-7 digit for each group put them next to each other (don’t add) to get your number.
chmod 754 test-file.txt
chmod 754 test-file.txt
7 (rwx) for the user
5 (rx) for the group
4 (r) for others
output redirection
1> file.txt: send standard output (not errors) to file.txt (overwrite)
>: same as “1>”
2> file.txt: send error output to file.txt (overwrite)
>> file.txt: send output to file.txt (append)