Using the Linux Command Line David Love

Software Interest Group University of Arizona October 28, 2011

1

Introduction

2

Basics Connecting to UNIX/Linux Basic Commands Linux Filesystem

3

More Advanced Commands A List of Useful Commands File Transfer with Linux Permissions and Executables

4

Your Environment, Shell Scripting & Variables

5

Making Things Better & Easier

Intro Basics Commands Scripting Improvements

What Are UNIX and Linux? UNIX is an operating system originally developed in 1969 at Bell Labs. Has been continually modified since then. Currently, UNIX is a set of standards that an OS might abide by Apple OSX is UNIX. Technically, Linux is not UNIX. Sometimes see “*nix” to denote both UNIX and “Unix-like” systems.

Linux is an open source operating system, originally developed in 1992. Has been under continual development, is still free and open source. David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Connection Basic Commands Filesystem

Why use UNIX/Linux? It is the OS of high-performance computing. As of June 2011, 471 of the 500 fastest supercomputers ran Linux.1 23 run some other UNIX system.

Lots of academic software is written for Linux. Run code without using up your computer. Easy file transfer Backups! Always have homework available.

1

http://top500.org/stats/list/37/os David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Connection Basic Commands Filesystem

Why use UNIX/Linux? It is the OS of high-performance computing. As of June 2011, 471 of the 500 fastest supercomputers ran Linux.1 23 run some other UNIX system. Which leaves 6 that run Windows.

Lots of academic software is written for Linux. Run code without using up your computer. Easy file transfer Backups! Always have homework available.

1

http://top500.org/stats/list/37/os David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Connection Basic Commands Filesystem

Connecting to Math Linux

Windows Three options: UA Software https: //sitelicense.arizona.edu/ssh/ PuTTY http://www.chiark.greenend.org. uk/~sgtatham/putty/ Cygwin http://www.cygwin.com/ Apple Comes with a terminal. Linux Comes with a terminal.

David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Connection Basic Commands Filesystem

Where do we Connect?

Math & Applied Math (& Statistics?) grad students will connect to: gila.math.arizona.edu Main server for doing most everything. iguana.math.arizona.edu Server for working with remote desktop. chivo1,chivo2,chivo3,chivo4 For high performance computing. MUST first connect through another server (gila).

David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Connection Basic Commands Filesystem

From Windows with PuTTY

Open PuTTY. When Configuration Window opens, change: Host Name Port Set to SSH and open the connection

David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Connection Basic Commands Filesystem

From Cygwin/Apple/Linux with a Terminal Open a terminal. Connect with the command ssh -p PORT USER@SERVER

USER Your username. Mine is dlove SERVER The server to connect to, e.g., gila.math.arizona.edu PORT Port number to connect to. Math department uses 31415. Other servers may use the default port, 22, which needs no -p tag. To connect to gila, I use ssh -p 31415 [email protected]

David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Connection Basic Commands Filesystem

The Structure of a Linux Command Linux Command Structore commandname [OPTIONS] ARGUMENTS The basic command: 2

Begins with the name of the command. Might include some options. As the name suggests, options are generally optional.

3

May include one or more arguments.

1

David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Connection Basic Commands Filesystem

The Structure of a Linux Command Linux Command Structore commandname [OPTIONS] ARGUMENTS The basic command: 1 2

Begins with the name of the command. Might include some options. As the name suggests, options are generally optional. 1 2

3

In Linux’s documentation, optional things are in brackets. Most options are denoted with a dash and a letter, -r.

May include one or more arguments. David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Connection Basic Commands Filesystem

The Structure of a Linux Command Linux Command Structore commandname [OPTIONS] ARGUMENTS The basic command: 1 2

Begins with the name of the command. Might include some options. As the name suggests, options are generally optional. 1 2 3

3

In Linux’s documentation, optional things are in brackets. Most options are denoted with a dash and a letter, -r. They can be strung together, -rfp. Some require arguments.

May include one or more arguments. David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Connection Basic Commands Filesystem

The Structure of a Linux Command Linux Command Structore commandname [OPTIONS] ARGUMENTS The basic command: 1 2

Begins with the name of the command. Might include some options. As the name suggests, options are generally optional. In Linux’s documentation, optional things are in brackets. Most options are denoted with a dash and a letter, -r. They can be strung together, -rfp. Some require arguments. 4 Word length options are generally denoted with two dashes, --recursive. 1 2 3

