Sunil Sapkota

Basic Linux Command

Linux Commands

Lets have the basic Linux commands with syntax and  examples,  which are frequently used to get you more familiar with the Linux command line. To be an expert in Linux first step for a beginner would be to start learning the basic linux commands.In linux system the command is followed by options (which are optional ) and a list of arguments. We use options to modify the  behavior of a command and  arguments any parameters ( may be files or directories or some other data on which the command acts).Some commands work  without arguments or options also (e.g. ‘ls’ command). The common way of providing options are of  three ways>full word options with — (e.g. –help)> single letter options with – (e.g. -a -b -c> multiple options with – (e.g.  -abc).

Linux commands Syntax

The Linux commands  have the following syntax:
$command options arguments

Let’s start with some simple commands.

1. pwd  and ls command

/‘pwd’ command prints the absolute path to current working directory.

and ls command is used to list all files inside current directory.

$ pwd

$ cd /usr/share/
$ pwd
$ cd doc
$ pwd

The above command does the following thing:

checks the current directory.
change the directory to /user/share/
check the current directory.
change the directory to doc/
check the current directory.

2. cal command

Displays the calendar of the current month.
$ cal
jan 2021
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
‘cal ’ is also used to  display calendar for the specified month and year.  Here the month and year are arguments.and the command is :
$ cal 3 2021
This command is used to display the calendar of 2021 march.

3. echo command

This command will prints or displays whatever you provide it.
$ echo “”
To save something in a file you can use this command like:
$ echo “this is the content of the file” >> file.txt

4. id command

This command prints user and groups (UID and GID) of the current user.
$ id uid=1000(sunil) gid=1000(sunil) groups=1000(sunil),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev)

5. date command

Displays current time and date.
$ date Fri Jul 6 01:07:09 IST 2012

6. clear command

This command clears the screen.
$ clear

7. Listing File And Directories Command

$ ls [files-or-directories-names]
List all the  files and/or directories. If no argument is given, the contents of current directory will be shown.
$ ls example file1.txt file2.txt file3.txt

8. mkdir command

The ‘mkdir’ command is used to create a directory.
$ mkdir dirName

9. touch command

For creating an empty file, use the “touch” command.
$ touch file1

10. copy, move and   remove command

Copy syntax:
$cp source destination
Copy files and directories. If the source is a file, and the destination (file) name does not exit, then source is copied with new name i.e. with the name provided as the destination.
$ cp usrlisting listing_copy.txt
To remove files and directories
$ rm files|directories
A directory must be removed recursively with -r option.
$ rm  -r folder1
you can also use “rmdir” command to remove an empty directory.

11. cat command

The ‘cat’ command is actually a concatenator but can be used to view the contents of a file.
$ cat /etc/passwd

12. grep command

The ‘grep’ command searches for a pattern in a file (or standard input). It supports regular expressions(regex). It returns a line if it matches the pattern in that line. So, if we wish to find the lines containing the word ‘pass’, we use ‘grep’ as follows:
$ grep pass /etc/passwd

13. Nano

‘nano’ is a command line based text editor in Linux.If the argument given as filename exists, then that file will be opened for editing in nano. Otherwise, a new file with that name will be created. Let’s create a new file named hello.txt:
$ nano hello.txt
Having made all the changes to the file, press ‘ctrl+o’ to write the changes to the file and ‘ctrl+x’ to exit from the editor. There are a lot of functions available with this editor. The help menu can be accessed with ‘ctrl+g’ keystrokes.

14. Shutdown Command

In Linux, you can use shutdown command to gracefully halt your system. Most commonly used command is
$ shutdown -h now

15. Get help for any command in case your don't know how to use or forget.

i) help option

With almost every command, ‘–help’ or “-h” option shows usage summary for that command.

$ date –help

Usage: date [OPTION]… [+FORMAT] or: date [-u|–utc|–universal] [MMDDhhmm[[CC]YY][.ss]] Display the current time in the given FORMAT, or set the system date.

 ii) whatis command

This command gives a one line description about the command which used as a quick reference for any command.

$ whatis date

date (1) – print or set the system date and time

$ whatis whatis

whatis (1) – display manual page descriptions

iii) Manual Pages

For more detailed information, Linux provides man pages and info pages. To see a command’s manual page, man command is used.

$ man date

The man pages are properly documented pages. They have following sections:

NAME: The name and one line description of the command.

SYNOPSIS: The command syntax.

DESCRIPTION: Detailed description about what a command does.

OPTIONS: A list and description of all of the command’s options.

EXAMPLES: Examples of command usage.

FILES: Any file associated with the command.

AUTHOR: Author of the man page

REPORTING BUGS: Link of website or mail-id where you can report any bug.

SEE ALSO: Any commands related to the command, for further reference.

iv) Info pages

Info documents are sometimes more elaborate than the man pages.These are like web pages. Internal links are present within the info pages. These links are called nodes. Info pages can be navigated from one page to another through these nodes.

$ info date

Leave a Comment