Linux Basic Commands

  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Linux Basic Commands as PDF for free.

More details

  • Words: 2,325
  • Pages: 16
LINUX BASIC COMMANDS CAT COMMAND: cat command helps us to list the contents of a file we specify.If we do not specify the file, it takes input from the standard input. For eg. [cse07340@localhost cse07340]$ cat > preeti.c 1 samrat preeti 2 4 6 7 Gunjan 12 14 nupur 15 Arjun 9 mohit 6 mayank 3

DATE COMMAND: The date command tells us the current date and time. For eg.. [cse07340@localhost cse07340]$ date Thu Aug 27 12:27:37 IST 2009

WHO AM I COMMAND: It gives us detail regarding the login time and the system's name for the connection being used. For eg.. [cse07340@localhost cse07340]$ who am i cse07340 pts/17 Aug 27 11:50 (192.168.1.68)

WHO COMMAND: Who command gives us the details of the users who have logged into the linux system currently. For eg.. [cse07340@localhost cse07340]$ who cse07354 pts/0 Aug 27 11:28 (192.168.1.75) cse07341 pts/2 Aug 27 11:30 (192.168.1.80) cse07334 pts/4 Aug 27 11:31 (192.168.1.52) cse07351 pts/7 Aug 27 11:35 (192.168.1.77) cse07363 pts/10 Aug 27 11:36 (192.168.1.55) cse07334 pts/12 Aug 27 12:04 (192.168.1.52) cse07360 pts/11 Aug 27 11:37 (192.168.1.79) cse07339 pts/1 Aug 27 11:39 (192.168.1.76) cse07358 pts/5 Aug 27 11:39 (192.168.1.60) cse07346 pts/14 Aug 27 11:40 (192.168.1.61) cse07334 pts/15 Aug 27 11:48 (192.168.1.52) cse07352 pts/3 Aug 27 11:50 (192.168.1.73) cse07340 pts/17 Aug 27 11:50 (192.168.1.68) cse07354 pts/18 Aug 27 11:52 (192.168.1.68)

PWD COMMAND: It displays the full path name for the current directory we are working in. For eg.. [cse07340@localhost cse07340]$ pwd /home/cse07340

LS COMMAND: It displays the list of files in the current working directory. For eg.. [cse07340@localhost cse07340]$ ls ! dsa12.c dsa3 oops10 oops17.cpp oops5.cpp 1 dsa13 dsa3.c oops10.cpp oops18 oops6 19 dsa13.c dsa4 oops11 oops18.cpp oops6.cpp cse07340.c dsa17.c dsa7 oops14 oops2.c oops9.cpp dsa dsa19 dsa7.c oops14.cpp oops2.cpp preeti dsa1 dsa19.c dsa8 oops15 oops3 preeti.c dsa10 dsa1.c dsa8.c oops15.cp oops3.cpp preeti.txt dsa10.c dsa2 dsa9 oops15.cpp oops4 samrat

LS WITH OPTIONS: ls -l: Lists files in the long format. The files are displayed along with their mode, no. of links, owner of the file, file size, modification date and time and filename. For eg.. [cse07340@localhost cse07340]$ ls -l total 852 -rw-r--r-- 1 cse07340 cse2007 472 Mar 31 12:45 ! -rw-r--r-- 1 cse07340 cse2007 406 Apr 1 11:56 1 -rwxr-xr-x 1 cse07340 cse2007 12127 Oct 16 2008 19 -rw-r--r-- 1 cse07340 cse2007 248 Jun 25 15:35 1.c -rw-r--r-- 1 cse07340 cse2007 172 Jun 25 17:10 2.c -rw-r--r-- 1 cse07340 cse2007 254 Jun 30 15:27 a.c -rwxr-xr-x 1 cse07340 cse2007 11570 Jun 30 16:23 a.out -rw-r--r-- 1 cse07340 cse2007 95 Jun 30 16:23 b.c -rw-r--r-- 1 cse07340 cse2007 51 Aug 13 12:19 cse07340 -rw-r--r-- 1 cse07340 cse2007 0 Aug 13 12:15 cse07340.c -rw-r--r-- 1 cse07340 cse2007 722 Oct 23 2008 dsa

ls -a: Lists all entries, including the hidden files. For eg.. [cse07340@localhost cse07340]$ ls -a ! dsa12.c dsa5.c oops14 oops5 . dsa13 dsa6 oops14.cpp oops5.cpp .. dsa13.c dsa6.c oops15 .oops5.cpp.swo 1.c dsa15 .dsa7.c.swp oops16 oops7 2.c dsa15.c dsa8 oops16.cpp oops7.cpp .2.c.swp dsa16 dsa8.c oops17 oops8 a.c dsa16.c dsa9 oops17.cpp oops8,cpp a.out dsa17.c dsa9.c oops18 oops8.cpp .bash_history dsa19 .emacs oops18.cpp oops9 .bash_logout dsa19.c .gtkrc oops19 oops9.cpp