3

May include one or more arguments. David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Connection Basic Commands Filesystem

Basic Unix Commands pwd - print the name of your current/working directory. cd [directoryname] - change your working directory. ls [directoryname] - list the contents of directories. mkdir directoryname - make a new directory. rmdir directoryname - remove/delete a directory. cp source destination - copy files and directories. mv source destination - move files and directories. rm filenamelist - remove/delete files. cat filename - concatenate file, i.e., display the contents. logout - log out of a terminal. David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Connection Basic Commands Filesystem

Examples & Useful Options If I log into gila, I can type some commands: [ d l o v e @ g i l a ˜ ] $ pwd / u1 / d l o v e [ dlove@gila ˜] $ l s bin / pub http internet / Public / makevnc ∗ Music /

Pictures / Desktop / Documents / Templates / Videos /

pwd told me the folder I was in. ls gave a list of the files and subdirectories in /u1/dlove

David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Connection Basic Commands Filesystem

More Examples [ dlove@gila ˜] $ l s bin / pub http internet / Public / makevnc ∗ Music /

Pictures / Desktop / Documents / Templates / Videos /

The / at the end indicates a subdirectory. The * indicates an executable file. Notice the prompt, showing: Username: dlove Machine name: gila Current directory: ~

David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Connection Basic Commands Filesystem

Useful Options & Other Details cp source file destination -r Recursive, copy directory and all contents. -i Interactive, ask before copying over another file. -f Force, override interactive. -u Update, only copy when source is newer than destination. Example cp file newfile Copy file to newfile. Example cp file new directory/ Copy file the the subdirectory new directory/ David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Connection Basic Commands Filesystem

Usefule Options & Other Details ls [directory or files] -a Dislpay all files, even hidden files. -l Display long form with more information. -R Recursive, display contents of subdirectories as well. rm [file] -r Recursive, remove files from subdirectories. -i Interactive, confirm before deleting. -f Force, override interactive.

David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Connection Basic Commands Filesystem

Useful Linux Shortcuts

Tab Completion Pressing the Tab key will complete a keyword or filename in UNIX/Linux. If the completion is not unique, pressing Tab twice will give a list of possible completions.

David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Connection Basic Commands Filesystem

Files in Linux Filenames in Linux can contain any character except / and the null character. You should avoid special characters like $, ?, *, \. When using files with special characters or spaces in commands, they need to be escaped with a \. rm file\ with\ spaces\*

Files don’t need extensions, but some like .c, .tex are standard. Files can have multiple extensions, like hw.tex.backup. Filenames beginning with a . are “hidden,” and not displayed by ls without using the -a option.

David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Connection Basic Commands Filesystem

The UNIX Filesystem The UNIX/Linux filesystem is quite different than Windows. Some things that might be helpful as a Linux user: / The root directory, contains everything else in the filesystem. /bin bin is a subdirectory of /, and contains some of the most basic executable files. /usr Contains most other executables, libraries, and documentation files. /etc “Editable Text Configuration files.” Configuration files for the entire system. Can provide examples for your own config. files. /tmp Space for temporary files. /home Contains home directories for all users. David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Connection Basic Commands Filesystem

Math Department Filesystem

The department computers have some nonstandard directories under /: /scratch Space to put temporary files generated by any computations you do. /u1,/u2,...,/u6 Home directories on the department computers are located in one of these directories, instead of /home. My directory is in /u1. Yours might be somewhere different.

David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Connection Basic Commands Filesystem

File System Abbreviations

UNIX and Linux have abbreviations for some directories: ~ Home directory. . Current directory. .. Parent directory. If I’m in my home directory ~, or /u1/dlove, then: . is /u1/dlove, and .. is /u1.

David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Useful List File Transfer Executables

Wildcards Linux uses some characters to indicate patterns of text. ? Fill in any single character. ls hw?.tex lists all filenames like hw1.tex, hwQ.tex, etc. * Fill in any number of characters ls hw* lists all files starting with hw, like hw1.tex, hw583.pdf, etc. cat hw*.tex concatenates all files that start with hw and end with .tex [] Specify a range for a single character cp hw[1-3].tex .. Copy files hw1.tex, hw2.tex, hw3.tex to the parent directory.

