0% found this document useful (0 votes)
71 views

Linux Commands: Single Unit by Its Name. A Directory Is A File That Acts As A Folder For Other Files

The document provides information about various Linux commands and their usage. It discusses commands like pwd, mkdir, ls, cd, cat, head, tail, cp, mv, rmdir, rm, cal, date, who, man, passwd, cmp, sort, write, grep, ping, and vi. The commands allow users to navigate directories, create and view files, copy or move files, remove files and directories, view system information, compare files, sort data, send messages, search files, check network connectivity, use wildcards, and edit files.

Uploaded by

Pradip Bastola
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

Linux Commands: Single Unit by Its Name. A Directory Is A File That Acts As A Folder For Other Files

The document provides information about various Linux commands and their usage. It discusses commands like pwd, mkdir, ls, cd, cat, head, tail, cp, mv, rmdir, rm, cal, date, who, man, passwd, cmp, sort, write, grep, ping, and vi. The commands allow users to navigate directories, create and view files, copy or move files, remove files and directories, view system information, compare files, sort data, send messages, search files, check network connectivity, use wildcards, and edit files.

Uploaded by

Pradip Bastola
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

LINUX COMMANDS

1.pwd
Command Name: pwd – Print the name of the current working directory.
Description: Print the name of the current working directory.
Syntax: $ pwd
Note: A file is a collection of data that is stored on disk and that can be manipulated as a
single unit by its name. A directory is a file that acts as a folder for other files.

2.mkdir, ls
Command Name: mkdir – make directories.
Description: Create directories, if they do not already exist.
Syntax: $ mkdir dir1 (dir1 denotes directory name).
Command Name: ls – list directory contents.
Description: List information about the contents of the directory.
Synatx: $ ls
Eg: username@User-PC:~$ mkdir textfile1 (It creates the directory named textfile1

at home directory.)
username@User-PC:~$ mkdir textfile2 (It creates the directory named textfile2

at home directory.)
username@User-PC:~$ ls ( It lists the directories present at home directory.)
textfile1 textfile2
$ls -a
It lists all files and folders including hidden files and folders.

3.cd
Command Name: cd – change directories
Description: It changes current directory to another directory. In other word, we use ‘cd’
command to go to a certain directory.
Syntax: $ cd dir1 (dir1 denotes directory name). (changes current directory to dir1
directory).
$cd (Changes to home/root directory)
Eg: username@User-PC:~$ cd textfile1
username@User-PC:~/textfile1$
If we simply type cd, i.e, username@User-PC:~/textfile1$ cd
we get username@User-PC: ~$ i.e., we return to the default home directory.

4.cat (short for ‘concatenate’)


Command Name & Description: cat – It is one of the most frequently used command in
Linux operating system. cat command allows us to create single or multiple files, view
contents of file.
Syntax: $ cat > file1 (It makes file named file1).
$ cat file1 (displays contents of file1)
$ cat -n file1 (display all lines in file1 including number of lines in file1).

Eg: username@User-PC:~$ cd textfile1 - It takes us to textfile1 directory.


username@User-PC:~/textfile1$ cat > doc1 - It makes doc1 file in textfile1 directory.
I am learning Linux. And i am doing well. – Here we can write the contents for file doc1.
(go to new line & press ctrl+D to come out)
username@User-PC:~/textfile1$ ls - It lists the files inside directory textfile1.
doc1 - doc1 is shown since we have created it above using cat command.
username@User-PC:~/textfile1$ cat doc1 – using just cat doc1, we can see its contents.
I am learning Linux. And i am doing well.
username@User-PC:~/textfile1$ cat -n doc1
1 I am learning Linux. And I am doing well. - (It shows the number of lines in the file ).

5.head, tail
Command Name and Usage: head – Print the first 10 lines of the file.
tail – Print the last 10 lines of the file.
Syntax: $ head filename
$ tail fillename

