------------------------------------------------------------------------ Essential unix: ------------------------------------------------------------------------ Unix regards all data as "files". Files consists of records which terminate with a "Line Feed" (\n or \012). The end of the file is "control D". [Windows uses "\r\n" to mark the end of the line. To convert Unix files to DOS files use dos2unix or unix2dos] # Moving around directories $ cd ~ #go to the top level $ pwd $ tree $ mkdir NewDirectory $ cd NewDirectory $ rmdir NewDirectory #delete NewDirectory (must be empty) $ rm b #remove file "b" $ rm -i b #ask ask question and if yes then remove file "b" $ date $ cal $ cal 2017 $ xcalc & #this means put job in "background" $ fortune $ xeyes & # Creating text files $ cat > Rabbits.txt % Rabbits are the best pets They are superior to cats They are nicer to hold than snakes. They are not as jumpy as dogs. Everyone should own a rabbit It is fun to own a buck (male rabbit) & a doe (female rabbit) Over time you will have lots of rabbits to pet % (control D) # At the shell level, "#" is the comment character # Within a program it is common to use "%" as a comment character $ more Rabbits.txt #has lots of features (see "man" page) $ clear #clear screen $ less Rabbits.txt #going up and down the directory tree $ cd .. $ ls -l $ cd NewDirectory $ ls -l $ which cd $ info cd $ man cd $ file Rabbits.txt $ cp Rabbits.txt Pets.txt #copies input file to output file $ compress Pets.txt $ ls -l *.txt Unix is really amazing (>,<,|) # * is wildcard # < is input to the program (usually from another file) # (can be dropped if the context is clear) # > is out from the program (usually directed to a file) # | is "pipe from this program to next program" # Comment line # $ cat < b.txt #formal way of saying Rabbits.txt is input file $ cat b.txt #for most (but not all) untilities you can skip "<" $ wc < Rabbits.txt $ wc Rabbits.txt $ head Rabbits.txt $ head -n 3 Rabbits.txt #this is called as flag or option $ tail Rabbits.txt #sometimes there are default values #pipes allow you take input of one program and feed it to another program $ head -n 10 Rabbits.txt | wc #provides word count for first ten lines # "tee" is like a plumbers "T" pipe. # it takes input stream and writes to file and also sends it to next # program! $ head -n 10 Rabbits.txt | tee RabbitsHeader.txt wc $ yes | head -n 10 #create a file with identical lines $ yes | head -n 10 | tee OneThroughTen.txt #write to both screen & file $ yes | head -n 10 | nl | tee OneThroughTen.txt #number the lines $ du #disk usage $ df #file store usage Other useful tools #Create files "numbers" and "alphabets" $more numbers #file1 1 2 3 $more alphabets #file 2 a b c $ paste a b #side by side $ paste -s a b #top & botom $ grep "dog" Rabbits.txt $ grep -v "dog" Rabbits.txt #try it out $ more d rabbits 1 cat 6 eog 2 lion 3 donkey 5 zebra 4 $ sort d #default sort is column1 and ascii sequence $ sort -k2 -n d #sort on column2 and numerically $ sort -k2 -nr d #reverse sort $ expand < Infile #expands tabs to spaces ---- power tools ---- $ cut -d" " -f1 < RRLyrae.dat $ cut -d" " -f1,2 < RRLyrae.dat > c.dat $ sed '1d' < b.txt #delete first line $ sed '1,3d' < b.txt #delete line 1 through 3 (useful to delete header lines) $ sed '$d' < b.txt #delete last line $ sed '/%/d' < Rabbits.txt #delete lines containing "%" character (comment) $ uniq $ join $ wget URL $ curl URL