David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Useful List File Transfer Executables

More Wildcards

Of course, wildcards can be combined. rm -f hw[1-4]?.[a,p]* removes, without confirmation, files that: Begin with hw, Followed by a digit between 1 and 4, Followed by another single character, and Have an extension that begins with an a or p

David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Useful List File Transfer Executables

Output Redirection Linux prints to the screen a lot, and lets you work with that. > Write or overwrite output to a file. cat hw*.tex > allhw.tex Concatenate all files hw*.tex into allhw.tex. >> Append output to end of a file. cat newhw*.tex >> allhw.tex Concatenate the files, add to the end of allhw.tex. | “Pipe” output into a new command. Example: tee FILE prints output both to FILE and the screen. COMMAND | tee FILE prints the output of COMMAND to both FILE and the screen.

David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Useful List File Transfer Executables

man pages UNIX & Linux systems have a built-in manual: the man pages. The structure of a man page: Name A short description of the command. Synopsis The basic form of the command: Optional arguments enclosed in “[]”. Options with finite choices enclosed in “{}”.

Description A longer description of the command, including all options. Other things in the man page might be: Examples, Bug reporting, List of similar commands.

David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Useful List File Transfer Executables

screen screen creates virtual terminals that: Can continue to run when you log out. Allow you to come back whenever you want.

Typing screen creates a new terminal. ^A-d (CTRL-a then d) detaches (leaves) a session. The -r option (screen -r) reattaches (continues) a session. Other commands: ^A-c Create another terminal window. ^A-n Cycle to next terminal window. ^A-p Cycle to previous terminal window. logout Close the terminal window.

David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Useful List File Transfer Executables

grep grep PATTERN [FILE...] prints lines in a file that contain PATTERN. grep for prog.c prints all lines in the file prog.c containing the pattern for. Some options: -n Print line number. -H Print file name. -i Ignore case. -v Print non-matching lines. -w Only match full words. Example: grep -nHiw rand01 * in a code I used.

David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Useful List File Transfer Executables

diff diff FILE1 FILE2 intelligently compares files line by line and prints the differences. If given directories, it compares files of the same name in each. Output: < Indicates a line in the first file. > Indicates a line in the second file. = Indicates common lines. Options: -i Ignore case. -w Ignore white-space. -q Print only whether files differ. gvimdiff is an editor that incorporates diff, but you should know vim first. David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Useful List File Transfer Executables

quota, ps and top quota Displays how much space your account is allotted. Use -s option. ps Lists running processes. Without arguments, it shows processes running from the current terminal. Use -u to specify a user name, e.g., ps -u dlove. top Real time list of processes using system resources. Quit by typing q. Type u to enter a username.

David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Useful List File Transfer Executables

kill & killall

Closing programs from the terminal: ^C Kills the program currently running in the terminal. kill PID Kills process with id number PID -9 Signal that can’t be blocked. killall NAME Kill a process by name. -9 Signal that can’t be blocked.

David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Useful List File Transfer Executables

tar & zip tar, short for “tape archive” creates archives. Options: -c,-r,-x -j,-z -v -f Examples:

Create, append to, extract archive. Compress with bzip2, gzip. Verbose. Specify filename for archive

tar -cvjf archive.tar.bz2 folder Compress folder into archive archive.tar.bz2.

zip and unzip work with .zip archives.

David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Useful List File Transfer Executables

find find recursively searches through directories to find files with given characteristics. find / -name passwd will find all files named “passwd” on the computer. find -iname resume.tex will find all files named ”resume.tex,” but is case insensitive. find -name "*.sh" -executable will find all executable files with name *.sh. When using wildcards, enclose in quotes to prevent the shell from expanding them.

find has a lot of possible tests and options. See http://www.thegeekstuff.com/2009/03/ 15-practical-linux-find-command-examples/ for a tutorial of some useful ones. David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Useful List File Transfer Executables

locate

locate also finds files based on a name or path pattern. It searches through a database–much faster than find! But database is only updated periodically. Many fewer options than find. locate [OPTIONS] PATTERN is the basic command.

David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Useful List File Transfer Executables