Eg: Let’s create a text file with more than 15 lines in it and try to display its 1 st 10 and last
10 lines using head and tail command respectively.
username@User-PC:~/textfile1$ cat > doc2
Linux OS.
1
2
3
4
5
6
7
8
9
10
11
12
(Press cntrl+D to come out )
username@User-PC:~/textfile1$ cat doc2 (It shows all of its contents.
(See its output in Linux.)
username@User-PC:~/textfile1$ head doc2 (It shows first 10 lines.)
Linux OS.
1
2
3
4
5
6
7
8
9
username@User-PC:~/textfile1$ tail doc2 (It shows last 10 lines.)
(Check Output in Linux).

6.cp
Command Name & Usage: cp – copy files and directories. It is used to copy files or group
of files or directory from source to destination.
Syntax: $ cp sourcefile destinationfile
Eg: username@User-PC:~/textfile1$ ls
doc1 doc2
username@User-PC:~/textfile1$ cp doc1 doc2 (All contents of doc1 is copied to doc2.
Previous contents of doc2 is removed).
username@User-PC:~/textfile1$ cat doc2
I am learning Linux. And I am doing well.
username@User-PC:~/textfile1$ cp doc1 doc3 (There is no file named doc3 however ‘cp’
command creates a new file ‘doc3’ and
copy contents of doc1 to doc3).
username@User-PC:~/textfile1$ ls
doc1 doc2 doc3
username@User-PC:~/textfile1$ cat doc3
(Check output in Linux).

7. mv
Command Name & Usage: mv – move (rename) files. It renames or move source files or
directory to destination file or directory.
Syntax: $ mv sourcefile destinationfile
Eg: username@User-PC:~/textfile1$ ls
doc1 doc2 doc3
username@User-PC:~/textfile1$ mv doc1 doc3 (It moves/renames doc1 file to doc3).
username@User-PC:~/textfile1$ ls
doc2 doc3
username@User-PC:~/textfile1$ cat doc3
(Output Check in Linux).

8. rmdir, rm
Command Name & Usage: rmdir – It removes directory, if they are empty.
rm- It removes files.
Syntax: $ rmdir directory
$ rm file
Eg: username@User-PC:~/textfile1$ ls
doc2 doc3
username@User-PC:~/textfile1$ rm doc2
username@User-PC:~/textfile1$ ls
doc3 (doc2 has been removed by ‘rm’ command)
username@User-PC:~/textfile1$ rmdir doc3
rmdir: failed to remove 'doc3': Not a directory (doc3 is a file.)
username@User-PC:~/textfile1$ cd
username@User-PC:~$ ls
textfile1 textfile2
username@User-PC:~$ ls textfile1 (Shows files in textfile1 directory).
doc3
username@User-PC:~$ rmdir textfile1
rmdir: failed to remove 'textfile1': Directory not empty (So we have to first delete doc3).
For deleting doc3 we have to move into textfile1 directory.
username@User-PC:~$ cd textfile1
username@User-PC:~/textfile1$ rm doc3
username@User-PC:~/textfile1$ ls (it shows nothing because all files are deleted.)
(Now we can delete textfile1 directory since it is empty).
username@User-PC:~/textfile1$ cd
username@User-PC:~$ rmdir textfie1
username@User-PC:~$ ls
textfile2

9.cal, date:
Command Name and Usage: cal – calender, it displays the calender.
date- date, it displays date.
Eg: username@User-PC:~$ cal (Check Output).
username@User-PC:~$ cal -y (Check Output).
username@User-PC:~$ cal -j (Check Output).
username@User-PC:~$ cal -jy (Check Output).
username@User-PC:~$ date (Check Output).

10. who:
The ‘who’ command is used to get information about currently logged in user on to
system.
Syntax: $who
11. man:
The ‘man’ command in Linux is used to display the user manual of any command that we
can run on the terminal. It provides a detailed view of the command.
Syntax: $ man [command name]
E.g.: $ man who
= It displays complete manual of who command.
12. passwd:
The ‘passwd’ command in Linux is used to change the user account passwords.
E.g.: $ passwd
It then displays;
Changing password for user.
Current UNIX password:
Enter new UNIX password:
Retype new UNIX password:
Password updated successfully.

13. cmp:
The ‘cmp; command in Linux/UNIX is used to compare the two files byte by byte and helps
you to find out whether the two files are identical or not.
Syntax: $ cmp file1 file2
(file1 and file2 refer to the filenames.)
E.g.:
Suppose we got two text files named as file1 and file2.
Contents of file1; I live in Pokhara.
Contents of file2; I live in Kathmandu.
Now, on comparing using ‘cmp’ command,
$ cmp file1 file2
File1 file2 differ: byte11, line1

14. sort:
SORT command is used to sort a file, arranging the records in a particular order. By
default, the sort command sorts file assuming the contents are ASCII. Using options in
sort command, it can also be used to sort numerically.
Syntax: $ sort filename (It sorts the file.)
$ sort -r filename (It sorts the file in reverse order.)
$ sort -n filename (It sort a file numerically.)

15. write:
The basic use case of the write command is to send messages to the users on the other
terminal as a way to interact.
Syntax: $ write username
Using this I could type and send message to another user.
16. grep:
The grep command searches a file for a particular pattern of characters, and displays all
lines that contain that pattern.
Syntax: $ grep [options] pattern [filename]
Eg:
Consider the below file as an input.
$cat > geekfile.txt
Unix is great os.
unix is easy to learn.
$ grep ‘unix’ geekfile.txt (Displays the line containing unix with ‘unix’ highlighted).
$grep -i ‘unix’ geekfile.txt (case insensitive search).
(Note: Here -i is option, ‘unix’ is pattern and ‘geekfile.txt’ is filename)
$grep -c ‘unix’ geekfile.txt (display the count of number of matches).

17. ping:
PING command is used to check the network connectivity between host and server/host.
This command takes as input the IP address or the URL.
E.g.: $ ping 192.168.1.79 (Input as IP address)
$ ping www.google.com (Input as URL (web address)).
18. wildcards:
A wildcard in Linux is a symbol or set of symbols that stands in for other characters. It can
be used to substitute any other character or characters in a string.
Three types of wildcards are common in Linux:
a. * It matches zero or more characters.
b. ? It matches exactly one character.
c. Bracket values It match characters enclosed in square brackets.

e.g.,
we have files named: oac, obd, ocd, oded.
$ ls o*d
It gives oac, obd, ocd, oded.
(It lists all files that begin with o, end with d, no matter the number of characters in
between).
$ ls o?d
It gives oac, obd, ocd.
(It lists all files that begin with o, end with d and have only one character in between).
$ ls o[ac]d
It gives only oad and ocd.
(It matches characters enclosed in square bracket).
$ ls o[a-c]d
It gives oad, obd, ocd.
(It matches range of characters enclosed in square bracket).

19. vi:
The default editor that comes with the UNIX operating system is called vi (visual editor).
Using vi editor, we can edit an existing file or create a new file from scratch. we can also
use this editor to just read a text file.
Syntax: $ vi filename
Working with vi editor.
$ vi filename
We now enter into the vi-editor command mode. Content of the file is displayed.
Now, to enter text, we must be in insert mode. To come in insert mode, we simply type i.
Now we can edit the contents of the file.
To get out of insert mode, press the Esc key, which will put you back into command mode.
To save the file and come back to Linux terminal press shift+zz.
20. ifconfig:
‘ifconfig’ stands for "interface configuration." It is used to view and change the
configuration of the network interfaces on your system.
Syntax:
$ ifconfig
It displays information about all network interfaces currently in operation.

(Model Question- Sample Answer)


What command do you write on the linux terminal to do the following?
a.change to the home directory?
ANS. We write ‘cd’ command on the linux terminal to change to home directory.
Synatax: $ cd
b. create new file document.txt?
ANS. We use cat command in Linux terminal to create new file.
Syntax: cat > document.txt
c.list all files and folders including hidden files?
ANS. We write ls command I linux terminal to list all files and folders including hidden
files.
Synatx: $ ls -a

You might also like