LINUX COMMANDS

 a)  Basics Commands

1.     echo SRM               🡺 to display the string SRM

2.     clear                        🡺 to clear the screen

3.     date 🡺 to display the current date and time

4.     cal 2003                  🡺 to display the calendar for the year 2003 cal 6 2003                                         🡺 to display the calendar for the June-2003

5.     passwd                    🡺 to change password

6.     free –m                   🡺 to view the size of RAM in MB free –g       🡺 to view the size of RAM in GB

7.     df –h                       🡺 to view the disk space available and used.

8.     uptime                    🡺 to view the system up time

9.     bc    🡺 to open a basic calculator

10.  ps    🡺 to view the current terminal running processes

11.  history                    🡺 to get the history of all the past commands

12.  whoami                   🡺 to know which user i am

 

b)  Working with Files

1.    ls     🡺 list files in the present working directory

ls –l                          🡺 list files with detailed information (long list) ls –a        🡺 list all files including the hidden files

ls –r root                  🡺 list the directory recursively

ls –lh                        🡺 list the current location content in human readable format ls –lt      🡺 to list the files based on modification time

ls –li                        🡺 to view the inode number of files and directories lscpu      🡺 to view the system specifications

2.     cat > f1                   🡺 to create a file (Press ^d to finish typing)

3.     cat f1                       🡺 display the content of the file f1

4.            wc f1               🡺 list no. of characters, words & lines of a file f1 wc –c f1 🡺 list only no. of characters of file f1

wc –w f1                 🡺 list only no. of words of file f1


wc –l f1                  🡺 list only no. of lines of file f1

5.     cp f1 f2                  🡺 copy file f1 into f2

6.     mv f1 f2                 🡺 rename file f1 as f2

7.     rm f1                      🡺 remove the file f1

8.     head –5 f1              🡺 list first 5 lines of the file f1 tail –5 f1     🡺 list last 5 lines of the file f1

 

c)  Working with Directories

1.     mkdir elias            🡺 to create the directory elias

2.     cd elias                  🡺 to change the directory as elias

3.     rmdir elias             🡺 to remove the directory elias

4.     pwd                        🡺 to display the path of the present working directory

5.     cd   🡺 to go to the home directory cd ..      🡺 to go to the parent directory

cd -                        🡺 to go to the previous working directory cd / 🡺 to go to the root directory

d)  File name substitution

1.     ls f?    🡺 list files start with ‘f’ and followed by any one character

2.     ls *.c    🡺 list files with extension ‘c’

3.     ls [gpy]et   🡺 list files whose first letter is any one of the character g, p or y and followed by the word et

4.     ls [a-d,l-m]ring 🡺 list files whose first letter is any one of the character

from a to d and l to m and followed by the word ring.

e)  I/O Redirection

1.  Input redirection       wc –l < ex1 🡺 To find the number of lines of the file ‘ex1’

2.  Output redirection           who > f2    🡺 the output of ‘who’ will be redirected to file f2

3.     cat >> f1                            🡺 to append more into the file f1

 

f)  Piping

Syntax : Command1 | command2

Output of the command1 is transferred to the command2 as input. Finally output of the command2 will be displayed on the monitor.

ex. cat f1 | more 🡺 list the contents of file f1 screen by screen head –6 f1 |tail –2 🡺 prints the 5th & 6th lines of the file f1.

a)  Environment variables

1.  echo $HOME                         🡺 display the path of the home directory

2.  echo $PS1                              🡺 display the prompt string $

3.     echo $PS2                            🡺 display the second prompt string ( > symbol by default )

4.     echo $LOGNAME               🡺 login name

5.     echo $PATH                         🡺 list of pathname where the OS searches

for an executable file

b)    File Permission

-- chmod command is used to change the access permission of a file.

 

Method-1

Syntax :                    chmod [ugo] [+/-] [ rwxa ] filename

u : user, g : group, o : others

+ : Add permission - : Remove the permission r : read, w : write, x : execute, a : all permissions

ex.                 chmod ug+rw f1

adding ‘read & write’ permissions of file f1 to both user and group members.

 

Method-2

Syntax :                    chmod octnum file1

The 3 digit octal number represents as follows

      first digit                        -- file permissions for the user

      second digit                    -- file permissions for the group

      third digit                       -- file permissions for others

Each digit is specified as the sum of following

4 – read permission,            2 – write permission, 1 – execute permission ex.                                             chmod 754 f1

it change the file permission for the file as follows

      read, write & execute permissions for the user ie; 4+2+1 = 7

      read, & execute permissions for the group members ie; 4+0+1 = 5

      only read permission for others             ie; 4+0+0 = 4


QUESTIONS FOR PRACTICE:

 

Q1. Write a command to cut 5 to 8 characters of the file f1.

$

 

Q2. Write a command to display user-id of all the users in your system.

$

 

Q3. Write a command to paste all the lines of the file f1 into single line

$

 

 

Q4. Write a command to cut the first field of file f1 and second field of file f2 and paste intothe file f3.

$

 

 

Q5. Write a command to change all small case letters to capitals of file f2.

$

 

 

Q6. Write a command to replace all tab character in the file f2 by :

$

 

 

Q7. Write a command to check whether the user judith is available in your system or not.(use grep)

$

 

Q8. Write a command to display the lines of the file f1 starts with SRM.

$

 

 

Q9. Write a command to display the name of the files in the directory

/etc/init.d that contains the pattern grep.

$

 

 

Q10. Write a command to display the names of nologin users. (Hint: the command nologin is specified in the last filed of the file /etc/passwd for nologin users)

$

 

 

Q11. Write a command to sort the file /etc/passwd in descending order

$


Q12. Write a command to sort the file /etc/passwd by user-id numerically. (Hint : user-id is in 3rd field)

$

 

 

Q13. Write a command to sort the file f2 and write the output into the file f22. Also eliminate duplicate lines.

$

 

 

Q14. Write a command to display the unique lines of the sorted file f21. Also display the number of occurrences of each line.

$

 

 

Q15. Write a command to display the lines that are common to the files f1 and f2.

$



Post a Comment

Previous Post Next Post