Table of contents
- Introduction
- What are Shell Commands?
- Basic Unix Commands
- a. The Listing command (ls)
- b. The change directory Command (cd)
- c. The command that shows you your current location (pwd)
- d. The make/create directory Command
- e. The touch Command (touch)
- f. The move/rename Command (mv)
- g. The cat Command (CAT)
- h. The less Command (less)
- i. The echo Command (echo)
- j. The remove Command (rm)
- k. The date Command (date)
- l. The word count Command (wc)
- m. The tail Command (tail)
- n. The copy Command (cp)
- Advantages of using a shell
- Additional Information
Introduction
Have you ever seen any web developer work without using shell commands? Working on a terminal without any knowledge on shell command is like pouring water on a stone, expecting it to get soft. Anyone in the tech world should have come across these common shell commands: ls, cd, pwd, mkdir and touch. What is the purpose of shell commands? Shell commands are simply used to communicate with the system. Even if you do most of your Git operations through a client, you must sometimes work in the shell.
What are Shell Commands?
A shell is a type of computer program called command-line interpreter that lets Linux and Unix users control their operating systems with command-line interfaces. In other words, a shell is a command line interface to your computer. On some platforms, the shell is called command interpreter because it interprets the commands the user issues. A shell can be used to create and delete files and directories( folder), start and stop processes that run programs, manage processes and process resources, and much more. The most common shells on Linux and MacOS are bash and tcsh.
Basic Unix Commands
a. The Listing command (ls)
This command is used to list out all files in a given directory.
Syntax:-
ls "directory"
This listing command can also help you when stuck, by inputting the command:
ls --help
b. The change directory Command (cd)
This command is used to change directory, that is; move from one folder to another. For instance, I'm in my OneDrive on my command line and I want to get to my desktop:
Syntax:-
cd Desktop
I can only do this if my desktop is in my OneDrive. In a situation where I'm in my desktop and I need to go back to my OneDrive, I would input:
cd ..
the (..) takes you to your previous command, while (.) shows you your current location.
c. The command that shows you your current location (pwd)
The "pwd" command shows you the location you are currently in, no matter where you are.
Syntax:-
pwd
d. The make/create directory Command
This command is used to create a directory.
Syntax:-
mkdir directoryname
The directoryname is expected to be the name of the folder/directory you intend to create.
e. The touch Command (touch)
This command is used to create a file.
Syntax:-
touch filename
The filename is expected to be the name of the file you intend to create.
f. The move/rename Command (mv)
This command is used to move a file or directory into a directory, as well as rename a directory.
Syntax for moving a file:-
mv file directory
Syntax for renaming a directory:-
mv directoryname newdirectoryname
The directoryname is the name of the directory/folder you wish to rename, while newdirectlyname is the new name of your directory.
g. The cat Command (CAT)
This command is used to read a file.
Syntax:-
cat file
h. The less Command (less)
This command is used for reading a file. The difference between this and the cat command is this command takes you to a different tab and to exit, you input; (esc):wq or q .
Syntax:-
less file
i. The echo Command (echo)
This command is used to write inside a file.
Syntax:-
echo text > file
When using the "echo" command, one has to be careful to prevent loss of data. We use ">"
to write for the first time or replace current information and ">>"
to write in the next line in order to avoid wiping out current information.
j. The remove Command (rm)
This command is used to delete files from a directory.
Syntax:-
rm -f file
To delete a directory, we use:
rm -rf directory
k. The date Command (date)
This command is used to show the current date and time.
Syntax:-
date
l. The word count Command (wc)
This is used to count the lines, words and character of a file.
Syntax:-
wc file
m. The tail Command (tail)
This command is used to read the last part of a file.
Syntax:-
tail file
n. The copy Command (cp)
This command is used to copy files or folders.
Syntax for copying a folder:-
cp -r folder filepath
The filepath is the location you wish to copy the file into.
Syntax for copying a file:-
cp file filepath
Advantages of using a shell
It allows one to automate repetitive tasks by creating scripts or shell programs.
It provides access to the system's utilities and functions.
It offers flexibility.
It manages the interaction between you and the operating system.
Additional Information
For more information, click here.