xargs xargs puts the output from one command into arguments for another command. Very useful with find! To deal with filenames that contain spaces or other difficult characters, use print0 (zero) option with find, and -0 (zero) option with xargs. Command: find OPTIONS -print0 | xargs -0 -I file COMMAND file OTHER ARGUMENTS. -I option replaces file with the output of find.

Examples find . -name "*.cpp" -print0 | xargs -0I file rm file find . -name "*.tex" -print0 | xargs -0I file mv file file.backup David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Useful List File Transfer Executables

Secure Shell Copy

Basic method is to use scp Open a terminal. Connect with the command scp -P PORT SOURCE DESTINATION

SOURCE Where the file currently is. DESTINATION Where you want to file to be. PORT Port number to connect to. Math department uses 31415. Other servers may use the default port (no -P tag).

David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Useful List File Transfer Executables

scp Continued

Suppose I want to copy ∼/file.txt from gila to Documents on my computer: scp -P 31415 [email protected]:∼/file.txt ∼/Documents/

David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Useful List File Transfer Executables

rsync: File Synchronization rsync is a much more powerful tool: Checks files on local machine and server, only transfers files that have changed. Only transfers differences between changed files–Very Fast! Suppose I want to copy ∼/file.txt from gila to Documents on my computer: rsync --rsh=’ssh -p 31415’ [email protected]:∼/file.txt ∼/Documents/ Servers using the default port don’t need the --rsh=’ssh -p 31415’ information.

David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Useful List File Transfer Executables

Permissions and chmod

Three types of user can have permissions for each file: user You. group Various subsets of the logins. On math systems, only group is “users.” others Everyone else. And three types of permissions: read Can read files. write Can write or change files. execute Can run executables or list contents of directories.

David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Useful List File Transfer Executables

Changing Permissions Permissions are changed with the chmod command. chmod MODE FILE Most intuitive use of MODE is: TYPE±PERM Adds/removes permissions PERM to users of TYPE. TYPE=PERM Gives TYPE exactly permissions PERM Examples: chmod u+x file Allow file to be executed. chmod a+X file Allows anyone to execute if file is a directory or if someone else has execute permission. chmod go-rwx file No one from groups or others can read, write or execute file. David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Useful List File Transfer Executables

Running Code on Linux

To run a program, must specify where the program is. Run program cep1, which is in /home/dlove: /home/dlove/cep1

Can use shortcuts. Run program nv, in the current directory, type: ./nv

David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Bash When you’re using the command line, you’re actually using a shell. The shell provides the user interface, and can function like a programming language. Back in the day, some guy named Bourne wrote the Bourne shell, with an executable named sh. You’ll still see shell scripts with extension .sh.

Then other people developed other shells. In 1989, some guy named Fox wanted to update the Bourne shell. Logically, he named it:

David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Bash When you’re using the command line, you’re actually using a shell. The shell provides the user interface, and can function like a programming language. Back in the day, some guy named Bourne wrote the Bourne shell, with an executable named sh. You’ll still see shell scripts with extension .sh.

Then other people developed other shells. In 1989, some guy named Fox wanted to update the Bourne shell. Logically, he named it: The Bourne-Again Shell (Bash).

David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Bash When you’re using the command line, you’re actually using a shell. The shell provides the user interface, and can function like a programming language. Back in the day, some guy named Bourne wrote the Bourne shell, with an executable named sh. You’ll still see shell scripts with extension .sh.

Then other people developed other shells. In 1989, some guy named Fox wanted to update the Bourne shell. Logically, he named it: The Bourne-Again Shell (Bash). Bash the default shell in most UNIX/Linux systems. David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Files in Your Home Directory Many “hidden” files, beginning with a “.” are in your home directory. Can see them with ls -a option. Many programs use these files to store configuration information. Hidden files can almost always be edited with only a text editor. The most important hidden files: .bashrc Contains the setup information for the shell. .profile Similar to .bashrc. There are often basic files in /etc.

David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Introduction to Variables Bash uses variables to store important information. Variables can be used like in a programming language. By convention, variables in the shell are named with capital letters. Variables value is accessed with $. For example, the PATH variable stores the folders that include executables. Using the echo command:

