Beginner’s Guide to Linux Command Line
Introduction to Linux CLI and Commonly Used Commands
Why Is It Essential To Learn the Linux Command Line?
The primary reason is that most of the applications in the production environment are running on Linux servers. So, the next question that comes into mind is why can’t we use a more user-friendly GUI in Linux. The problem with running a GUI in the production servers is resource wastage. GUI demands much more resources to run compared to a CLI-only server. Most of the time you’ll have to log in to your application server remotely. GUI also demands more resources in connecting remotely as well. So, that’s why we would connect to our Linux server CLI using something like SSH.
Setting Up Your Linux Environment for Testing
All you have to do is install a virtualization software like Oracle VM VirtualBox and set up a Linux virtual machine on top of your Windows OS. First, install the virtualization software on your PC. Then download an ISO image of any Linux Distribution. Ubuntu, Fedora are good choices. Next, open your virtualization software and create a virtual machine using the ISO you downloaded. After setting it up, create a snapshot of your VM just in case.
You can download all of the above for free, even the Linux Operating System. All of them are Free Open Source Software.
Understanding Linux Directory Structure
Most of us are used to Microsoft Windows Directory Structure. So, it is better to learn about the Linux Directory structure before stepping into commands.
Above are some of the important directories in Linux. As you can see, it is different from the partition-like structure in Windows. Let’s look at a couple of the most important directories.
/ :
Every file starts from the root directory. Please note that /root is different from /.
/home:
Normally, this contains the home directories of the users except root.
ex: /home/Nimal, /home/Kamal
/root:
The root user is the superuser in Linux. Much like the administrator in Windows. /root is the home directory of the root user.
/etc:
/etc contains the configuration files related to programs. ex: /etc/httpd/conf/httpd.conf
/var:
Contains /var/log. /var/log consists of system log files important for troubleshooting.
File Navigation
In Linux, we can specify the path to a file or a directory in 2 methods.
Let’s take an example. Pretend that our current location is “/home”. And we want to specify the path to “hello.txt”.
We can specify the Absolute path as: /home/Saman/hello.txt
We can specify the Relative path as: Saman/hello.txt
We can navigate using the “cd” command.
# cd <directory_Path>
ex: Let’s say we want to change the directory to Saman. We can pass the absolute path or the relative path to cd.
# cd Saman or # cd /home/Saman
Then, you can view your current directory using “ pwd”(present working directory) command.
Other Commonly Used Commands
- “ls” command: list directory contents. You can pass -l option to get a detailed description. ex:
# ls /home/Saman
# ls -l /home/Saman
2. “cp” and “mv” commands: copy and move files
# cp [options] SOURCE DESTINATION
3. “cat” command: print the content of a file to the terminal
# cat Saman/hello.txt
4. “rm” (remove) command: remove files or directories
to remove hello_copy.txt
# rm Saman/folder1/hello_copy.txt
to remove a directory, we have to pass the -r option.
# rm -r Saman/folder1
Getting Help
It is not practical to remember everything to work with the command line. Knowing where to look for a particular command or an option is enough.
We can get quick help by passing “- -help” after the command.
# cp --help
Or we can get more detailed help from manual pages using the “man” command. Use arrow keys to move up and down and “q” to exit the manual page.
# man cp
Hope you got some basic understanding to work with Linux CLI from this article.
Please leave a comment below if you see any mistakes or if you know another better way to do the above operations or a better explanation. There’s almost always a shortcut or another best practice when working with Linux.