ls -al: All the hidden files are shown. For eg.. [cse07340@localhost cse07340]$ ls -al total 1040 -rw-r--r-- 1 cse07340 cse2007 472 Mar 31 12:45 !

drwx-----drwxr-xr-x -rwxr-xr-x -rw-------rw-r--r--rw-r--r--rw-r--r--rw-r--r--rw-r--r--rw-r--r--rw-r--r--

3 cse07340 cse2007 4096 Aug 27 12:00 . 906 root root 16384 Aug 10 12:15 .. 1 cse07340 cse2007 11570 Jun 30 16:23 a.out 1 cse07340 cse2007 13009 Aug 20 14:15 .bash_history 1 cse07340 cse2007 24 Aug 27 2008 .bash_logout 1 cse07340 cse2007 191 Aug 27 2008 .bash_profile 1 cse07340 cse2007 124 Aug 27 2008 .bashrc 1 cse07340 cse2007 95 Jun 30 16:23 b.c 1 cse07340 cse2007 51 Aug 13 12:19 cse07340 1 cse07340 cse2007 0 Aug 13 12:15 cse07340.c 1 cse07340 cse2007 722 Oct 23 2008 dsa

CD COMMAND: It is used to change from the working directory to any other directory specified. For eg.. [cse07340@localhost cse07340]$ cd .. [cse07340@localhost home]$ [cse07340@localhost /]$ cd /home/cse07340 [cse07340@localhost cse07340]$

MKDIR COMMAND: This is used to create a new directory. For eg.. [cse07340@localhost cse07340]$ mkdir samrat [cse07340@localhost cse07340]$ ls cse07340.c dsa17.c dsa7 oops14 oops2.c oops9.cpp dsa dsa19 dsa7.c oops14.cpp oops2.cpp preeti dsa1 dsa19.c dsa8 oops15 oops3 preeti.c dsa10 dsa1.c dsa8.c oops15.cp oops3.cpp preeti.txt dsa10.c dsa2 dsa9 oops15.cpp oops4 samrat

RMDIR COMMAND: This is used to remove a directory specified in the command line. It requires the specified directory to be empty before removing it. For eg.. [cse07340@localhost cse07340]$ rmdir samrat [cse07340@localhost cse07340]$ ls

! 1 19 1.c 2.c

dsa12.c dsa3 oops10 oops17.cpp oops5.cpp dsa13 dsa3.c oops10.cpp oops18 oops6 dsa13.c dsa4 oops11 oops18.cpp oops6.cpp dsa14 dsa4.c oops11.c oops19 oops7 dsa14.c dsa4.c44 oops11.cpp oops19.cpp oops7.cpp

MV COMMAND: It is used to rename and move ordinary and directory files. To do this we need both execute and write permissions.The old file no longer exists. For eg.. [cse07354@localhost cse07354]$ mv my.c swati1.c [cse07354@localhost cse07354]$ cat swati1.c //file handling to enter/write a character #include<stdio.h> #include<stdlib.h> main() { char arr[50],ch; FILE *ptr; ptr=fopen("swapping.c","w"); //open the file if(ptr==NULL) { printf("Unable to open the file"); } else { printf("Enter a character,press enter"); while(ch!='\n') { ch=getchar(); putc(ch,ptr); } fclose(ptr); } }

RM COMMAND: It is used to remove one or more files from a directory. Example: For eg.. [cse07340@localhost cse07340]$ rm preeti [cse07340@localhost cse07340]$./ preeti -bash: ./preeti: No such file or directory

CHMOD COMMAND: It is used to change mode of any file or directory. we can grant or revoke permissions of read, write, execute to any file or directory. For eg.. [cse07340@localhost cse07340]$ chmod u+x preeti.c [cse07340@localhost cse07340]$ ls –l -rw-r--r-- 1 cse07340 cse2007 409 Apr 14 12:14 oops8.cpp -rwxr-xr-x 1 cse07340 cse2007 15553 Apr 1 11:57 oops9 -rw-r--r-- 1 cse07340 cse2007 1235 Apr 14 11:59 oops9.cpp -rw-r--r-- 1 cse07340 cse2007 1100 Aug 13 12:28 preeti -rwxr--r-- 1 cse07340 cse2007 204 Aug 27 12:17 preeti.c -rw-r--r-- 1 cse07340 cse2007 63 Jul 7 17:36 preeti.txt -rw-r--r-- 1 cse07340 cse2007 216 Aug 27 12:25 samrat.c -rw-r--r-- 1 cse07340 cse2007 61 Jul 8 14:35 samrat.txt

