0% found this document useful (0 votes)
42 views7 pages

Linux Commands

Linux command

Uploaded by

mohammadkaif3108
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views7 pages

Linux Commands

Linux command

Uploaded by

mohammadkaif3108
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Linux Commands

Create a new file using terminal: vi editor

$ vi fork.c //creates a new file fork.c, press i for insert, a for append to start typing

To save this file:


Press : then wq (save file and quit)
Press : then q! (Do not save file and quit)

Information Maintenance: wc, clear, cal, who, date, pwd

wc: word count

$ wc file1 //print lines, number of words and number of characters

clear: clears the screen

$ clear //will clear the screen of previous commands on command prompt

cal: prints the calendar

$ cal. //print the current month calendar

$ cal 2020 // print the year 2020 calendar

$ cal 6 2020 //print the calendar for June 2020

who: prints list of all users logged into the computer

$ who //lists all users

$ who am I //lists current user

date: print current date Kme year

$ date

Wed Dec 6 [Link] IST 2023

pwd: print the working directory (current directory)

$ pwd

File Management: cat, cp, rm, mv, cmp, comm, diff, find, grep, awk
cat: outputs the content of the file. concatenate and print files

$ cat fork.c //prints the content of the file fork.c on the command prompt

$ cat file1 >> file2 //appends content of file1 to file2

$ cat file1 > file2 //copy content of file1 to file2

$ cat -n file //print contents of file preceded by line numbers

$ who > file //copy the output of who command (or any other command) to file

cp: This command copies a file, preserving the original and creaKng an idenKcal copy.

$ cp file1 file2

rm: removes/deletes file

$ rm file1

mv: move a file, change the directory locaKon of a file, rename files.

$ mv file1 file2

cmp: compares the two files and states the first difference in both files

For example

$ cat f1

hello
world
text

$ cat f2

hellO
W0rld
text

$ cmp f1 f2

f1 f2 differ: char 5, line 1

comm: select or reject lines common to two files

For example
$ cat f1

hello
world
text

$ cat f2

hellO
W0rld
text

$ comm f1 f2

diff: diff stands for difference, used to display the differences in the files by comparing the
files line by line. gives which lines in one file have is to be changed to make the two files
same

For example

$ cat f1

hello
world
text

$ cat f2

hellO
W0rld
text

$ diff f1 f2

1,2c1,2 //line and column where its different


< hello
< world
---
> hellO
> W0rld
4d3
< //no output means idenKcal

find: The find command lists all of the files within a directory and its subdirectories that
match a set of condiKons.

$ find . -name "f*" //find in current directory (.) name that starts with f

grep: stands for global regular expression print, matches regular expression and prints
matching output

$ cat f1

hello
world
text
work
tent
tell

$ grep "ell" f1
hello
tell

$ grep "el*" f1
hello
text
tent
tell

$ grep "ell*" f1
hello
tell

awk: paZern-directed scanning and processing language, created by Aho, Weinberger &
Kernighan (hence the name)

Directory Management: cd, mkdir, rmdir, ls

cd: change the current working directoy

$ cd //change to home directory

$ cd .. //change to parent directory of the current directory


$ cd / //change to root directory

$ cd <path of the directory A> //change to directory A (as given in the path)

$ cd newDir //change to sub directory newDir in the current directory

mkdir: create a new directory

$ mkdir newDir //create a new directory in the current directory

rmdir: delete a directory

$ rmdir newDir //delete a directory named newDir

ls: lisKng the contents of a directory

$ ls //list the names of files/directory in the current directory

$ ls -l //long list names of files/directory in the current directory

$ ls
f1 newnewDir

$ ls -l
total 8
-rw-r--r-- 1 username stuff 18 Dec 6 19:21 f1
drwxr-xr-x 2 username stuff 64 Dec 6 18:44 newnewDir

Process Control: fork, getpid, ps, kill, sleep

fork: DONE IN CLASS

getpid: returns the pid of a process

#include <unistd.h>

pid_t getpid(void); //returns the process id of process

pid_t getppid(void); //returns the process id of parent process

ps: process status, informaKon about currently running processes

$ ps

kill: destroy/kill any processes or programs that are suspended and are unable to restart.
sleep: suspend execuKon for an interval of Kme

while (1)
if (! <condiKon>) then
sleep 300

Communication: Input-output redirection, Pipe

Input-output redirecKon

> redirect output to file overriding any previous content

$ cat file1 > file2 //copy content of file1 to file2

$ who > file //copy the output of who command (or any other command) to file

>> redirect output to file appending to any previous content

$ cat file1 >> file2 //appends content of file1 to file2

< redirect output from file

$ cat file1 < file2 //copy content of file2 to file1


Pipe (|): pipes or pushes the output of one command as input to another

$ date | cal //output of date will become input for cal

Protection Management: chmod, chown, chgrp

chmod: change mode or access permissions of files and directory

Access permissions for every file/directory

-rwxr--r-- break up to understand - rwx r-- r--

For first posiKon:


- Regular file.
b Block special file.
c Character special file.
d Directory.

Yellow: permissions for owner/user [u] (comprises of r (read) w (write) x (execute)


Green: permissions for group the user belongs to [g]
Yellow: permissions for other users/groups [o]

In octal read is 4, write is 2, execute is 1


$ chmod 777 file1 //give r w x to all groups/users

$chmod 736 file1 //give rwx to user, -wx to group, rw- to others

$ chmod +r file1 //give read permission to all users/groups

$ chmod -w file1 //delete write permission from all users/groups

$ chmod o+wx file1 //give write and execute permission to others [o]

chown: change file owner

$ chown usr file1 //change the owner of file1 to usr

chgrp: change group of the file

$ chown usr file1 //change the group of file1 to usr

You might also like