NOTE: The following glossary is only the commands we can or will use in the training, and not the terms. For a glossary of terms, see this page. These are ordered non-alphabetically, and more by where they approximately are in the order of fundamental commands to grasp.
ls - list contents of target directory or (with no arguments) list contents of current directory.
Useful options:
-l = 'long' list, separates output into individual lines for each object and includes relevant info such as permissions, timestamp, ownership, and approximate size (for files)
-p = print a / (slash) character at the end of any output that is a directory.
-h = When used with the -l option, use units: Byte, Kilobyte, Megabyte, Gigabyte, Terabyte and Petabyte in order to reduce the number of digits when showing sizes.
-t = sort by time modified, descending, with most recently modified objects at the top.
-r = reverse the order of the sort, pairs well with -t to make freshest timestamp files appear on bottom (useful for very populated folders with many lines of output)
-a = display all files, including "dot" files (object name starts with a dot '.') which denote hidden objects such as .forward --and note that folders can also be hidden too.
cd - change directory; with no arguments will send you to your home folder. If specified, will change directory to target defined as an argument.
Very good command to practice tab completion.
Very good command to practice relative directory references (. and ..) as well as history expansion (!$ or the 'most recent argument' from your previous command).
Recall that '.' denotes your current working directory, and '..' is your current directory's parent directory.
e.g.:
cd /unix101/clues/intel
# Command above places your terminal at that directory. Start typing 'cd /uni' and press tab to practice tab completion.
cd .
# keeps you in the current directory (essentially does nothing).
cd .. moves you up one level to the parent directory: /unix101/clues
pwd - print working directory; outputs the absolute path where your command line prompt currently resides. This matters for when you want to reference relative to where you are, rather than typing the absolute path for everything. If you cd too many times and forget where you are, pwd is there to help.
cd /unix101/clues
pwd
# Output:
/unix101/clues
man - opens the documentation of a Unix command as written by its author(s) and often gives examples of it. Not sure how to use it? Try running the following, funny as it is:
man man
cp - copy; allows you to copy a specified object [arg1] to a new location [arg2]. Good for backing up a file before modification.
e.g.:
cp myFile.txt /unix101/backup/myFile_backup.txt
Copying populated directories with multiple files: use the -R (recursive) flag.
cp -R /unix101/secretformula /unix101/backup
IMPORTANT: it goes without saying that when you copy something, there is now a second version of it. This comes with some necessary downsides that should be understood:
The copied data is now taking up 2x the original storage space unless one copy is modified to be compressed or has data removed from it.
You now have to keep track of which one is which and ultimately decide what to do with both copies and where they will reside, unless one copy is deleted.
cat - concatenate; prints the entire contents of a file that has any sort of plain-text data. Will not work in a meaningful way on anything encoded differently like an image or .exe file, but is good for viewing an entire .txt or .log or script file.
Very good command to practice with grep using pipe [ | ] special expression.
e.g.:
cat /unix101/readme.txt
grep - searches for a specified pattern [arg1] within [arg2]. Has useful options for further refinement. Hugely important command for convenient information gathering from something with lengthy output or dense files. Examples:
grep -i brian /etc/passwd
This searches the file '/etc/passwd' for all lines including the substring 'brian' with no regards to upper/lower case (-i flag). Will also show anyone named Briana.
When used with a pipe [ | ] character, can filter the output of a different command. e.g.:
cat /etc/passwd | grep -i briana
...will show the only two lines with a Briana, and none of those Brians!
You can exercise escape characters very effectively with grep.
Single and double quotes work to determine how literally the command interprets certain things.
Single quotes are fully literal, e.g. if we run echo '$USER' it returns $USER
Double quotes are more gracious and will know to expand variables etc. e.g.:
echo "$USER" will return your username (try it out!)
To exclude an expression from your output, use the -v flag (note: looks like scissors!):
grep -i brian /etc/passwd | grep -v "Brian\ "
## Output below:
bfuertes:x:170068:15:Briana Fuertes :/homes10/900/bfuertes:/bin/true
bshields:x:170102:15:Briana Shields:/homes10/900/bshields:/bin/true
esposito:x:170010:15:Brianna Esposito:/homes10/900/esposito:/bin/true
Advanced: to filter N number of lines that occur before and after a pattern, use the '-A <numberOfLines>' option for after, and the '-B <numberOfLines>' option for before, e.g.:
grep -i 'enjoy' /unix101/readme.txt -A 1 -B 1
##############################################################
Here is some text to enjoy finding with the tool called grep #
##############################################################
head - print first 10 lines of a specified file identified in the command:
head file.txt
Can be used with -n flag to specify lines greater or lesser than 10.
e.g.:
head -n 3 teneleven.txt
1
2
3
tail - show the last 10 lines of a file as standard output.
When used with the '-n' flag and a number, will print the last N lines of the file instead. e.g.:
Suppose a file called teneleven.txt has 11 lines, each one with a sequential number.
tail -n 3 teneleven.txt
9
10
11
Now suppose you have a log file and want to see the most recent activity in it, about 50 lines instead of 10. We would run:
tail -n 50 bigoldfile.txt
But wait! We can also use grep with this, plus our appending redirect expression [ >> ] to make only the most specific entries of the log file be pared down into a cleaner file.
Let's say our program that logs to bigoldfile.txt identifies the most important output with the word "IMPORTANT" in caps. We can run:
tail -n 50 bigoldfile.txt | grep "IMPORTANT" >> smallnewfile.txt
# And then, we run...
cat smallnewfile.txt
# And we see...
IMPORTANT: Generating highly prominent output
IMPORTANT: Generating highly prominent output again
IMPORTANT: Generating highly prominent output once more
IMPORTANT: staying hydrated
IMPORTANT: program ran successfully!
less - file reading utility that, unlike a text editor, won't lead to you accidentally changing something. e.g.: less /unix101/resources/bigoldfile.txt
less has many useful features outlined in-depth by running "man less" but noteably it lets you scroll, as well as having a search function in the style of ctrl+F by pressing the forward slash key ( / ) and typing a phrase then hitting enter and using 'n' to cycle through all occurrences in the file.
wc -l - With the '-l' flag, checks how many lines a target file [arg1] contains. e.g.:
wc -l teneleven.txt
11 teneleven.txt
This indicates there are 11 lines in the file.
Also very useful in tandem with find.
find - Discover (and list) all objects in a folder hierarchy from a specified location [arg1] downwards. Extremely useful in tandem with 'piped' commands like wc -l or grep depending on your intention. Examples:
Determine how many objects a directory contains, including itself at the top-level:
find sixObjectFolder/ | wc -l
6
This indicates there are 6 files/folders total nested within 'sixObjectFolder/'
A folder named 'findme/' contains the following, confirmed by running 'find' on it:
# Our command:
find findme
# Our output:
findme
findme/here_I_am
findme/here_I_am/this_one.txt
findme/distractions/not_here
findme/distractions/not_here/seriously_not_here.txt
findme/distractions/not_here_either
findme/distractions/not_here_either/seriously_not_here.txt
findme/.nothing_to_see/.what_about_me.txt
If that went on and on, and we needed to see how many files beyond 'not_here' inclusive ones were present, we can first run:
find findme | grep -v distractions
findme
findme/here_I_am
findme/here_I_am/this_one.txt
findme/.nothing_to_see
findme/.nothing_to_see/seriously_not_here.txt
findme/.nothing_to_see/.what_about_me.txt
We are sick of seeing all the folders redundantly, and we just want the files and how many there are. We use the '-type -f' option to specify to find that we only want to see files, and the 'wc -l' piped command to count them:
find findme -type f | grep -v distractions | wc -l
3
chown - change owner of an object (file or folder) to another user or another user and group.
Examples:
chown brian FolderForBrian/
Now, if there is data in it and we want to change it all in one go, use -R for recursive:
chown -R brian FolderForBrian/
If we want to change the owner (brian) and the group (staff) at the same time, use a colon [:]:
chown -R brian:staff FolderForBrian/
chgrp - change group of an object (file or folder) to another group that is not its current one, or recursively set group bit for all subcontents of a target folder.
Example:
chgrp -R unix101 /unix101/attendees/brian
ls -l !$
Output:
drwxr-xr-x 2 brian unix101 2 Sep 15 15:56 brian
chmod - change mode of how restrictive a file or folder is. Has some nuance in that there is a numerical way to use it and an alphabetical way to use it.
Numerical significance for use with examples:
4: Read (r)
2: Write (w)
1: Execute (x)
[4 + 1] = 5 = [read + execute], no write.
[4 + 2] = 6 = [read + write], no execute.
[1 + 2] = 3 = [execute + write], no read.
Syntax can include any of the examples included below as well:
chmod [u|g|o]+[r|w|x] fileOrFolder
Template only above! Actual applied use would be more like this:
chmod u+rwx fileOrFolder
The command above grants the owner user (whose name shows for the object with ls -l) all 3 permissions: read, write, and execute.
Can also be run with one or two of the specified permissions, e.g.:
chmod g+wx fileOrFolder
The example above will append (add) the write and execute permissions for the assigned group of fileOrFolder
chmod 700 object.txt
Only the owner user can read/write/execute object.txt, with full permissions.
chmod 777 free_spirit.txt
All users on a system can read/write/execute free_spirit.txt
chmod o-rwx
chmod -R 740 folderName
The command above makes it so the owner is able to read/write/execute (7) upon it, while all other users cannot access it at all (0).
Inclusion of the -R will make the command recursive, so all subcontent files/folders will also be changed to 770 permissions, assuming the executor can run the command on all contents below the target 'folderName'.