CP COMMAND: It is used to create duplicate copies of ordinary files. For eg.. [cse07340@localhost cse07340]$ cp preeti.c samrat.c [cse07340@localhost cse07340]$ cat samrat.c 1. name branch 2. samrat cse 3. gunjan cse

COMM COMMAND: In this command the common matter between two files are shown. For eg.. [cse07340@localhost cse07340]$ comm preeti.c samrat.c 1. name branch 2. samrat cse 3. gunjan cse 4. nupur it 5. mayank it samrat is very januable guy. mayank is very adorable guy.

$echo $user COMMAND: In this the id of the user who is working on the computer is displayed

For eg.. [cse07340@localhost cse07340]$ echo $USER cse07340

HEAD COMMAND: The head command is used to display the initial part of a file. By default head command display first 10 lines of a file. Example: For eg.. [cse07327@sbitserver cse07327]$ cat > samrat.c Hello How are u? What is your name? My name is samrat. What is this? This is a book. What are your hobbies? Singing Dancing Surfing net Listening music What is your real name? My real name is Mohit sehgal? This example displays the first 10 lines of the file. [cse07327@sbitserver cse07327]$ head samrat.c Hello How are u? What is your name? My name is samrat. What is this? This is a book. What are your hobbies? Singing Dancing Surfing net

The above eg displays the first two lines of the file. [cse07327@sbitserver cse07327]$ head -2 samrat.c Hello How are u? The above eg displays the first three lines of the file. [cse07327@sbitserver cse07327]$ head -3 samrat.c Hello How are u? What is your name? TAIL COMMAND: The tail command is used to display the last part of a file. By default tail command displays last 10 lines of a file. For eg.. [cse07327@sbitserver cse07327]$ cat > kanika Hello How are u? What is your name? My name is nannu. What is this? This is a book. What are your hobbies? Singing Dancing Surfing net Listening music What is your real name? My real name is Kanika chugh? The above eg displays the last ten lines of the file. [cse07327@sbitserver cse07327]$ tail kanika.c My name is nannu. What is this? This is a book. What are your hobbies? Singing Dancing Surfing net Listening music What is your real name? My real name is Kanika chugh?

The above example displays the lines of file "kanika" beginning with an offset of +2 from the beginning. [cse07327@sbitserver cse07327]$ tail +2 kanika What is your name? My name is nannu. What is this? This is a book. What are your hobbies? Singing Dancing Surfing net Listening music What is your real name? My real name is Kanika chugh? The above example displays the lines of file "items" with an offset of -2 from the end of the file. cse07327@sbitserver cse07327]$ tail -2 kanika What is your real name? My real name is Kanika chugh?

SORT COMMAND The sort filter arranges the input from the Standard Input in the alphabetical order. The options available with sort are -r, -f, -n, -b, -t etc. For eg.. [cse07327@sbitserver cse07327]$ cat > preeti yellow orange green skyblue blue red [cse07327@sbitserver cse07327]$ sort preeti blue green orange red skyblue yellow

Sort -r It display the Input in reverse alphabetical order. [cse07327@sbitserver cse07327]$ sort -r preeti yellow skyblue red orange green blue [cse07327@sbitserver cse07327]$ cat >numbers 5 8 9 12 58 65 25 35 33

Sort -n It will arrange the input according to numerical value and then display it. [cse07327@sbitserver cse07327]$ sort -n numbers 5 8 9 12 25 33 35 58 65

Sort -f When the sort command is used with -f option, the case distinction is ignored and the output is arranged in alphabetical order. [cse07340@localhost cse07340]$ sort -f preeti.c 1 12 14 15 2 3 4 6 6 7 9 Arjun Gunjan mayank mohit nupur preeti samrat

Sort –u This option will remove duplicate lines from the input and display the output. [cse07354@localhost cse07354]$ sort -u swati1.c cse07236 pts/10 Aug 20 12:14 (192.168.1.79) cse07334 pts/14 Aug 20 12:20 (192.168.1.53) cse07334 pts/3 Aug 20 11:35 (192.168.1.53) cse07339 pts/8 Aug 20 12:32 (192.168.1.76) cse07340 pts/11 Aug 20 12:14 (192.168.1.66) cse07340 pts/17 Aug 20 12:29 (192.168.1.66) cse07341 pts/18 Aug 20 12:27 (192.168.1.79) cse07343 pts/24 Aug 20 12:47 (192.168.1.60) cse07344 pts/15 Aug 20 12:21 (192.168.1.78) cse07344 pts/6 Aug 20 12:03 (192.168.1.78)

