Lab 01 - Introduction to the command line 0. Cut and paste in Linux... usually. If you highlight text by clicking and dragging over it, pressing the middle mouse button (scroll wheel, both buttons if none, or right click on WSL) will paste the text at the cursor. 1. Getting started Open a terminal window. If using WSL, start your Xserver. At the prompt, type xeyes and press ENTER. A pair of eyes should appear that track your mouse pointer. Install xeyes if it is not installed. 2. Job control Close xeyes by clicking the x in their window bar. Back at the terminal, press "up arrow". The xeyes command should appear. Press ENTER (eyes should appear again). While holding down the CONTROL key, press z. This key combination is denoted as Ctrl-z, or ^Z . The eyes will no longer track your mouse. The xeyes program has been stopped, and the command prompt has returned. fg will bring xeyes alive again into the ForeGround. Ctrl-c (^C) will kill them. Bring up xeyes again, this time using "tab completion" by typing xey and pressing TAB. xeyes should appear on the line. Press ENTER. press ^Z, and then enter bg The prompt returns, but your xeyes are alive. xeyes is running in the BackGround. jobs will show you what jobs are running. The job number associated with xeyes is likely [1]. You can kill xeyes, or bring them into the foreground via kill %1 or fg %1 You can set a process to run in the background from the start with the & xeyes & They will run in the background. A way to list the processes running is ps You'll get a small list that likely includes bash, xeyes and ps. Every process has a process id (PID), usually not a small number like 1 or 2. kill 51914 (Replace the above number with the PID assocaited with xeyes.) Hard-to-kill jobs sometimes require the "-9" option. For the above, it would have been kill -9 51914. Experiment with various options for ps, like ps a, ps u, ps aux, ps -a You can also try the command ‘top’ (press q to quit top). 3. Connecting to a remote machine Log on to alfven, a machine in the physics dept. ssh user_name@alfven.physics.mun.ca where user_name is the prefix to your MUN email. Answer "yes" if you get a question about being sure about connecting. Ask about your password. Once logged in to alfven, the first thing is changing your password with passwd You'll be prompted with a couple of questions. I suggest changing your password to whatever your MUN password is. The program will tell you if the change has been successful, after which exit This will log you out. Log back in again, allowing alfven graphics to appear on your machine. ssh -X user_name@alfven.physics.mun.ca And try old xeyes The eyes should appear on your screen, but they are being generated by alfven. Kill them and log out (^C, exit). You should be back on your own machine. 4. Downloading from the Web There is an archive for Project 1 on the website. Download the file with wget http://www.physics.mun.ca/courses/p3800/PROJECTS/P1/Project1.tgz This may complain. If so, use, as is suggested, wget --no-check-certificate http://www.physics.mun.ca/courses/p3800/PROJECTS/P1/Project1.tgz A progress bar should show up. After not too long Project1.tgz should be saved to your machine. The file is a compressed archive containing a directory with various file. Linux will tell you that. file Project1.tgz Unpack it with tar -zxvf Project1.tgz The -z options is for compressing/decompressing with gzip -x is for expand -v is for verbose -- writing to the screen the files processed -f is for given the file name (Project1.tgz in this case) 5. Where are we and what have we done! So we've downloaded a .tgz file and uncompressed it... but where did we put it, and what's inside? You put it in your home directory, which is somewhere in the Linux directory structure. To find out where we are currently in the directory "tree", enter pwd for “Print Working Directory.” The output is the absolute "path" starting from the root directory / The path refers to where a file is situated, and includes the sequence of directories in the tree. The root directory is the starting level for all absolute paths. A relative path starts at the current directory. “root” also refers to a special user on a linux system with privileges over everything. We can "list directory contents" with ls and you should at least see Project1.tgz, which we've downloaded, and PROJECT1, the directory that the .tgz file contained. The "ls" command, as most in Linux, have several options. Play around with the following ones ls -a (shows "hidden" files and directories that being with a ".") ls -l (detailed listing) ls -rl (-r reversed the order) ls -lrt (my favourite, most recently modified listings appear at bottom) ls -F (directories are distinguished with a "/" at the end) ls -d (treat directories like files - don't list their contents) You can get a description of all the options through MANual pages man ls which exist for most commands. Press q to exit man and get back to the command line. Try ls Project1.tgz ls PROJECT1 So if the argument is a directory, you get its listing. Try ls Pr* ls PR* The wildcard "*" stands for any number of any characters in a name. We can Change Directory into PROJECT1 with cd PROJECT1 Check where we are with pwd You are no longer in your home directory. The argument to the cd command can be a relative path or absolute path. In the above, we gave a relative path to PROJECT1, relative to where we were. Play with these cd (go to our home directory) cd - (go back to directory where you were previously) cd .. (go up to parent directory of current directory) cd . (change to current directory - DOES NOTHING!) cd / (change to root directory - try ls here!) cd ~ (~ stand for our home directory) cd $HOME ($HOME is the same as ~) cd ~/PROJECT1 (absolute path to PROJECT1) cd ../../ (relative path - try ls, see your friends' directories) cd /usr/bin (absolute path - try ls here!) An argument for cd, a path, can also serve as an argument for ls, e.g. ls $HOME/.. 6. File permissions cd ~ ls -l The long listing entry for PROJECT1 on my system is drwxr-xr-x 2 saika users 4096 Jan 5 22:10 PROJECT1 The first "d" means that it is a directory. It would be "-" for a regular file. rwxr-xr-x is the permission string (more on this later) There are 2 links (1 for a file, 2 + n for directory with n immediate subdirs). The user who owns the directory is "saika". The group associate with the directory is "users" The directory (not its contents, just as a file) takes up 4096 bytes. It was last modified on Jan 5 22:10. The file/directory name is PROJECT1 The permissions string is grouped into three sets of three characters. The first three, rwx, mean the user (u) has read, write and execute permissions. The second three, r-x, mean the group (g) has only read and execute permissions. The last three, r-x, mean that anyone else (o -- other) on the system also has read and execute permissions. Executable for a directory means that it is accessible. Executable for a file means that it can be run (more on this later). Let's make the directory non-executable with chmod -x PROJECT1 ls -l now yields drw-r--r-- 2 saika users 4096 Jan 5 22:10 PROJECT1 and cd PROJECT1 doesn't work! Change permissions back. chmod +x PROJECT1 cd PROJECT1 works! We are now in the PROJECT1 directory. Make sure with pwd ls -lrt There are a bunch of files, including some Fortran 90 source files, the ones ending in .f90 Compile one using gfortran integrate.f90 -o integrate ls -lrt There is a file called "integrate" and it is executable. That means we can type ./integrate And it will run! Just press ^C to kill it. There are other files, ending with .sh, that are script files and are meant to be executable. Look at the contents of run_integrate2.sh using cat run_integrate2.sh (dumps, or "concatenates" the file to the screen) and less run_integrate2.sh (can browse through a file, press q to quit) The file contains bash scripting code, a list of commands, that the shell can run. We can make it executable by the owner (u) and the group (g) via chmod u+x run_integrate2.sh chmod g+x run_integrate2.sh or, equivalently, chmod u+x,g+x run_integrate2.sh Now we can run it with ./run_integrate2.sh It should produce three columns of numbers. Other users on the system (who are not part of the group) do not have execute privilege. You can run scripts even if they are not executable with, e.g., bash ./run_integrate.sh There is another way to specify permission strings, where r = 4 w = 2 x = 1 To get the permission string rw-r-x--x You can either use, on say README.txt chmod u+rw,u-x,g+rx,g-w,o-rw,o+x README.txt Or add up the “values” of permissions for each of u (4+2+0), g (4+0+1), and o (0+0+1) and write chmod 651 README.txt 7. Creating, deleting, copying, renaming and moving files and directories Go to your home directory cd Make a directory (use your name at the end of the name) mkdir FAKEP1_YOUR_NAME Now execute the following commands. To observe their effect, use ls -lrt and pwd liberally in between. cd FAKEP1_YOUR_NAME cd .. rmdir FAKEP1_YOUR_NAME (deletes an empty directory) mkdir FAKEP1_YOUR_NAME (make the directory again) touch funny_file (creates empty file) mv funny_file funny_file_renamed (renames a file or directory) mv funny_file_renamed FAKEP1_YOUR_NAME (moves a file to a directory) cd FAKEP1_YOUR_NAME ls cd .. rmdir FAKEP1_YOUR_NAME (shouldn't work) nano funny_file2 This opens a text editor that is fairly intuitive. Write some text into the file, and then look at the menu at the bottom. Type Ctrl-X, then hit y for yes, then hit return to keep the default file name. You should now be back on the command line having created a non-empty funny_file2. cat funny_file2 cp PROJECT1/README.txt . (copies the README.txt file that is in the PROJECT1 directory into the current directory) ls -l README.txt (note the timestamp for the file) touch README.txt (updates timestamp) ls -l README.txt cp README.txt README_copy.txt (create a copy of a file) cp README.txt FAKEP1_YOUR_NAME (create a copy in a directory) ls FAKEP1_YOUR_NAME (this directory should have two files in it) cp -r FAKEP1_YOUR_NAME COPY_FAKE (this copies a whole direcotry) ls COPY_FAKE (all contents were copied as well) rm -r COPY_FAKE (delete a directory and all of its contents) USE rm -r WITH CAUTION! mv funny_file2 README_copy.txt FAKEP1_YOUR_NAME (move multiple files to a directory) 8. Submitting a project directory cd You should have a directory called FAKEP1_YOUR_NAME in your home directory. Let's say this contains your workings for Project 1. You need to upload it to alfven, archive it, and submit it. This is one way to do it. scp -r FAKEP1_YOUR_NAME user_name@alfven.physics.mun.ca: Don't forget the colon at the end. Use your own username. You will need to supply your password. ssh user_name@alfven.physics.mun.ca (supply password) ls -lrt You should see the directoy FAKEP1_YOUR_NAME now in your home directory on alfven. If this were a real project submission, you would go into the directory and make sure everything runs properly on alfven. Create an archive with tar -zcvf Fake_P1_YOUR_NAME.tgz FAKEP1_YOUR_NAME Copy the archive to a special directory on alfven cp Fake_P1_YOUR_NAME.tgz /home/Submissions/ ls -l /home The Submissions directory has interesting permissions. You can write to it (copy files to it), but you can't read it. exit (logout from alfven) 9. Aliases Typing out long commands can be tedious. You can create a short form of the command with alias alf="ssh -X saika@alfven.physics.mun.ca" (use your username at alfven) Now instead of typing out ssh -X saika@alfven.physics.mun.ca you can just use alf Use nano to add the following lines to the .bashrc file in your home directory (create the file if it is not there). (On macOS, your default shell might be zsh, in which case you should edit the .zshrc or .zprofile file.) alias alf="ssh -X saika@alfven.physics.mun.ca" alias ll="ls -lrt" Now open a new terminal. In that new terminal type alias The two aliases should appear. If you update your .bashrc, you can engage the changes without opening a new terminal by typing source ~/.bashrc 10. A couple of short cuts. While on the command line ^A (Ctrl-A) brings you to the beginning of the line ^E brings you to the end of the line ^U delete the line up to the cursor position