Unix crashcourse
• unix, shell, and shellpower
Following is a list of basic shell commands for Unix:
ls
ls -l Outputs
drwxr-x--- 2 akarki15 akarki15 6 Jun 2 2011 bin
-rw-r----- 1 akarki15 akarki15 4194304 Apr 22 2014 block-device.data
drwxrwxr-x 17 akarki15 akarki15 4096 Feb 28 2014 cs111
drwxr-x--- 19 akarki15 akarki15 4096 May 28 2014 cs112
drwxr-x--- 6 akarki15 akarki15 147 May 10 2014 cs-261
drwxr-x--- 3 akarki15 akarki15 18 May 1 2013 CS281- First column of the output is showing access rights
- rwx: read, write, executable
- Three sets of them for user, others in the group, and everyone
How do you change the access settings?
chmod u+wwill give write rights to users in the groupchmod o+xwill give executable rights to others in the groupchmod a-rwill take away read rights from all in the group
Some switches for ls
| Command | Description |
|---|---|
ls -a |
shows “all” files |
ls -d a* |
Wildcard to display all files whose filename begins with a |
ls -d [i-l]* |
[i-l] is a set containing letters i through l |
Cool things you can do with dot dot (..) and tilde (~)
- .. represents the parent director. So say you are in
/home/aashishand/homehas another file calleda.java. You can access it from aashish by saying
cat ../a.java~/refers to the user home directory
Using History
historyshows list of previous shell commands used. It gave following output on my computer:
5673 ssh -Y akarki15@romulus.amherst.edu
5674 cd Desktop/jam/blog/
5675 jekyll build
5676 jekyll serve- Run a certain numbered command by
!<number of the command>. For exmaple!5676will runjekyll serve.
.cshrc file
~/.cshrccontains shell configurationPATHcontains paths for programs you run on terminal. E.g. more, less etc
Random cool monitors
-
topshows the running processes on the system -
vmstat 5shows the paging history, 5 denotes how fast the list is being refreshed -
ps uaxshows the processes running right now
All about that stream
- Three I/O streams of unix (Similar to what it is in Java)
- Say you have a java executable called prog.class
- stdin
java prog < inputFilewill useinputFilefor keyboardinput expected by stdin in java.
- stdout
java prog > outfilewrites whateverSystem.out.println("")into theoutfile- Note that
System.err.println("")is written on the screen instead of file
- Note that
java prog >& oufilewill write both stdout and stderr intooutfilejava prog > /dev/tty) >& /pathwill write stdout to/dev/ttyand stderr to/pathprog >> outfileappendas to theoutfileinstead of overwriting it
Pipe command
- Feeds the output of one command to another (prog1 to prog2). e.g.
prog1 < file1 | prog2Suspend/resume jobs aka COOL STUFF
ctrl+zsuspends/pauses the running jobbgputs a job to backgroundjobsprints the list of jobsfg %1bring job no. 1 from background to foreground
grep
grep 'word' filenamesearches for word in file called filenamegrep --color 'data' fileNamedisplay grep results in colorgrep 'word' file1 file2 file3searches for word in file1, file2 and file3grep 'string1 string2' filenamesearches for string1, string2 in filenamecat otherfile | grep 'something'pipe grep result and display its (their) contents
Tar Tricks
tar cf Simplex.tar Simplexcreate afileSimplex.tarfromSimplex
mkdir newDir ; cd newDir ; tar xf ../Simplex.tar- Creates
newDirand extractsSimplexintonewDir
- Creates
- Loop:
basenameparses out from every$ijust its filename. The loop creates a copy of every file but changes the extension to.save.
foreach i (*.txt)
foreach? cp $i `basename $i .txt`.save
foreach? end