Sort –o Output. Using this sort option we will be able to redirect the contents to the file mentioned. [cse07354@localhost cse07354]$ sort -o amit.c swati1.c [cse07354@localhost cse07354]$ vi amit.c cse07236 pts/10 Aug 20 12:14 (192.168.1.79) cse07334 pts/14 Aug 20 12:20 (192.168.1.53) cse07334 pts/3 Aug 20 11:35 (192.168.1.53) cse07339 pts/8 Aug 20 12:32 (192.168.1.76) cse07340 pts/11 Aug 20 12:14 (192.168.1.66) cse07340 pts/17 Aug 20 12:29 (192.168.1.66) cse07341 pts/18 Aug 20 12:27 (192.168.1.79) cse07343 pts/24 Aug 20 12:47 (192.168.1.60) cse07344 pts/15 Aug 20 12:21 (192.168.1.78) cse07344 pts/6 Aug 20 12:03 (192.168.1.78) cse07349 pts/12 Aug 20 12:15 (192.168.1.73) cse07349 pts/19 Aug 20 12:23 (192.168.1.73)

GREP COMMAND GREP stands for "Global search for Regular ExPression". It is used to search for a particular pattern from a file or from the Standard Input and display those lines on the Standard output. For eg.. [cse07327@sbitserver cse07327]$ grep "this" kanika What is this? This is a book. To extract the lines which end with the character "e": [cse07327@sbitserver cse07327]$ grep "e$" preeti Blue Orange Skyblue To extract the lines which starts with the character "b": [cse07327@sbitserver cse07327]$ grep "^b" preeti blue

Grep -v To skip the lines which ends with the character "e": [cse07327@sbitserver cse07327]$ grep -v "e$" preeti Yellow Red Green Grep -c Displays only the count of the lines that match the specified pattern. [cse07327@sbitserver cse07327]$ grep -c "e$" preeti 3 Grep -n Displays those lines that match the specified pattern along with the line number at the beginning of the line. [cse07327@sbitserver cse07327]$ grep -n "e$" preeti 2: orange 4: skyblue 5: blue

FGREP COMMAND: FGREP stands for"Fixed GREP". This command is used to extract only fixed strings without the use of any regular expression.fgrep used with -x option to axtract those lines which match the string exactly. For eg.. [cse07354@localhost cse07354]$ fgrep "cse07354" swati1.c cse07354 pts/16 Aug 20 12:22 (192.168.1.75) cse07354 pts/29 Aug 20 12:54 (192.168.1.74) [cse07340@localhost cse07340]$ fgrep "samrat is" preeti.c samrat is very januable guy. [cse07340@localhost cse07340]$ fgrep "is very" preeti.c samrat is very januable guy. mayank is very adorable guy.

PIPES:A pipe is a mechanism in which the output from noe command can be redirected as input to another command. For eg.. In the above example, the output of command '"who" is passed as the input to the "wc " command and the result is displayed. [cse07354@localhost cse07354]$ who|wc 32 192 1600 •

The output of the above command will be the display of the first three lines of the file named items.

[cse07340@localhost cse07340]$ cat f1|head preeti.c 1. name branch 2. samrat cse 3. gunjan cse 4. nupur it 5. mayank it samrat 2 4 6 7

CUT COMMAND: One particular field from any file or from output of any command canbe extracted and displayed using the cut command. A single character can also be extracted by using the -c option of the cut command. For eg.. In the above command "-d" represents the "delimiter" or the field separator. The option "-f" is followed by the list of fields that have to be displayed. [cse07340@localhost cse07340]$ cut -d "." -f2 preeti.c name branch samrat cse gunjan cse nupur it mayank it samrat 2 4

6 7 Gunjan 12 14 nupur 15 Arjun 9 mohit 6 mayank 3

PASTE COMMAND: It merges the contents of two files into a single file. It reads a line from each file list specified and combines them into a single file. For eg.. [cse07340@localhost cse07340]$ paste preeti.c samrat.c 1. name branch 2. samrat cse 3. gunjan cse 4. nupur it 5. mayank it samrat is very januable guy. 2 4 6 7 Gunjan 12 14 nupur 15 Arjun 9 mohit 6 mayank is very adorable guy. 3

Related Documents

Linux Basic Commands
June 2020 16
Linux Basic Commands
June 2020 11
Linux Basic Commands
June 2020 10
Basic Linux Commands
June 2020 18
Linux Basic Commands
June 2020 23
Basic Linux Commands
May 2020 19