Basic Shell Commands in Linux - mantoskez/mantas-wiki GitHub Wiki
A shell is a special user program that provides an interface to the user to use operating system services. Shell accepts human-readable commands from the user and converts them into something which the kernel can understand. It is a command language interpreter that executes commands read from input devices such as keyboards or from files. The shell gets started when the user logs in or starts the terminal.
Shell is broadly classified into two categories –
- Command Line Shell
- Graphical shell
Command Line Shell Shell can be accessed by user using a command line interface. A special program called Terminal in linux/macOS or Command Prompt in Windows OS is provided to type in the human readable commands such as “cat”, “ls” etc. and then it is being execute. The result is then displayed on the terminal to the user. A terminal in Ubuntu 16.4 system. Graphical Shells Graphical shells provide means for manipulating programs based on graphical user interface (GUI), by allowing for operations such as opening, closing, moving and resizing windows, as well as switching focus between windows. Window OS or Ubuntu OS can be considered as good example which provide GUI to user for interacting with program. User do not need to type in command for every action.
There are several shells are available for Linux systems like –
- BASH (Bourne Again Shell) – It is most widely used shell in Linux systems. It is used as default login shell in Linux systems and in macOS. It can also be installed on Windows OS.
- CSH (C SHell) – The C shell’s syntax and usage are very similar to the C programming language.
- KSC (Korn Shell) – The Korn Shell also was the base for the POSIX Shell standard specifications, etc. Each shell does the same job but understand different commands and provide different built-in functions.
A shell script comprises following elements –
- Shell Keywords – if, else, break etc.
- Shell commands – cd, ls, echo, pwd, touch etc.
- Functions
- Control flow – if..then..else, case and shell loops etc.
1). Basic Terminal Navigation Commands:
- ls : To get the list of all the files or folders.
- ls -l: Optional flags are added to ls to modify default behavior, listing contents in extended form -l is used for “long” output
-
ls -a: Lists of all files including the hidden files, add -a flag
-
cd: Used to change the directory.
-
du: Show disk usage.
-
pwd: Show the present working directory.
-
man: Used to show the manual of any command present in Linux.
-
rmdir: It is used to delete a directory if it is empty.
-
ln file1 file2: Creates a physical link.
-
ln -s file1 file2: Creates a symbolic link.
-
locate: It is used to locate a file in Linux System
-
echo: This command helps us move some data, usually text into a file.
-
df: It is used to see the available disk space in each of the partitions in your system.
-
tar: Used to work with tarballs (or files compressed in a tarball archive)
2). Displaying the file contents on the terminal:
-
cat: It is generally used to concatenate the files. It gives the output on the standard output. (must follow the file after otherwise you'll stick, to cancel, stop it press ## CTRL + C)
-
more: It is a filter for paging through text one screenful at a time.
-
less: It is used to viewing the files instead of opening the file.Similar to more command but it allows backward as well as forward movement.
-
head : Used to print the first N lines of a file. It accepts N as input and the default value of N is 10.
-
tail : Used to print the last N-1 lines of a file. It accepts N as input and the default value of N is 10.
3). File and Directory Manipulation Commands:
-
mkdir : Used to create a directory if not already exist. It accepts the directory name as an input parameter.
-
cp : This command will copy the files and directories from the source path to the destination path. It can copy a file/directory with the new name to the destination path. It accepts the source file/directory and destination file/directory.
-
mv : Used to move the files or directories. This command’s working is almost similar to cp command but it deletes a copy of the file or directory from the source path.
-
rm : Used to remove files or directories.
-
touch : Used to create or update a file.
4). Extract, sort, and filter data Commands:
-
grep : This command is used to search for the specified text in a file.
-
grep with Regular Expressions: Used to search for text using specific regular expressions in file.
-
sort : This command is used to sort the contents of files.
-
wc : Used to count the number of characters, words in a file.
-
cut : Used to cut a specified part of a file.