MINIMUM LINUX INTRODUCTION FOR USERS ======= ===== ============ === ===== [R.Sonnenfeld - NMT Physics - August 2007] 1 sh-3.2$ #Sometimes you can forget what machine you are on! 2 sh-3.2$ hostname 3 speare4b-3-4 4 #Sometimes you can forget who you are! 5 sh-3.2$ whoami 6 rsonnenf 10 sh-3.2$ #Directories are arranged as trees with "/" as the "root". 11 sh-3.2$ cd / 12 sh-3.2$ tree -L 2 |more 13 sh-3.2$ #Actually the tree is upside down! 14 sh-3.2$ #You often forget where you are! 15 sh-3.2$ pwd #pwd -- present working directory 16 /u/rsonnenf 17 sh-3.2$ #Change to a new directory 18 sh-3.2$ cd phys241 #cd -- change directory 19 sh-3.2$ pwd 20 /u/rsonnenf/phys241 21 sh-3.2$ #Go back up one level 22 sh-3.2$ cd .. # .. mean go up one level 23 sh-3.2$ pwd 24 /u/rsonnenf 25 sh-3.2$ #Go back down 26 sh-3.2$ cd /phys241 27 bash: cd: /phys241: No such file or directory 28 sh-3.2$ # OOPS! -- /phys241 means a directory off of root. 29 sh-3.2$ # This works -- it's the full path name. 30 sh-3.2$ cd /u/rsonnenf/phys241 31 sh-3.2$ pwd 32 /u/rsonnenf/phys241 32a which matlab -- finding a command 32b cd - Go back where you were last 33 sh-3.2$ #list files (ls) 34 sh-3.2$ ls 35 lectures linuxintro.txt matlab mysteryfile test.m 36 sh-3.2$ mkdir newdirectory 37 sh-3.1$ ls 38 lectures linuxintro.txt matlab mysteryfile newdirectory test.m 39 sh-3.2$ ls -l #list files with the "long" option -- more info. 40 total 24 41 drwxr-sr-x 2 rsonnenf 1612 4096 Aug 22 18:35 lectures 42 -rw-r--r-- 1 rsonnenf 1612 6785 Aug 22 18:37 linuxintro.txt 43 drwxrwx--- 9 rsonnenf 1612 4096 Aug 20 19:02 matlab 44 -rw-rw---- 1 rsonnenf 1612 0 Aug 23 07:04 mysteryfile 45 drwxr-sr-x 2 rsonnenf 1612 4096 Aug 22 18:19 newdirectory 46 -rw-rw---- 1 rsonnenf 1612 3 Aug 23 07:03 test.m 47 sh-3.2$ Recognize the directories because they have a "d" in front. 48 sh-3.1$ #cp -- The copy command cp inputfile outputfile 49 sh-3.1$ #cp is used on your machine only 50 sh-3.1$ cp linuxintro.txt mysteryfile 51 cp: overwrite `mysteryfile'? y 52 #Moving files to remote machines: 53 gftp & 54 #Type in the host name and your password 55 #Choose the "SSH" or "SFTP" protocol rather than FTP 69 #Logging into remote machines -- ssh (secure shell) 70 -bash-3.1$ ssh richard@feynman.nmt.edu 72 #General command syntax -- 73 # command -options inputfile (if any) outputfile (if any) 74 # To look up a command, use "man" 75 #Finally -- we talk about editors. Choose pico or vim. 76 # If you choose "vim", run "vimtutor" to learn it. 77 # Also -- "The easy way" -- gftp -- 78 # Seeing what is in a file -- "more". 79 # MORE HELP 80 #http://www.physics.nmt.edu/~rsonnenf/computerlab/computers.html 81 82 #Can't resist showing a script -- 83 $ more dateloop.scr #!/bin/bash # This shows off how to do a command forever while waiting for something to happen until [ 1 = 0 ] do date -u +'%D %H:%M:%S.%NZ' sleep 0.01 done exit 0 THE POWER OF LINUX === ===== == ===== [R.Sonnenfeld - NMT Physics - August 2012] Linux comes with lots of power tools. Can operate on giant data files easily. cat data.txt -- "cat" = concatanate cat data.txt data.txt data.txt data.txt data.txt > big.txt cat big.txt big.txt big.txt > bigger.txt sort bigger.txt head -n 10 bigger.txt tail -n 10 bigger.txt wc bigger.txt grep 107.1345 bigger.txt # A script to change all occurrences of joe to mary in a file #!/bin/bash echo Takes a script called $1.sh and creates $1.shh echo USAGE: sedit.sh discard_list sed -e 's/joe/mary/g' $1.sh > $1.shh Sort by size cd ~; ls -lhSR |less Sort by time cd ~; ls -lhtR |grep Oct |less rename 's/ /_/' * #Uses perl rename facility Replace the first #space in each file name with an _ # A script to compress entire directories of photos. #!/bin/bash #Not clever enough to recurse to subdirectories. Must do one directory at a time. #Example Usage: #richard@elrond:~/photos/2009/Europe$ directory_compress.scr London # input_file_directory=$1 rootdir=$(pwd) echo using ImageMagick to resize and change size on directory $input_file_directory from root $rootdir output_file_path=$rootdir/$input_file_directory/compressed mkdir $output_file_path echo Placing files in $output_file_path cd $rootdir/$input_file_directory rename 's/.JPG/.jpg/' * #Uses perl rename facility Replace .JPG with .jpg #Program is not clever enough to handle both .JPG and .jpg extensions. for file in `ls *.jpg` ; do echo converting $file convert "$file" -resize 22% $output_file_path/c_$file #convert "$file" -resize 1024x768 $output_file_path/c_$file done