Introduction to UNIX
Slides can be found here
Directory
- A folder on your computer which contains files. UNIX filesystems are organized as hierarchical directories.
- Forward slashes divide levels in the nested hierarchy of directories, e.g.
/top_level_directory/second_level_directory
- The directory at the top of this hierarchy is called the root directory and is denoted simply as
/
.
Path
- The address to a directory or file on your computer. There are, generally, two types of paths:
- Absolute/full path represents the path of a given directory/file beginning at the root directory.
- Relative path represents the path of a given directory/file relative to the working/current directory.
- For example, say you have a file “my_favorite_file.txt” located in the directory
/Users/myname/Desktop/my_directory
.- The full path to this file is
/Users/myname/Desktop/my_directory/my_favorite_file.txt
. - The relative path to this file depends on where you are on the computer.
- If you are calling this file from Desktop, the relative path would be
my_directory/my_favorite_file.txt
- If you are in
/Users/myname/
, the relative path becomesDesktop/my_directory/my_favorite_file.txt
.
- The full path to this file is
Remember - Whenever you call the full path, you can reach the file from anywhere on your computer. Relative paths will change based on your current location.
Roaming around
Command | Translation | Examples |
---|---|---|
cd |
change directory | cd /absolute/path/of/the/directory/ Go to the home directory by typing simply cd or cd ~ Go up (back) a directory by typing cd .. |
pwd |
print working directory | pwd |
mkdir |
make directory | mkdir newDirectory creates newDirectory in your current directory Make a directory one level up with mkdir ../newDirectory |
cp |
copy | cp file.txt newfile.txt (and file.txt will still exist!) |
mv |
move | mv file.txt newfile.txt (but file.txt will no longer exist!) |
rm |
remove | rm file.txt removes file.txt rm -r directoryname/ removes the directory and all files within |
ls |
list | ls *.txt lists all .txt files in current directory ls -a lists all files including hidden ones in the current directory ls -l lists all files in current directory including file sizes and timestamps ls -lh does the same but changes file size format to be human-readable ls ../ lists files in the directory above the current one |
history |
history | see command history, and even call an earlier command with an exclamation mark and the line number (i.e !773 to reissue command #773). |
echo |
echo | print words |
man |
manual | man ls opens the manual for command ls (use q to escape page) |
cat |
concatenate | cat seqs.fasta prints the contents of seqs.fasta to the screen (ie stdout). Other options are less and more (because “less is more”, I’m sorry to say) |
head |
head | head seqs.fasta prints the first 10 lines of the file head -n 3 seqs.fasta prints first 3 lines |
tail |
tail | tail seqs.fasta prints the last 10 lines of the file tail -n 3 seqs.fasta prints last 3 lines |
wc |
word count | wc filename.txt shows the number of new lines, number of words, and number of characters wc -l filename.txt shows only the number of new lines wc -c filename.txt shows only the number of characters |
grep |
global regular expression parser | grep "a" filename.txt Search for particular strings or regular expressions in a file, in this case, search for lines with the letter “a” in the file “filename.txt”. grep -c "a" filename.txt will COUNT the number of lines with “a” in the file grep -v "a" filename.txt will find lines in the file WITHOUT “a” |
Shortcut | Use |
---|---|
Ctrl + C | kills current process |
Ctrl + L (or clear ) |
clears screen |
Ctrl + A | Go to the beginning of the line |
Ctrl + E | Go to the end of the line |
Ctrl + U | Clears the line before the cursor position |
Ctrl + K | Clear the line after the cursor |
* |
wildcard character |
tab | completes word |
Up Arrow | call last command |
. |
current directory |
.. |
one level up |
~ |
home |
> |
redirects stdout to a file, overwriting file if it already exists |
>> |
redirects stdout to a file, appending to the end of file if it already exists |
| |
redirects stdout to become stdin for next command |