Post on 11-Oct-2020
Introduction to Unix
Advanced Computing Center for Research and Education
http://www.accre.vanderbilt.edu
Why Unix?What comes to mind when you hear the word “Unix”?
Command line!
Unix is a programmer’s paradise Takes advantage of the keyboard and short commands (typically 2-4 letters in length) to efficiently manage files, folders, and so on Emphasizes performance Large user community (including most supercomputing centers) NOT designed to be a math editor
What is Unix?✦Unix is an operating system (un)like Windows ✦Originally created in the late 1960’s at AT&T Bell Labs ✦Designed to be a programmer’s OS ✦Turned out to be a portable, multi-tasking, multi-user OS (we take those things for granted now, but they were revolutionary at the time!)
✦There are many different “flavors” of Unix...
Different “flavors” of Unix have different niches
✦ IBM’s AIX and HP’s HP-UX are heavily used in business ✦Oracle’s (formerly Sun’s) Solaris once powered 70% of the web / e-mail servers on the Internet
✦Apple’s MacOS X is a user-friendly desktop ✦Linux is very high performance and free, which makes it ideal for HPC clusters
Unix ArchitectureShell
Kernel (OS)
Physical
Devices
✦Only the Kernel (the operating system) can manipulate physical devices (disks, the network, printers, etc.)
✦Users interact with the kernel via a shell (command interpreter)
✦Two primary shells: 1) bash, 2) tcsh
Unix Commands✦The format of Unix commands is command [ options ] [ arguments ]
✦ id is a command ✦ -f is an option to the ps command ✦example1 is an argument to the ls command (and -l is an option)
The most important command of all
✦The man command displays manual pages
✦Example at left is the output of man ls
✦Displays synopsis ✦Gives description of each option and, if applicable, argument to the command
Command History / Editing
✦The shell maintains a history of the commands you have previously executed (1000 commands by default)
✦Up and down arrow keys scroll thru your command history
✦Left and right arrow keys scroll thru the command ✦Edits can be made by adding, deleting, or replacing parts of the command
✦Pressing the enter / return key executes the command
Hierarchical Filesystem/
bin usr home scratchetc tmp
chmod
cp
date
grep
mv
rm
vi
nikkiannie codybin lib
bin docs src
libc.so
libgpfs.so
libjpeg.so
libstdc++.so
diff
find
gcc
id
make
perl
ssh
prog1.c
prog2.f77
prog3.cpp
myprog.sh
dothis.pl
dothat.py
Working With Directories✦pwd prints your present working directory
✦ cd changes directories (with no arguments always changes to your home directory)
✦mkdir makes a directory ✦ rmdir removes an empty directory; rm -r removes a non-empty directory
✦ ls lists directories (and files)
Working With Files✦file tells you what type of file a file is
✦more displays the contents of a file
✦ cp copies files ✦mv renames (moves) files
✦ rm removes files ✦The -i option makes cp, mv, and rm “interactive”
Other Handy Commandsecho
Used for displaying messages or variables (think of as a “print” command) Examples:
echo “Hello world!” echo $myvar
cat Used for displaying the entire contents of a file Example:
cat myfile.txt “more” and “less”
Used for scrolling through the contents of a file Examples:
more myfile.txt less myfile.txt
Command Substitution✦Any command enclosed in grave accents (not single quotes) is executed first and then its output is substituted in on the command line
✦Command substitution can be used with other commands (as the first example shows) or to assign a value to a variable (as the second example shows)
Input / Output Redirection
✦Any shell has 3 file handles open by default: ✦stdin - standard input, defaults to keyboard, file descriptor 0
✦stdout - standard output, defaults to screen, file descriptor 1
✦stderr - standard error, defaults to screen, file descriptor 2
Input / Output Redirection
✦ Input redirection - e-mail yourself a file: mailx Nikki.Husky@vanderbilt.edu < example1
✦Output redirection - myprogram > output.log ✦Error redirection - myprogram 2> error.log ✦Output and error redirection to different files - myprogram > output.log 2> error.log
✦Output and error redirection to the same file - myprogram > combined.log 2>&1
Editing Files✦There are three text editors available... ✦emacs - popular with programmers ✦ nano (pico) - basic, easy to use editor ✦ vi - the original Unix text editor; has steeper learning curve, but is the fastest of the three
✦Which should you use? ✦Whichever one you like!
Shell ScriptsSometimes rather than manually running a series of commands from the command line, it makes more sense to execute the commands from a script
For example, if you need to run the series of commands more than once
#!/bin/bash mkdir mydir cd mydir pwd echo “Hello world!” > myfile.txt ls -lh cd .. rm -r mydir
Create Your Own Bash ScriptCreate a bash script that creates 20 directories with names run1, run2, run3,…run20 Within each directory, create a file named foo.txt that contains a single line that reads “Hello world from runi”, where i is the directory number Hint:
Start with this:
#!/bin/bash for i in `seq 1 20` do echo $i done