Command Line
The command line, also known as the command-line interface (CLI), is a text-based interface used to interact with a computer’s operating system or other software. Users can enter commands into the command line to perform various tasks, such as launching programs, manipulating files and directories, configuring system settings, and more. The command line often provides more advanced and flexible control over a system than graphical user interfaces (GUIs) and is commonly used by developers, system administrators, and power users.
Introduction to the Shell
To explore the world of Linux, first, you’ll need to get acquainted with the translator, the one who will do the talking for you. To operate the Linux system, you would need to put out commands for programs or tasks you’d want to execute. The program that recognizes your command from the keyboard and interprets it to the kernel for execution is called the shell. In different graphical user interfaces (GUI) you have probably used text-based interfaces called ‘Terminal’ or ‘Console’ which are just different representations of the shell, and they all have a similar purpose of operation at the core.
Most Linux distributions are equipped with the default bash (Bourne again shell) program, and it is likely that you’ll use the same program as well. For rookies who don’t want to install Linux OS before getting a basic grasp of it, the web service JSLinux lets you run a Linux virtual module on your browser completely free. In this tutorial, I’ll be using the JSLinux service to ease newcomers into it.
Normally, most shell prompts end with a ‘$’ sign for normal users. Some come with other signs too, as the older shell prompts end with ‘%’. For root users, the sign changes to ‘#’. An image showing The JSLinux shell interface is shown below. Notice how the end sign is ‘#’, meaning the web service lets you log in as a root user.
Let me show you an example of how to put out simple commands on your shell interface. A simple command echo lets you get back whatever you write beside it as plain text output. The utilization of the echo command will look like this.
pwd
In Linux, a hierarchical directory tree design is utilized to keep all your files organized. You can find out where you are in the file system by using the print-working-directory command. When this command is run on the computer, the current directory’s absolute pathname will be shown to the user.
A certain pathname indicates a certain file’s location in the system It’s more like an address that helps you realize exactly which route of the filesystem you’ll need to take to locate the file. To use the print-working-directory command simply type in pwd like below.
Syntax: # pwd
Notice that here a pathname saying root has appeared. This is the first directory in any Linux file system.
Change Directory (cd)
Mastering the concept of the path in Linux is extremely important as you’ll need paths to navigate your way around the filesystem. The command pwd will tell you the path of your current directory and another change directory command cd will let you reach the directory you want.
There are two sorts of path descriptions that you can use: absolute pathname, and relative pathname. An absolute pathname is the path description of a directory from the home/root directory. To illustrate an example, let’s see an absolute pathname: /home/George/pictures.
Syntax: # cd /home
In contrast, relative pathnames are paths from one directory to another. Suppose you would like to access a file from your current directory. It’d be really bizarre if to do that, you had to start describing the path all the way from the home directory. Relative pathnames save you from that sort of hassle. In the previous example, if you’d like to access a file ‘Harry’ inside pictures, simply write ‘# cd Harry’.
Besides these, there are far more compact command expressing techniques associated to change directory like, ‘# cd .’, ‘# cd ..’, ‘# cd~’, ‘# cd-‘. These commands will respectively take you to the current directory, parent directory, home directory, and previous directory.
mkdir
To store your files or to further extend your nested filesystem you’ll need to make directories. With the command mkdir, you’ll be able to make new directories where you can reach using the command cd and create more files. The picture below will show how the command works.
Syntax: # mkdir filename
Creating a File (touch)
Let’s learn how to create a file in the directory you desire. Reach the directory you want to create your new file in first. You can easily create a new file there using the touch command. The following example illustrates a scenario where you create a new file named ‘felina’ in your home directory.
Syntax: # touch filename
List Directories (ls)
Specified pathnames will let you access different files currently stored in your filesystem. But what if you forget the name of a file! Will the file be lost forever? No worries, as the list directories command, ls, will show you a list of all the available directories. Let me show you an example.
Syntax: # ls
Here, we can see there are only two files present in the home directory. One of them is the file we created in the previous example.
File Description (file)
The command ‘file’ is used to determine and show the description of a file. Remember you’ll have to type in the whole name of the file, including extensions, for the system to acknowledge it. Here’s an example where we determine the description of a PNG file.
Syntax: # file filename.ext
Cat
The cat command is short for its function which is to concatenate files. It’s mainly a utility command that can perform various tasks, like printing the content of a file on the output interface, including text into a file, combining multiple files, etc. Check out an example of the command’s simplest function that simply prints out the texts of a file.
Syntax: # cat filename.ext
Sadly, the command is not convenient for large files, so better keep that in mind.
Less
This command does nothing that it sounds like. It’s a utility command that lets you see the writings of a text file on the screen, one page at a time. Unlike the cat command, this one doesn’t face any issues while dealing with larger files, mostly because it accesses and presents the file page by page. See the example below to realize the command’s functionality better.
Syntax: # less filename.ext
History
Using this history command, you can access the list of your previously executed commands and re-execute any of them. This saves you the hassle of retyping the same commands countless times. Just type in history to access the list and after getting the list, switch between options using the up-down arrows and press enter to execute the desired command. See the example below for a better understanding.
Syntax: # history
Clear
Another useful utility command is clear, which clears up the shell prompt and gives you a clean slate to type in commands. Remember that clearly doesn’t erase your history, it just clears up the interface screen. You can always access the list of your previous commands by typing in history. See the after-effects of the clear command below.
Syntax: # clear
Cp
Copying a file or files to a certain directory, using the shell interface, is a really simple process. The command cp lets you copy a certain file into a certain directory. Just type in the cp command, type the file name, and then type in the desired directory; your file will be copied. See how to use this command in the picture below.
Syntax: # cat filename.ext /home/dirname
A very helpful, additional expression related to the cp command is ‘cp -r’, which lets you copy all the files in a directory to another directory of your choice. See the example below.
mv
The mv command is a versatile utility command. You can use it to move a file or multiple files to a new directory. This command is also used for renaming a file or directory. See the examples below.
For moving one file:
Syntax: # mv filename.ext /home/dirname
For moving multiple files:
Syntax: # mv filename1.ext filename2.ext /home/dirname
For renaming:
Syntax: # mv prev_filename.ext new_filename.ext
rm
Use the rm command to remove any file you want. Remember files once removed can’t be restored in Linux systems. So, be careful while using this command. Check the example below to see how to use it.
Syntax: # mv filename.ext
find
For finding a specific file in a directory, the shell has the find command. To apply the find command you will need to specify the directory where the search will take place and the name of the file you’re looking for. For further specification, the type of the file can be mentioned. Know that the search will take place not only in the mentioned directory but also in the subdirectories as well. Take a look at the following example to know how to use this command.
Syntax: # find /home/dirname filename.ext
help
The help command is a great assistance tool for Linux users, especially rookies. Applying this command, you can get adequate descriptions like utilization, syntax, structure, etc. for any command as well as information about the associated expressions available. This example below will further clarify the help command’s functionality. Here using the help command, we have obtained a description of the command pwd.
Syntax: # help command_name
Man
The man command stands short for manual. With this command, you can find system built-in, user manual pages for a specified command. The manual page will contain in detail information like name, synopsis, return values, description, etc. about the inquired command. The picture below shows the manual page obtained for the command find.
Syntax: # man command_name
Alias
alias command is a powerful tool to enhance user experience. With the use of this command, you can assign a short alias for a specific command, so that you can avoid repeated typing of long command names. See the example below.
Syntax: # alias your_name=’command_name’
Now, I can use the string c instead of cd, for the cd command.
logout
The function of the logout command is to exit the shell interface. You can also use the exit command for this. Both of these have the same syntax. Check out the picture below.
Syntax: # logout