California State University Fresno Essential Linux Harinder Bawa California State University Fresno
Editors In order to do many tasks you will have to use an editor Edit Code Control analysis/programs In the linux world these all edit text files Word is not an editor Can run at the command line, or in window 5/19/2018 Dr. Harinder Bawa
Choice of an editor Nearly always available Don’t require xwindows Command line editors vi vim pico/nano Xwindow editors Xemacs/emacs nedit Nearly always available Don’t require xwindows fast Not very powerful and no access to mouse 5/19/2018 Dr. Harinder Bawa
vi Editor The most readily available Generally installed as standard on all systems Quite powerful But difficult to use or at least get used to Some people swear by vi Its important to know some basics in case vi is the only option for a particular task. 5/19/2018 Dr. Harinder Bawa
Basic commands of vi vi filename (open file to edit) Two modes of operation Insert mode: Used for typing ( after typing i) Command mode:- used to issue commands(Esc + command) :q to quit without saving (!q= quit & discard changes) u = undo the last change x = Delete a character; dd= delete full line :wq to quit after saving 5/19/2018 Dr. Harinder Bawa
vi: Cheat sheet 5/19/2018 Dr. Harinder Bawa
Editing a file using pico or nano Type “pico” or “nano” at the prompt 5/19/2018 Dr. Harinder Bawa
Editing a file using pico To save use “ctrl-x” 5/19/2018 Dr. Harinder Bawa
Compiling a C++ Program To compile a c++ program, use the g++ command. “g++ helloworld.cpp” Provided there are no errors, this will create an executable file called a.out. If you want to name your executable file, use the -o flag to specify a name. “g++ helloworld.cpp -o helloworld.out”
Running a C++ Program Running a c++ program is easy, just type in the name of the executable file! “a.out” There could be a minor issue however. If for some reason, that doesn’t work, try preceding the name with a ./ “./a.out”
OTHERS WAY TO DISPLAY A FILE CONTENTS AND SEARCH INSIDE IT 5/19/2018 Dr. Harinder Bawa
Displaying a file Various ways to display a file in Unix cat less/more head tail 5/19/2018 Dr. Harinder Bawa
Command: cat Dumps an entire file to standard output Good for displaying short, simple files 5/19/2018 Dr. Harinder Bawa
Command: less/more “less” displays a file, allowing forward/backward movement within it return scrolls forward one line, space one page y scrolls back one line, b one page use “/” to search for a string Press q to quit The file contents are displayed by ‘cat’ command. The contents will scroll off the screeen, when the file contents are more than one screen page. The file contents are displayed page by page with the use of ‘more’ command. 5/19/2018 Dr. Harinder Bawa
Command: head “head” displays the top part of a file By default it shows the first 10 lines -n option allows you to change that “head -n50 file.txt” displays the first 50 lines of file.txt 5/19/2018 Dr. Harinder Bawa
Command: head Here’s an example of using “head”: 5/19/2018 Dr. Harinder Bawa
Command: tail Same as head, but shows the last lines 5/19/2018 Dr. Harinder Bawa
Pipes and Redirection Programs can output to other programs called “piping Command > file- Redirect output to a file Command>>file- Append output to an existing file Command<file- Get input from a file, eg sort<file.txt Command<file1>file2- Get input from file1, and write to file2 Command | command- Pipe one command to another 5/19/2018 Dr. Harinder Bawa
Searching grep pattern files- search for pattern in files grep -r pattern dir- search recursively for pattern in dir Command | grep pattern- search for pattern in the output of command locate file- find all instances of file 5/19/2018 Dr. Harinder Bawa
A few examples of piping 5/19/2018 Dr. Harinder Bawa
Command: wc To count the characters, words, and lines in a file use “wc” The first column in the output is lines, the second is words, and the last is characters 5/19/2018 Dr. Harinder Bawa
A few examples of piping 5/19/2018 Dr. Harinder Bawa
Command: grep To search files in a directory for a specific string use “grep” 5/19/2018 Dr. Harinder Bawa
Command: diff To compare to files for differences use “diff” Try: diff /dev/null hello.txt /dev/null is a special address -- it is always empty, and anything moved there is deleted 5/19/2018 Dr. Harinder Bawa
LINUX SHELLS & ITS SCRIPTING 5/19/2018 Dr. Harinder Bawa
Shells The shells is a part of the interface for control of linux There are a number of options, the two most common are: tc (or c) shell (tcsh) bourne shell (bash) Change your shell with chsh Pick a shell and stick to it, but can be useful for a new account somewhere. 5/19/2018 Dr. Harinder Bawa
Linux Command Line Interface Linux shells: A shell is a command interpreter that allows you to type commands from the keyboard to interact with the operating system kernel. ■ sh (Bourne Shell) The sh shell was the earliest shell, being developed for UNIX back in the late 1970s. ■ bash (Bourne-Again Shell) The bash shell is an improved version of the sh shell and is one of the most popular shells today. It’s the default shell used by most Linux distributions. ■ csh (C Shell) The csh shell was originally developed for BSD UNIX. It uses a syntax that is very similar to C programming. ■ tsch The tsch shell is an improved version of the C Shell. It is the default shell used on FreeBSD(Berkeley Software Distribution) systems. ■ zsh (Z Shell) The Z Shell is an improved version of the bash shell. 5/19/2018 Dr. Harinder Bawa
Environment variables Much of your linux environment is set by environment variables These are access via the $sign eg $HOST To view your environment use env Prints out all your environment variables Use echo to view an individual one echo $HOST or echo $HOME 5/19/2018 Dr. Harinder Bawa
Setting environment variables You can set your environment variables using export (bash shell) export ROOTSYS=/user/local/root setenv(tc shell) setenv ROOTSYS /user/local/root This creates the variable if it doesn’t already exist or updates if it does Typically the environment variables you set yourself will be to tell programs where to find things. 5/19/2018 Dr. Harinder Bawa
Your PATH Your path is an environment variable $PATH (check by echo $PATH) It contains all the directories that your OS will look in to find the programs you want to run The path should contain “.” to ensure that you don’t need to specify it while running local program Entries are separated by “:” Update your path with setenv PATH /newdir:$PATH 5/19/2018 Dr. Harinder Bawa
Adding/Modifying the “PATH” 5/19/2018 Dr. Harinder Bawa
Bash Shell Startup When a bash shell is started, it automatically executes commands stored in certain files. There are three kinds of shells: 5/19/2018 Dr. Harinder Bawa
Note: Hidden files If you try the ls command in your home directory, you will (probably) notice that the file .bash_profile is not listed. Filenames that begin with a period are hidden by default. You can use the ls –a command will show hidden files as well as non-hidden files. 5/19/2018 Dr. Harinder Bawa
Ubuntu Variance When you open an interactive terminal session in Ubuntu, the sequence described on the preceding slide is NOT followed by default. In particular, ~/.bash_profile is not executed automatically, and therefore changes you make to it will not be effective. There is a simple fix for the issue: - open a terminal session and go to Edit/Profile Preferences - select the Title and Command tab - check the box for “Run command as a login shell” In fact, in my Ubuntu installation, ~/.bash_profile did not exist initially; I had to create it with editor. 5/19/2018 Dr. Harinder Bawa
~/.bash_profile You should use ~/.bash_profile to set changes to the PATH variable because ~/.bash_profile is only executed once. Here is a sample ~/.bash_profile And you know how to add PATH. Right? 5/19/2018 Dr. Harinder Bawa
A sample ~/.bashrc alias commands are a convenient way to create mnemonics(memory) for specialized execution of system commands. 5/19/2018 Dr. Harinder Bawa
Setup atlas environment in lxplus 5/19/2018 Dr. Harinder Bawa
Remote communication Most of your analysis is unlikely to be on your local machine. You need to login to bigger computing clusters e.g CERN’s “lxplus.cern.ch” or FresnoState “t3head.atlas.fresnostate.edu” provided you have account in that! Advantage Large Processing nodes for jobs Remote data storage for experiments Remote code repositories & databases . 5/19/2018 Dr. Harinder Bawa
ssh/scp ssh is used to securely log in to remote systems, successor to telnet ssh options [username]@[hostname]/ip ssh bawa@lxplus.cern.ch Type “exit” to log out of session 5/19/2018 Dr. Harinder Bawa
ssh/scp scp is used to copy files to/from remote systems, syntax is similar to cp: scp [local path] [username]@[hostname]:[remote file path] scp options file bawa@lxplus.cern.ch:path/. or scp options bawa@lxplus.cern.ch:path/file 5/19/2018 Dr. Harinder Bawa
Exercise-1 Download http://zimmer.csufresno.edu/~hbawa/ubuntu.txt Print number of words in the file Print number of lines in a file Command to scp transfer the file to your lxplus area login to lxplus and move the file to /tmp/username dir. Scp transfer file from lxplus /tmp/username to your current working dir. 5/19/2018 Dr. Harinder Bawa
Exercise-2 Download simple c++ code from http://zimmer.csufresno.edu/~hbawa/cplus.C Open using any editor (vi /pico) Remove second line and correct indentations. Save the file Compile using both c++ and g++ compiler Make executable name “myoutput” Run and print the output 5/19/2018 Dr. Harinder Bawa
Brain Teaser How to save the output text on the screen in a file named “screen.txt”. 5/19/2018 Dr. Harinder Bawa