Shell Commands
Total Page:16
File Type:pdf, Size:1020Kb
Operating Systems Lab Islamic University – Gaza Engineering Faculty Department of Computer Engineering Fall 2012 ECOM 4010: Operating Systems Lab Eng: Ahmed M. Ayash Lab # 5 Shell Commands October 6, 2012 Linux Shell (Terminal): Shell Definition: is a special Linux component that acts as interface between user and kernel. Shell accepts user commands in English and passes them to the kernel to execute. Shell is a command language interpreter that executes commands read from keyboard or from a file. Shell is not part of the kernel; it uses it to execute programs. Shell Types: 1- BASH (Bourne-Again SHell): it is a freeware shell and most common shell in Linux. 2- CSH (C Shell): The C shell's syntax and usage are very similar to the C programming language. 3- KSH. 4- TCSH. 5- ZSH 6- BSH Tip: To find all available shells in your system type following command: $ cat /etc/shells By default, the BASH shell has a dollar sign ($) prompt, but Linux has several other types of shells, each with its own prompt. A shell prompt, such as the one shown here, marks the beginning of the command line: $ Structuring Commands - Commands have the following syntax: command options arguments 1 Some Shell Commands Command Syntax Function man $ man {command name} Get help about commands. info $ info {command name} Get help about commands. --help $ {command name} --help Get help about commands. whatis $ whatis {command name} Get help about commands. cat $ cat {file name} Display text file. date $ date Display current date and time $ date --date= '2012-10-10' Set the date to 2010-10-10(y,m,d). $ date --date='2012-10-10 Set the date to 2010-10-10 and the 11:30 AM' time to 11:30 AM. more $ more {file} Display text file content in the terminal. cp $ cp {source path} Copy Files. {destination path} du $ du [directory name] Display the disk usage according to space used by file/directory. ls $ ls [file/dir name] Show list of files and directories. pwd $ pwd Print working directory. vi $ vi {file name} Create text file. rm $ rm {file name} Delete a file. $ rm –rf [directory name] Delete directory and all its sub- directories and files. cd $ cd [new dir] Change the current directory. mkdir $ mkdir [folder name] Create a new folder. Who am i $ who am i About the user. clear $ clear Clear terminal screen. file $ file {filename} Determining File Content cal $ cal Display calendar halt / shutdown $ sudo halt To shutdown / poweroff Ubuntu $ sudo shutdown -h now Linux reboot $ sudo reboot To reboot Ubuntu Linux echo $ echo {text} Display a line of a text nl $ nl {filename} Number lines of files 2 less $ less {filename} Displaying the contents of a file on the screen Some Commands in details 1. ls (list short) ls [options] [files_or_dirs] This command is used for finding out what is in the current directory. It lists the contents of the current working directory. ls –a: used for listing all files including hidden. ls –l : used for listing all files in details excluding hidden files using long listing format. 2. Changing Directories cd changes directories: - To an absolute: cd /etc/squid/ or relative path cd ../lab2 - To a directory one level up: cd .. - To your home directory: cd or cd ~ - To your previous working directory: cd – 3. Removing Directories - rmdir removes empty directories. Usage: rmdir [options] [name of dir] Example: rmdir dir1 rm -r [dir name] recursively removes directory trees. –f: (--force) do not ask any thing. rm -rf dir1 4. Creating Files touch create empty files or update file. We can use this command in multiple ways: 3 For example to create three files named: file1, file2, file3: 1. touch file1 file2 file3 2. touch file{1,2,3} 3. for i in $(seq 1 3) ; do touch file$i ; done 5. Copying Files and Directories cp - copy files and directories. cp [options] file destination More than one file may be copied at a time if the destination is a directory: cp [options] file1 file2 destination Note: - If the destination is a directory, the copy is placed there. - If the destination is a file, the copy overwrites the destination. 6. Moving and Renaming Files and Directories mv - move and/or rename files and directories mv [options] file destination - More than one file may be moved at a time if the destination is a directory: mv [options] file1 file2 destination 7. A Text Editor using vi . Syntax : vi <filename> . Entering Input Mode a Add text after the cursor. i Insert text before the cursor. R Replace text starting at the cursor. o Insert a new line after the current one. Exiting or Saving Your File :wq Save the current changes to the file and exit from vi. :w Save the current file but continue editing. :q Quit the current file. This works only if you don’t have any unsaved changes. 4 :q! Quit the current file and don’t save the changes you just made to the file. ZZ Same as :wq . Entering Command Mode esc Switch from Input mode to Command mode. Moving :# move to line # :$ move to last line of file Tips: 1. Some commands can be complex and take some time to execute. When you mistakenly execute the wrong command, you can interrupt and stop such commands with the interrupt key CTRL-C. 2. Don’t forget to use: . <Tab> button to complete any command. <q> button to exit any opened file. You can edit text files using graphical editors like: gedit. Exercises: 1. What is the output of hostname command? 2. Create the following tree of directories and files using Linux shell commands: Temp1 Temp2 Temp3 file1 Write Lab # file2 file3 Write your Write hello name and ID world! 5 .