David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Introduction to Variables Bash uses variables to store important information. Variables can be used like in a programming language. By convention, variables in the shell are named with capital letters. Variables value is accessed with $. For example, the PATH variable stores the folders that include executables. Using the echo command: [ d l o v e @ g i l a ˜ ] $ echo $PATH / usr / l o c a l / sbin : / usr / l o c a l / bin : / usr / sbin : / u s r / b i n : / s b i n : / b i n : / u s r / games

Can add more by PATH=$PATH:DIRECTORY. E.g., in .bashrc. David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Scripts

Scripts in Bash are a lot like programs in C, but have higher level coding. Still things like: For loops, If tests, Changing values of variables.

Next, I have an example code to run multiple iterations of a code for my research.

David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

An Example Script 1 #!

/bin/bash

2 # $1=problem, $2=m 3 for G in 0100 0200 1000 4 do 5

if [ $2 -ge $gamma ];

6

then

7

RUN="./rd $1 -s $2 -g $G | tee $1 m$2 $G.dat | grep Rep"

8

echo ${RUN}

9

eval ${RUN}

10

fi

11 done 12 ./parser $1 m$2 *.dat -o total $1 m$2.dat 13 echo "Done!"

David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

An Example Script 1 #!

/bin/bash

2 # $1=problem, $2=m 3 for G in 0100 0200 1000 4 do 5

if [ $2 -ge $gamma ];

6

then

7

RUN="./rd $1 -s $2 -g $G | tee $1 m$2 $G.dat | grep Rep"

8

echo ${RUN}

9 10

eval ${RUN} fi

11 done 12 ./parser $1 m$2 *.dat -o total $1 m$2.dat 13 echo "Done!"

Program to use to interpret David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

An Example Script 1 #!

/bin/bash

2 # $1=problem, $2=m 3 for G in 0100 0200 1000 4 do 5

if [ $2 -ge $gamma ];

6

then

7

RUN="./rd $1 -s $2 -g $G | tee $1 m$2 $G.dat | grep Rep"

8

echo ${RUN}

9 10

eval ${RUN} fi

11 done 12 ./parser $1 m$2 *.dat -o total $1 m$2.dat 13 echo "Done!"

A comment. $1 is first command line argument. $2 is second.

David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

An Example Script 1 #!

/bin/bash

2 # $1=problem, $2=m 3 for G in 0100 0200 1000 4 do 5

if [ $2 -ge $gamma ];

6

then

7

RUN="./rd $1 -s $2 -g $G | tee $1 m$2 $G.dat | grep Rep"

8

echo ${RUN}

9 10

eval ${RUN} fi

11 done 12 ./parser $1 m$2 *.dat -o total $1 m$2.dat 13 echo "Done!"

Basic structure of a for loop David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

An Example Script 1 #!

/bin/bash

2 # $1=problem, $2=m 3 for G in 0100 0200 1000 4 do 5

if [ $2 -ge $gamma ];

6

then

7

RUN="./rd $1 -s $2 -g $G | tee $1 m$2 $G.dat | grep Rep"

8

echo ${RUN}

9 10

eval ${RUN} fi

11 done 12 ./parser $1 m$2 *.dat -o total $1 m$2.dat 13 echo "Done!"

Basic structure of an if test. -ge means “≥“ David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

An Example Script 1 #!

/bin/bash

2 # $1=problem, $2=m 3 for G in 0100 0200 1000 4 do 5

if [ $2 -ge $gamma ];

6

then

7

RUN="./rd $1 -s $2 -g $G | tee $1 m$2 $G.dat | grep Rep"

8

echo ${RUN}

9 10

eval ${RUN} fi

11 done 12 ./parser $1 m$2 *.dat -o total $1 m$2.dat 13 echo "Done!"

Load a string containing the command into RUN. Variables are evaluated inside quotes. David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

An Example Script 1 #!

/bin/bash

2 # $1=problem, $2=m 3 for G in 0100 0200 1000 4 do 5

if [ $2 -ge $gamma ];

6

then

7

RUN="./rd $1 -s $2 -g $G | tee $1 m$2 $G.dat | grep Rep"

8

echo ${RUN}

9 10

eval ${RUN} fi

11 done 12 ./parser $1 m$2 *.dat -o total $1 m$2.dat 13 echo "Done!"

Print the string to the screen. David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

An Example Script 1 #!

/bin/bash

2 # $1=problem, $2=m 3 for G in 0100 0200 1000 4 do 5

