Commands: General format: Command Option Argument Command: ls - Lists the contents of a file. Option: Begins with minus sign (-) ls a Lists including the hidden files. Argument refers to the name of a file-.(dot). Path Name (Identifies the target file) Path for BIN {/bin- The first slash refers to the root directory} {/usr/bin} TWO TYPES OF PATH NAME Absolute path name [complete path] Relative path name Path names for job directory {/home/kumaran/personal/job}-absolute {/personal/job}-relative Identifying the path of working directory: pwd command Path of working directory Present working directory Print working directory [ilamathi@localhost ilamathi]$ pwd /home/ilamathi Changing the current directory: Current user ilamathi wants to switch over from home directory to the bin directory. The steps are [ilamathi@localhost ilamathi]$ pwd Page 1 of 12
/home/ilamathi [ilamathi@localhost ilamathi]$ cd/usr/bin [ilamathi@localhost ilamathi]$ pwd /usr/bin To change from current directory to parent directory give (cd..) command at the $ prompt. [ilamathi@localhost bin]$ cd.. [ilamathi@localhost/usr]$ pwd /usr NOTE: The cd command without any path name will always take the user to his /her home Directory The Tilde Sign (~) (Substitute for home directory) 1. Kumaran is currently in the directory /usr/bin wants to go to his personal directory.the easiest way to achieve this is the use of tilde sign and /personal. [kumaran@localhost bin]$ ~/personal /home/kumaran/personal [kumaran@localhost personal]$ cd ~ [kumaran@localhost kumaran]$ pwd /home/kumaran 2. Kumaran is in personal directory and he wants to go to his health directory [kumaran@localhost personal]$ cd.. /health [kumaran@localhost health]$ pwd /home/kumaran/health. Subdirectory- Directory under another directory Creating a Directory(mkdir): [kumaran@localhost health]$ mkdir insurance A new directory named insurance is created Removing a Directory (rmdir): [kumaran@localhost health]$ rmdir insurance A directory named insurance is removed Conditions for removing a directory The directory should be empty Should not be the working directory Page 2 of 12
Listing the contents of a directory: Kumaran wants to know the names of files and subdirectories of /home/kumaran [kumaran@localhost kumaran]$ ls /home/kumaran Health cprogramming c++programming personal [kumaran@localhost kumaran]$ ls F. Health/c programming/c++ programming/personal/. F adds slash at the end of the directories and subdirectories. l gives information about the files. Other Commands: The clear command clears the screen The same thing is done by tput clear command also The command tput cup 20 20 will position the cursor at row 20,column 20 Extracting the Help: Use of man command will lead us to know about the particular command $ man ls {help for ls command} The Echo Command: Displays a message to the user $ echo n Please enter your name.-n places the cursor in same line. File Commands: Cat-displays the contents of a file If the file is too lengthy, more command is used to show one page at a time Press f to move forward and b to move backward The tee command does double the work of cat command. It is just like Tpipe It takes the input from standard input and displays the contents on screen Stores the same in the file specified The LINUX files are arranged in a continuous stream of bytes Input data stream-standard input Output data stream-standard output Input: $ cat file 1 Output: contents of file 1 Input: $ more file 1 Output: contents of file 1 page by page Page 3 of 12
Redirection and Piping: 1. $ cat file 1 > file 2: copies the contents of file 1into file 2 where file 2 is empty If file 2 exists then its destroys file 2 and copies the contents of file 1 2. $ cat file 1 > file 1: copies file 1 into file 1 since file 1 exists and the right hand side is executed first, file 1 is destroyed. Then, left hand side is executed. Noclobber: Prevents overwriting an existing file by redirection operation Append (>>): To overcome the difficulty of overwriting the existing file, append operator is used. Example: $ cat file 1 >> file 2: appends the contents of file 1 to file 2 without overwriting. Standard Input: Commands receive data from the standard input. It is either connected to device or to file. By default, it is connected to the device KEYBOARD. The characters typed into the keyboard are taken to the standard input and then directed to the command. $ cat (without any argument): This command expects data from the standard input KEYBOARD. Ctrl + D: End of file work. $ cat > file (standard input redirection operator) Message Ctrl + D $ cat > file 3: The typed in material will be redirected to the cat command through the standard input and this is sent to the file after the > symbol. Input Redirection Operator (<): $ cat < file 3 In order to make the cat command to get data from the file 3 (not from keyboard), the command used is $ cat < file 3. Since there is no > symbol, the message is sent to the screen. If the standard input is to be redirected to receive its data from file 3 and the standard output is to be redirected to place the data in file 4, the command used is, $ cat < file 3 > file 4. Piping ( ): Page 4 of 12
Redirection operator works only on files. If you want to send the output of one command to another command, pipe is the only command used. Godown -> Carpenter -> Painter -> Godown Wood -> Chairs -> Painter chairs -> Godown Here, the chairs are taken to the painter through conveyor belts. And then to godowns through lorries. The action of conveyor belt is piping and that of lorry is redirection File is storage medium to store data. Command is a program or set of instructions. You can save data into or retrieve data from a file. A command may read from or save data into a file but command itself cannot store the data. Redirection simply places the output in a file but piping sends the output to another command. 1. $ cat file 3 lpr: The cat command gets the contents of the file 3 and sends it to the standard output. The output is piped to the lpr (line print). The lpr takes the standard output as the input and sends it to the printer. Thus pipes receive data from the command placed before the pipe and send it to the command placed after pipe. 2. $ cat n file 3 lpr: The cat command with n option sends the contents of file 3 to the standard output after numbering each line 3. $ cat file 3 more: Used if the length of the contents is large 4. $ cat file 1 file 2 file 3 more: displays the contents of more than one file with the line numbers. Each file is separated by a space and no comma is used. Sort Command This command sorts each line of the given file alphabetically and sends the sorted version to the standard output $ sort file 3 more $ sort file 3 cat - more $ sort file 3 cat n lpr $ sort file 5 tee file 6 : copies file 5 to file 6 but also displays the contents of file 5 on screen. $ sort file 5 tee s file 5 : sorted contents of file 5 can be copied into another file and displayed on screen. Here, s file 5 is the sorted file of file 5. The sorted file s file 5 is displayed on screen $ sort file 5 tee s file 5 lpr : sort - copy print Copying file: $ cp [options] < source file < destination > Page 5 of 12
$ cp file 1 file 6: copies the contents of file 1 to file 6. Option I in the above command is used for getting a warning from the system before over writing so that you can stop copying process. Input: $ cp i file 1 file 2 Output: overwrite file 2? n $ $ cp r alpha alpha 1 Copy a directory recursively using cp command with the r option -s option creates a symbolic link -v explains in detail what is being done. Removing file: $ rm file 1 file 2: file1, file 2 are removed in current directory $ rm r alpha : removes alpha 1 directory along with sub directories. Wild Cards: $ ls : all files $ ls *.c: list on files only with extn.c $ ls doc[12]: list files that start with doc and end with either 1 or 2 $ ls char? :? fixes number of characters. $ doc [1-5] doc [1-g] : specifies ranges. Edit Commands: Edit command line with the help of Left arrow (ctrl + B) Right arrow (ctrl + F) Back space (ctrl + H) Delete (Erase a char) Deletes a line (ctrl + U) Can enter more than one command in a same line but separate them by a semicolon. Moving and renaming files: mv [option] <source> <destination> Move mv command is used to move or rename a file $ mv temp temporary: Renames temp as temporary. Viewing system date and time: Refer page 155, 156. File Systems Mount and Unmount: Page 6 of 12
Establishing the connection between file system on a storage device and your main directory tree is calling mounting the device. This is done by Mount command. Here the command line prompt changes from $ to # Mount has two parameters. Storage device and Directory Device is a special device file that connects your system to the hardware device. Syntax: # mount device destination. E.g. for mount: # mount /dev/fd0/destination Device files are located in the /dev directories Fd0: first floppy disk drive Fd1: Second floppy disk drive Hda1: hard disk drive, first partition on the first hard drive. E.g. for Unmount: # unmount/dev/fd0 Unmount floppy disk mounted to the /destination directory Note: You can never unmount file system that you are currently working in. Mounting and formatting floppy disk: Mount: # mount/dev/fd0/mnt/floppy # unmount/dev/fd0 (or) #unmount/mnt/floppy, #mount/mnt/floppy Mkfs: makes formatting, commands (formats a floppy) Mounting- CD ROMs # mount/mnt/cdrom # unmount/mnt/cdrom # mount/dev/hdc/destination Vi Editor: Editors are used for creating, deleting and editing the files. The two types of editors are Ed (line editors) and VI (visual editors). Editors use keyboard for two main purposes. 1. To specify editing commands 2. To receive character input. Page 7 of 12
The keys used are the alphabetic character keys, function keys and control keys. Editors in linux use minimum number of keys. They are the alphabetic characters, control characters, ESC key and Enter key. The modes of VI editor are command mode and input mode. Command mode: The key (x) acts as delete key and erases a character where the cursor is on. Input mode: The same key (x) simply adds x to file. ESC keys help to check in which mode we are working. If already in command mode, no beep sound is heard. Beep sound indicates that we are in command mode at present but before in input mode. Creating, editing, saving, and quitting a file in VI: $ vi filename: edit/create a new file with the given filename If the file exists, the contents of file is listed page by page, otherwise empty screen is screen. Press a, I, or o: You will be taken to the input mode. After entering data, press ESC key. It will take you to command mode. Press upper case ZZ which saves your file and then exits vi editor. Press ESC key to change the command mode and press : to go to the line editing mode. Press w. This action saves the file and returns to command mode. The command :w (filename) is equivalent to save as in other word processors. $ vi (Creates a file without name) :w some name will save the file untitled 1. [contents of the buffer are copied into some name file] Clash of file name will not be tolerated. If a file exists already, the name will be rejected. We have to try with another name. Quit vi editor: q [takes to the shell] q! [out of vi editor without saving the saves] Moving keys: h - Left arrow j- Down arrow l- Right arrow k- up arrow Ctrl + F - moves the screen forward Ctrl + B - moves the screen backward Line number G: (Some line number) G: Takes you to the particular line. Simply G moves to the end of a file. Page 8 of 12
Word wrap: : set wm=col no.: Sets word wrap margin. Std: XI CHAPTER-3 LINUX Commands for inserting text: The lower case (a) key enters text input mode and appends the text after the cursor. The lower case (i) key enters text input mode and appends the text after the cursor. The lower case (o) key enters text input mode by opening a new line immediately below the current line Commands for Deleting text: x- deletes the current character dd- delete the current line nx- delete n characters from current position ndd- deletes n lines starting from the line where the cursor is on or erased. undo: press n (Undo last modification) Break a line: Press Enter key into the input mofe Press upper case (J) key to join two lines Moving: ndd delete n nos of lines p- insert deleted line (paste) Copying: yy- copies a line nyy- copies n lines fom the cursor into the buffer p- Similar to (xv paste) Searching: /-allows to search the pattern forward?.. allows to search the pattern backward. Changing: cc change entire contents of a line r - replace the current character R allows you to overwrite text cw- allows to change a word dw- allows to delte a word. Page 9 of 12
Shell Script: It is a text file that contains linux commands. A shell script is handy as you need to enter the command only once. Shell script allows input/output operations and manipulation of variables. Executing shell script when you logon to the linux system, you get a copy of the shell to work with. This shell is known as login shell. Default shell is BASH shell. It has the capabilities of the programming languages. Creating a shell: By giving sh command in the command prompt, a new shell is created. This new shell is known a sub shell or child shell of the current shell, which can be used to execute a shell script. You will have read and write privileges but execute permission is not granted immediately. Execute/Run a shell program: $ sh file name $. file name If you want to run a shell script directly at $ prompt, you can change FAP ( File access permission) by granting execute permission. This is done using chmod command $ chmod u + x edufile $ edufile +x with chmod gives the execute permission to any user u + x : Owner of file gets the execute permission Variables: They are place holders to store values. All linux variables are treated as character strings. Mathematical operations can be done using expr and let commands. Rules for creating variables: refer page 173 Syntax for creating variables: <variable _name> = <value> Name = Ezhil Name = Ezhil Name =Ezhil (The quotes are optional) Referencing Variables: $ symbol is need to refer the contents of a variable Page 10 of 12
var1=${var2}, var1, var2 are memory locations ${var2} command extracts the value found in that location Concatenation: If you want to concatenate the contents of one variable with another value, the braces are essential $ son1=$ {father} son, where father =john; add son to john. Store the result in son1. Reading a value into variable: echo Please enter your name -> prints string on screen Expressions: expr, let commands $ expr 21 + 51 displays 72 on screen $ let <value1><operator><value2> $% let pr = 5*10 echo The product of $pr Output: The product is 50 Command Substitution: $ date - Access current date of the system. echo Today s date is date + %D, mm/dd/yy Command substitution can also be used to store the output of a command in a variable: cfiles= ls*.c\wc-l cfiles Variables contains the no of files in the current directory where names end with.c Output to be stored in variable using command substitution: $ var1=5 $ var1= expr $var1 + 20 assigns 25 to var1 Page 11 of 12
. Page 12 of 12