Linux Shell Commands and Vi Editor

Linux Shell Commands and Vi Editor

__________________________________________________________________________ THE HONG KONG POLYTECHNIC UNIVERSITY Department of Electronic and Information Engineering ENG224 Information Technology Laboratory 1 Linux Shell Commands and vi Editor Objective: To practice the usage of Linux shell commands and vi editor Equipment: PC with Ubuntu Linux 5 preloaded. vi editor. Methodology: Start up the system. Login the system with username student. Consult your tutor for the password required. Answer all the questions in this lab sheet. 1. Introduction What is a shell? The shell is the command line interface to Linux. It is similar to the Microsoft DOS prompt as known to Windows users, but is much more powerful with its scripting languages. Currently the standard setup of Linux has included two shells, bash and tcsh. What is a shell command? Users coming from the DOS environment are probably familiar with the concept of commands that encompass core features of the operating system, such as DIR, COPY, and ATTRIB. These commands provide the base on which more complicated actions could be built and from which sophisticated batch files could be written. But in the DOS world, as in many operating systems, the number of available commands is limited and generally static: users cannot add new commands. In Linux, and also in Unix, the concept is different. A command is any executable file. This means that any executable file added to a system becomes a new command on the system. How to use Linux Shell inside the X-Window system? A terminal emulator is a program that opens up a window and then runs a shell in that window. It could be compared to the DOS prompt in Windows. You start a terminal emulator like any other X program, so it means that you can have a command line while you are still safely in the GUI! There are a bunch of different terminal emulators out there, like xterm, rxvt, gnome-terminal, konsole, kvt, eterm, and many others. We use gnome-terminal to practice the exercise in this tutorial. Open a console window by pressing the upper-left corner button "Applications", "System Tools" and then "Terminal" (Not Root Terminal). You may also open a new terminal by a single right click on the desktop, then choose Open terminal from the pop up menu. A terminal window similar to the one on Figure 1 will be shown. Page 1 __________________________________________________________________________ Figure 1 The Terminal in Ubuntu Linux 2. Common Linux Commands a. Find the usage of commands If you don’t know how to use the Linux shell commands, you can use man (which stands for MANual) command to show you the usage of them. Usage: man <command-name> Example: man ls (press q when you finish reading the manual of the command ls) b. Finding out where you are The pwd command (which stands for Present Working Directory) is the one of the most basic commands in Linux. By typing the command and hitting Enter, you will be informed of which directory you are currently in. Usage: pwd Exercise 2.1 Type “pwd” in the command line. What is the result? __________________________________________________________________________________ Page 2 __________________________________________________________________________ c. Change directory The cd command allows you to change your current directory to any accessible directory on the system. Usage: cd <directory> Example: cd /usr/X11R6/bin Note: using the command cd without applying any parameter will bring you back to your home directory. d. Listing the contents of a directory The ls command can be used to view the contents of the current directory. Usage: ls [option] <filename> Example: ls –al / Exercise 2.2 Type “cd /” and “ls” in the command line. What is the result? __________________________________________________________________________________ Exercise 2.3 Type these commands “cd /usr/X11R6/”, “cd ./bin/”, “cd ..”, and “pwd”. Give the current directory which you are located. What are the meanings of . and .. ? __________________________________________________________________________________ e. Find, Grep and Cat The find and grep commands are powerful tools for searching files. While both commands are used for searching, their purposes differ: find is used to search for files by a number of criteria, including name or date of creation. grep is used to search the contents of files. The cat command combines one or more files and displays them on the standard output. If no files are given, the standard input is written to the standard output. Usage of find command: find <starting-directory> <parameters> <actions> Usage of grep command: grep <text-pattern> <file-list> Usage of cat command: cat <options> <filename> Exercise 2.4 Type "cd" to go back to your home directory. Type “cat > testcat.txt” in the command line. After pressing return, type the following line of text “This is a test of cat.”, and then press crtl-d. Type “cat testcat.txt” again. What do you see? __________________________________________________________________________________ For the command “cat > testcat.txt” above, the symbol > allows the output of the command cat (the keyboard input of user) to store into a file namely testcat.txt. Page 3 __________________________________________________________________________ Exercise 2.5 Type “find . -name testcat.txt -print” in the command line. What is the result? What are the purposes of the options -name and -print (use man find to find out their meanings)? __________________________________________________________________________________ Exercise 2.6 Type “find . -name testcat.txt –print > list.lst” in the command line. You will find a file “list.lst” in your current directory. Use cat commands to show its contents. What is the result? What is the meaning of > list.lst? __________________________________________________________________________________ Exercise 2.7 Go to “/” directory by the command "cd /" and type “ ls –l | grep “bin” ” in the command line. What is the result? What is the function achieved by this line of commands? (The character | refers to pipe. It allows the output from the left hand side of | to become the input of the command on its right hand side.) __________________________________________________________________________________ 3. File Management Linux Commands a. Copying files and directories To copy files, you can use the mv or cp commands. The command line “mv file1 file2” allows one to move file1 to file2. After the move, file1 will no longer exist. So functionally it is similar to renaming a file in DOS. Command cp on the other hand copies one file to another. The following will copy file1 to file2. Note that if file2 does not exist, it will be created; however if it does exist, it will be overwritten: Usage: cp file1 file2 There is NO undo command in Linux, so accidentally overwriting an important file would probably make you pull your hair out. The risk of doing so is smaller if you use the -i option ("interactive") with cp. The following does the same as the above, but if file2 exists, you will be prompted before overwriting: Usage: cp –i file1 file2 If you want to copy file1 into directory dir1: Usage: cp file1 dir1/ The following would do the same as the above, copy file1 into dir1, but under a different name: Usage: cp file1 dir1/file2 You can also copy multiple files into one directory with a single command: Usage: cp file1 file2 file3 dir1 For copying directories, you can use the cp and mv commands just like you use them with files. If you have already tried to copy a directory with cp, you should have probably noticed that cp just complains to you. The cp command wants you to use the -r option if you want to copy a directory with its contents. The -r means "copy recursively": Usage: cp -r dir1 dir2 Page 4 __________________________________________________________________________ Exercise 3.1 Change to your home directory by using the "cd" command. Create a subdirectory called “studentsub” using the command “mkdir studentsub”. Copy the files testcat.txt and list.lst to studentsub. While staying at your home directory, copy ./studentsub to another directory called ./studentsub2? Give the command that you have used. __________________________________________________________________________________ __________________________________________________________________________________ __________________________________________________________________________________ __________________________________________________________________________________ Exercise 3.2 Rename the “studentsub2” directory to “studentsub3” using mv. Give the command that you have used. __________________________________________________________________________________ b. Deleting files and directories The rm command is used for removing files. To remove a file: Usage: rm file1 If you use the -i option, you'll be prompted before removing the file: Usage: rm -i file1 You can also delete more files at once: Usage: rm file1 file2 There are two commands you can use for removing directories. If the directory is empty, you can use rmdir: Usage: rmdir dir1 You can use rmdir only if the directory is empty. If you want to remove a directory with all its contents, you can use rm with the -r option. The -r option tells rm to remove a directory Recursively: Usage: rm -r dir1 It can cause a lot of trouble with rm -r if you are not careful. Therefore, it might be a good practice to use the -i option when deleting a directory with its contents so that you would be prompted before each file in the directory

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    10 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us