You are on page 1of 23

Unix / Linux Commands

Linux is an Operating System created by Linus Torvalds It is a System Software which provides interface between hardware (peripheral devices) and user. Linux is multitasking operating system. It is open source s/w. (Modifications can be done) Commands are given in bash shell. Linux has two types of Operating System
GUI e.g. Windows Text based e.g. DOS (single tasking O.S)

Commands :
For adding a new user -> Syntax - adduser user_name For setting password for a user -> Syntax - passwd user_name For changing user -> Syntax - su user_name

**Some General Purpose Commands:


To view calendar -> Syntax - cal To view Date -> Syntax date -> Syntax date +%D (it will give date) -> Syntax date +%y (it will give year) -> Syntax date +%m (it will give month) To view Present Working Directory -> Syntax - pwd To display approx. processor time used by a program -> Syntax - clock

To view how many users logged in -> Syntax - who To view the current user logged in to the terminal -> Syntax - whoami -> Syntax - who am i

To check how many users logged in & what they are doing -> Syntax - w
To view current processes -> Syntax - ps -> Syntax - ps -l

To wait -> Syntax - sleep time To stop a background process -> Syntax - kill processID -> Syntax - Killall process_name To clear the terminal screen. -> Syntax- Clear To check in which shell you are working -> Syntax- echo $SHELL To show all the recent commands used -> Syntax- History

**Directory

and File related commands

To create a directory -> Syntax- mkdir dir_nm To call or change directory -> Syntax- cd dir_nm To create a file -> Syntax- touch file_nm To create and write into a file -> Syntax- cat > file_nm after typing text to come out type 'ctrl+d To add or append data in a file -> Syntax- cat >> file_nm to come out press 'Ctrl+d'

To come out of a directory -> Syntax- cd .. -> Syntax- cd \ (To directly come out of all dir. and come to the home dir. ) To view file contents -> Syntax- cat file_nm -> -> -> To view dir.contents Syntax- ls or ll Syntax- ls -a (To display the dir. contents with hidden files) Syntax- ls l (To display the dir. contents with file permissions)

To remove an existing directory -> Syntax- rmdir dir_nm -> Syntax- rm -rf dir_nm (to remove dir. forcefully with all dir.and files inside) -> Syntax- rmdir r directory_nm ( deletes the full directory with files)

To remove a file -> Syntax- rm file_nm -> Syntax- rm -f file_nm (forcefully deletes files) To copy a file into a dir -> Syntax- cp file_nm dir_nm To copy file contents from one file to another -> Syntax- cp -i myfile yourfile With the "-i" option, if the file "yourfile" exists, you will be prompted before it is overwritten. To move a file into a dir -> Syntax- mv file_nm dir_nm

To view directory tree -> Syntax- tree To count words,characters and lines in a file -> Syntax- wc file_nm (will provide no of lines , words and characters of files) -> Syntax- wc -l file_nm (will provide no of lines) -> Syntax- wc -w file_nm (will provide no of words) -> Syntax- wc -c file_nm (will provide no of characters) To search a file -> Syntax- find file_nm find Char* (will list all files starting with that char) find *char (will list all files ending with that char) e.g. find *txt (find text files) e.g. find *bmp (find bitmap files)

To search for a particular word or text in a file. -> Syntax- grep "string" file_nm (enter the string or word to search) grep [s][t][r][i][n][g] file_nm To get help there are two sets of 'Help' files 'man' and 'info. -> Syntax- man command_nm (enter name of command for which you require details) -> Syntax- info command_nm *To find a command for a particular task -> Syntax- man -k task_nm (for ex. man -k delete) To check environmental variables. -> Syntax- env

To sort data -> Syntax- sort [press enter] Enter data to sort and press ctrl+d -> sort -r (To sort in descending or reverse order) -> sort -n (To sort numbers) - > sort File_nm (to sort data in a file) To compare two files -> Syntax- Cmp file1 file2 (if both files are same then it displays $)

To use shortcut for a command. -> Syntax- Alias new command= old command e.g. alias clr=clear (For assigning alias to parent command) To revoke the shortcut. -> Syntax- unalias new command Head Output the first part of files. -> E.g. head -3 file_nm Tail Output the Last part of files. -> E.g. tail -3 file_nm (It will show last 3 lines of the file)

To change the access permissions of files and directories


= + U owner/ users present permission G Group users present permission O others present permission a all 3 Opcodes:-> absolute permission -> permission granted -> permission denied

e.g. chmod o=r file_nm (It allocates only read permission) e.g. chmod u+wx file_nm (It grants write and execute permission to user) e.g. chmod o-wx file_nm (It revokes write and execute permission of others)

Modes - R - Read - 4 - W - write -2 - X - Execute -1 This is using numbers E.g. Chmod 777 file_nm (it will give all three permissions for u, g and o) *Pipes & Filters: Pipe '|' operator: takes the input from first command or file & use as an input to the next command or file. Filters: 1.'more' 2. 'less' 3.'tail' 4.'head'

Question bank
Q.1: What is the core of Linux Operating System? a) Shell b) Kernel c) Command d) Script e) Terminal Q.2: What Linus Torvalds Created? a) Fedora b) Slackware c) Debian d) Gentoo e) Linux

Q.3: Linux is which kind of Operating System? a) Multi User b) Multi Tasking c) Multi Process d) All of the above e) None of the above Q.4: Under UNIX the key board is the default input device and the monitor is the default output device a) True b) False

Q.5. What command is used to count the total number of lines, words, and characters contained in a file? a) Countw b) Wcount c) wc d) count p e) None of the above Q.6. What command is used to remove files? a) dm b) rm c) Delete d) erase e) None of the above

7) Explain the following Commands. i)Grep ii)Sort 8) Explain Linux Command. a)Who b)Chmod 9) What can you type at a command line to determine which shell you are using? 10) Create the following Linux command.
i) Create the two file and insert appropriate data. ii) Compare both the file and find if any difference is there in the file. iii) Change the access permission of file to read . iV)Display first two lines of a file.

11) Why it is necessary to give permission to the files? Name the types of permission that given to user or group .

12) What account is created when you install Linux?


13) Linux is widely used in most of the organization .what makes it different from rest of Operating system?

14 )Create a below tree structure word enclosed in square brackets are directory. / | [Company] | [CEO] | [General Manager] / | \ [Pur_Manager] [Sales_Manager] [Prod_Manager] / \ [Employee1] [Employee2] | | Employee.txt Employee1.txt i) Make company as current directory and display its listing. ii) Copy a file Salary.txt to the directory Sales_Manager. ii) Display the content of file Employee.txt.

15) Create a statecapital.txt file with below content. Name of State Goa Maharashtra Karnataka capital Panaji Mumbai Bangalore area 10,000 sq.km 2,30,000 Sq.km 1,89,000 sq.km

i) Search word Panaji in the file statecapital. ii) Display the first 3 lines of file statecapital.txt.

16) Create a below tree structure word enclosed in square brackets are directory. / | [Family] | [Head] | [Grand Parent] | [Parent] | [Children] / \ Child1.txt Child2.txt

i)Display the content of file child1.txt. ii)Rename the directory children. iii)Copy the both the file child1.txt and chld2.txt under directory parent

You might also like