In this tutorial, I’ll be demonstrating the very basic Linux commands with examples that are sometimes used to familiarize you with the command line for Linux. Being a first move expert in Linux for a beginner will be beginning to learn the simple commands.
The command follows with options (of course optional) and a list of arguments. The options will alter command actions. The arguments can include files or folders, or any other data that the command acts on. Maybe not every order requires arguments.Some commands work with or without them (e.g. ‘ls’ command). The options can be provided in two ways: full word options with — (e.g. –help), or single letter options with – (e.g. -a -b -c or multiple options, -abc).
Linux Basic Commands
Let’s start with some simple commands.
1) pwd command
‘pwd’ command prints the absolute path to current working directory.
2) cal command
Displays the calendar of the current month.
‘cal ’ will display calendar for the specified month and year.
3) echo command
This command will echo whatever you provide it.
The ‘echo’ command is used to display the values of a variable. One such variable is ‘HOME’. To check the value of a variable precede the variable with a $ sign.
4) date command
Displays current time and date.
If you are interested only in time, you can use ‘date +%T’ (in hh:mm:ss):
5) tty command
Displays current terminal.
6) whoami command
This command reveals the user who is currently logged in.
7) id command
This command prints user and groups (UID and GID) of the current user.
By default, information about the current user is displayed. If another username is provided as an argument, information about that user will be printed:
8) clear command
This command clears the screen.
Nobody can remember all the commands. We can use help option from command like
9) help option
With almost every command, ‘–help’ option shows usage summary for that command.
10) whatis command
This command gives a one line description about the command. It can be used as a quick reference for any command.
11) Manual Pages
‘–help’ option and ‘whatis’ command do not provide thorough information about the command. For more detailed information, Linux provides man pages and info pages. To see a command’s manual page, man command is used.
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.
With -k option, a search through man pages can be performed. This searches for a pattern in the name and short description of a man page.
12) Info pages
Info documents are sometimes more elaborate than the man pages. But for some commands, info pages are just the same as 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.
Linux Filesystem commands
13) Changing Directories Command
Change the current working directory to the directory provided as argument. If no argument is given to ‘cd’, it changes the directory to the user’s home directory. The directory path can be an absolute path or relative to current directory. The absolute path always starts with /. The current directory can be checked with ‘pwd’ command (remember?):
In the first ‘cd’ command, absolute path (/usr/share) is used, and with second command, relative path (doc) is used.
14) Listing File And Directories Command
List files and/or directories. If no argument is given, the contents of current directory are shown.
If a directory is given as an argument, files and directories in that directory are shown.
‘ls -l’ displays a long listing of the files.
In this long listing, the first character is ‘d’ or ‘-‘. It distinguishes between file types. The entries with a ‘-‘ (dash) are regular files, and ones with ‘d’ are directories. The next 9 characters are permissions (‘rwxr-xr-x’ in first listing). The number following the permissions is the link count. Link count follows user and group owner. In the above example, the file owner is ‘raghu’ and group owner is ‘raghu’ as well. Next is the size of the file. And then time stamp before the name of file (or directory).
By default, hidden files or directories are not shown, to see hidden files as well, -a option is used. Hidden files in Linux start with a period sign (.). Any file that starts with a period is hidden. So, to hide a file, you just need to rename it (and put a period before it).
If you want to see the properties of a directory instead of the files contained in it, use -d (with -l) option:
Creating files and directories
15) mkdir command
To create a directory, the ‘mkdir’ command is used.
16) touch command
For creating an empty file, use the touch command.
If a file already exists, touch will update its time stamp. There are a lot of other methods to create a new file, e.g. using a text editor like vi or gedit, or using redirection. Here is an example of creating a file using redirection:
A file named usrlisting is created in this example.
Copy, move and remove commands
17) copy command
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.
If the destination is a directory, then the file is copied with its original name in that directory.
Multiple files can also be copied, but in that case, the last argument will be expected to be a directory where all the files are to be copied. And the rest of the arguments will be treated as file names.
If a directory is to be copied, then it must be copied recursively with the files contained in it. To copy a directory recursively, use -r option with ‘cp’ command:
18) move command
Move files or directories. The ‘mv’ command works like ‘cp’ command, except that the original file is removed. But, the mv command can be used to rename the files (or directories).
Here, ‘listing_copy.txt’ is moved with the name ‘usrcopy’ in the same directory (or you can say that it has been renamed).
19) To remove or Delete
‘rmdir’ command removes any empty directories, but cannot delete a directory if a file is present in it. To use ‘rmdir’ command, you must first remove all the files present in the directory you wish to remove (and possibly directories if any).
To remove files and directories
A directory must be removed recursively with -r option.
Here, the file named ‘file2’ is removed first, and then the directory ‘example’ is removed recursively. This can be seen in the output of ‘ls -l’ command where these two are no longer present.
Other file commands
20) file command
The file command determines the file type of a given file. For example:
You can provide one or more than one file as an argument to the file command.
21) stat command
To check the status of a file. This provides more detailed information about a file than ‘ls -l’ output.
22) cat command
The ‘cat’ command is actually a concatenator but can be used to view the contents of a file.
The cat command lists file as a whole. But if the file is big enough to fit into one screen, then we will be able to see only the last page of the file. The commands ‘less’ and ‘more’ display files one page at a time. So they are also called pagers. You can navigate through a file using arrow keys. To quit from a pager, hit ‘q’.
24) head command
Displays the first few lines of a file. By default, the ‘head’ command displays the first 10 lines of a file. But with -n option, the number of lines to be viewed can be specified.
25) tail command
Similar to ‘head’; the ‘tail’ command shows the last 10 lines by default, and -n option is available as well.
26) wc command
This command counts lines, words and letters of the input given to it.
The /etc/passwd file has 35 lines, 57 words, and 1698 letters present in it.
27) grep command
The ‘grep’ command searches for a pattern in a file (or standard input). It supports regular expressions. It returns a line if it matches the pattern in that line. So, if we wish to find the lines containing the word ‘nologin’, we use ‘grep’ as follows:
28) ln command
The ln command is used in linux to create links. Links are a kind of shortcuts to other files. The general form of command is:
There are two types of links, soft links and hard links. By default, hard links are created. If you want to create soft link, use -s option. In this example, both types of links are created for the file usrlisting.
29) Pico & Nano
‘Pico’ is a text editor in Linux. ‘Nano’ editor is inspired from ‘pico’. They work almost the same. If the argument given as filename exists, then that file will be opened for editing in pico/nano. Otherwise, a new file with that name will be created. Let’s create a new file named 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.
30) VI editor
The VI stands for Visual editor; another text editor in Linux. This is a standard editor in many Linux/Unix environments. This is the default editor that comes with many Linux distributions. It might be possible that it is the only text editor available with your distro.
You can open a file with vi for editing using the following:
The vi editor has 3 modes in which it performs its functions. The default is COMMAND mode, in which tasks like copy, paste, undo etc can be performed. You can change a mode from command mode only (and come back to it). The second mode is the INSERT mode, in which whatever key you type is treated as a character and will be loaded into the file buffer. To enter this mode, press ‘i’ when in command mode.
The final mode is EX mode or last line mode. The changes made in the buffer can be saved or discarded in this mode.
31) alias command
The ‘alias’ is another name for a command. If no argument is given, it shows current aliases. Aliases can be used for short names of commands. For example, you might use the clear command frequently. You can create an alias for it:
Next time you enter ‘c ‘ on command line, your screen will get clear. Current aliases can be checked with ‘alias’ command:
32) w command
w command is used to check which users are logged in to the system, and what command they are executing at that particular time:
It also shows the uptime, number of users logged in and load average of the system (in the first line of output above).
33) last command
Displays information about the users who logged in and out of the system. The output of the last command can be very large, so the following output has been filtered (through head) to display the top 10 lines only:
A similar command is ‘lastb’ that shows the last unsuccessful login attempts. But this command must be run as root otherwise you would get an error saying permission denied.
34) du command
The du command determines disk usage of a file. If the argument given to it is a directory, then it will list disk usage of all the files and directories recursively under that directory:
35) df command
The df reports file system usage. For example:
36) fdisk command
The fdisk is a tool for getting partition information, and for adding and removing partitions. The fdisk tool requires super user privileges. To list all the partitions of all the hard drives available:
The fdisk is an interactive tool to edit the partition table. It takes a device (hard disk) as an argument, whose partition table needs to be edited.
Pressing ‘m’ at the fdisk prompt prints out the help shown above that lists all the commands available for fdisk. A new partition can be created with ‘n’ and an existing partition can be deleted with the ‘d’ command. When you are done editing the partitions, press ‘w’ to write the changes to the disk, and finally, hit ‘q’ to quit from fdisk (q does not save changes).
37) netstat command
The ‘netstat’ is a command used to check the network statistics of the system. It will list the current network connections, routing table information, interface statistics, masquerade connections and a lot more information.
38) Shutdown Command
In Linux, you can use shutdown command to gracefully halt your system. Most commonly used command is shutdown -h now.