if [ $2 -ge $gamma ];

6

then

7

RUN="./rd $1 -s $2 -g $G | tee $1 m$2 $G.dat | grep Rep"

8

echo ${RUN}

9 10

eval ${RUN} fi

11 done 12 ./parser $1 m$2 *.dat -o total $1 m$2.dat 13 echo "Done!"

Execute the code using eval. David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

An Example Script 1 #!

/bin/bash

2 # $1=problem, $2=m 3 for G in 0100 0200 1000 4 do 5

if [ $2 -ge $gamma ];

6

then

7

RUN="./rd $1 -s $2 -g $G | tee $1 m$2 $G.dat | grep Rep"

8

echo ${RUN}

9 10

eval ${RUN} fi

11 done 12 ./parser $1 m$2 *.dat -o total $1 m$2.dat 13 echo "Done!"

Execute another program to consolidate all the output files. David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

An Example Script 1 #!

/bin/bash

2 # $1=problem, $2=m 3 for G in 0100 0200 1000 4 do 5

if [ $2 -ge $gamma ];

6

then

7

RUN="./rd $1 -s $2 -g $G | tee $1 m$2 $G.dat | grep Rep"

8

echo ${RUN}

9

eval ${RUN}

10

fi

11 done 12 ./parser $1 m$2 *.dat -o total $1 m$2.dat 13 echo "Done!"

David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

My Solution to File Transfer I have three rsync-based scripts to transfer files from my computers to gila. I make sure that the ∼/Documents folder is identical on my desktop, laptop, and on gila: ups (“Upstream Backup”) Copies ∼/Documents from my computer to gila. If it finds files on gila that are not on my computer, it deletes them. dow (“Downstream Backup”) Copies ∼/Documents from gila to my computer. If it finds files on my copy of ∼/Documents that are not on gila, it deletes them. ms (“Math Sync”) Synchronizes in both direction, but will not delete files anywhere. David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

My Code My code has 4 lines that need to be edited: Change the user name. MATHSERVER="[email protected]"

The folder on your computer to sync. LOCALDOCS=∼/Documents

The folder on gila to sync. MATHDOCS="∼/Documents"

The options. The ’n’ tells rsync not to change any files. Remove this when you’re confident the code is correct. OPTS="-vrtun"

David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Aliases

Aliases make complicated commands simple. Put them in .bashrc or .profile. Format is alias NEWCOMMAND=OLDCOMMAND. For example, the file /etc/skel/.bashrc contains: alias alias alias alias

ll=’ls -alF’ la=’ls -A’ l=’ls -CF’ grep=’grep --color=auto’

David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Networking Aliases

I have some aliases set up specifically for networking: alias math=’ssh -p 31415 [email protected]’ alias mathi=’ssh -p 31415 [email protected]’ alias mathvnc=’vncviewer iguana.math.arizona.edu:4 -passwd $HOME/.vnc/passwd’

David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

Passwordless Connection & Privacy

Most connection methods allow you to connect without passwords. Make sure that only you have permission to read the required files!

David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

SSH Without Password To login from a terminal without giving a password Run ssh-keygen -t rsa Accept the default location for the key. Enter your password. Make a .ssh folder on the server (e.g., gila): ssh -p 31415 [email protected] mkdir -p .ssh Append id rsa.pub to your list of authorized keys on gila cat .ssh/id rsa.pub | ssh [email protected] ’cat >> .ssh/authorized keys2’

David Love

Command Line Linux

Intro Basics Commands Scripting Improvements

VNC Without Password To use VNC without a password, you need only modify your own computer. Run vncpasswd Accept the default location for the password file. Enter your password. There’s no need for a view-only password. Call your VNC viewer with the -passwd option, and the location of your password, e.g., vncviewer iguana.math.arizona.edu:12 -passwd $HOME/.vnc/passwd (Note: $HOME is the same as ~.)

David Love

Command Line Linux

Resources

Linux Documentation Project http://tldp.org/guides.html Introduction to Linux, a Hands On Guide http: //tldp.org/LDP/intro-linux/html/index.html SSH Login Without Password http://linuxproblem.org/art_9.html

VNC Without Password http://www.dotkam.com/2009/03/22/ vnc-into-remote-server-without-typing-a-passwor

The End