In this tutorial, we will show you learn basic Linux commands system. For those of you who didn’t know, A basic understanding of the Linux command line environment (also commonly known as the “shell” or the “terminal”) is important if you ever need to set up a special configuration, install an application manually, or you just want to have a better understanding of how your web site works. The command line gives you more power over your system and access to features that are not available through a graphical interface.
Before we get started, let’s make sure you’re equipped with the basics. If you’re not familiar with Linux or don’t know your way around the shell, this might not be the article for you. But, assuming you’re already a pro at navigating your VPS, let’s move on to the good stuff. The installation process we’re about to tackle is a piece of cake. All you need is a root account, and you’re good to go. Of course, if you’re not already running in root, you might need to add ‘sudo’ to your commands to get those coveted root privileges.
Prerequisites
- A server running one of the following operating systems: Ubuntu or any other Debian-based and RHEL-based.
- SSH access to the server (or just open Terminal if you’re on a desktop).
- A
non-root sudo user
or access to theroot user
. We recommend acting as anon-root sudo user
, however, as you can harm your system if you’re not careful when acting as the root.
Basic Linux Commands for Beginners: A Useful Guide to Get Started
Navigating the File System
Navigating the Linux file system may seem daunting at first, but with a bit of practice, you’ll be moving around like a pro in no time. In this section, we’ll cover a few basic commands to help you navigate your way around the file system.
Present Working Directory (pwd)
The first command to get familiar with is the pwd
command, short for present working directory. This command will tell you where you are in the file system hierarchy. To use the pwd
command, simply type it into the terminal and press enter. The command will return the absolute path of the directory you are currently in.
List Contents (ls)
Once you know where you are in the file system, the next command to learn is ls
. The ls
command is used to list the contents of a directory. Simply type ls
followed by the directory you want to list. For example, ls /home/user/
will list the contents of the user’s home directory. Adding the -l
option will give you a detailed listing that includes permissions, ownership, size, and modification date and time.
Change Directory (cd)
Now that you know how to see where you are and what’s in the current directory, let’s move on to changing directories. The cd
command is used to change directory. Simply type cd
followed by the path to the directory you want to navigate to. For example, cd /var/log/
will take you to the log directory.
Relative Path
It’s important to note that the file system hierarchy is relative to your current directory. This means that if you are in the /home/user/
directory and you want to navigate to the Downloads
directory within that directory, you would use the command cd Downloads
. Notice that we didn’t use the full path starting with /
, we only specified the path relative to our current directory.
With these basic commands, you should now be able to easily navigate your way around the Linux file system. Remember to use the pwd
command to see where you are, ls
to list the contents of a directory, and cd
to navigate to a different directory.
Manipulating Files and Directories
In Linux, manipulating files and directories is an important task that every user must know. Below we’ve listed several basic commands that will help you move, rename, and delete files and directories with ease.
Moving Directories and Files
The mv
command allows you to move files and directories from one location to another. To use this command, simply type mv
followed by the name of the file or directory and the location you want to move it to. For example, if you want to move the file file1.txt
from the current directory to the directory /home/user1/
, you would enter the command mv file1.txt /home/user1/
.
Renaming Files and Directories
To rename a file, use the mv
command followed by the current name of the file, along with the new name. For example, if you want to rename the file file1.txt
to file2.txt
, you would enter the command mv file1.txt file2.txt
.
To rename a directory, use the mv
command followed by the current name of the directory, along with the new name. For example, if you want to rename the directory dir1
to dir2
, you would enter the command mv dir1/ dir2/
.
Creating and Deleting Directories
To create a new directory, use the mkdir
command, followed by the name of the new directory. For example, if you want to create a directory called newdir
, you would enter the command mkdir newdir
.
To delete a file or directory, use the rm
command. If you want to delete a file, simply enter the command rm
followed by the name of the file. For example, if you want to delete the file file1.txt
, you would enter the command rm file1.txt
.
If you want to delete a directory and all of its contents, use the command rm -r
followed by the name of the directory. For example, if you want to delete the directory dir1
and all of its contents, you would enter the command rm -r dir1/
.
Checking File and Directory Permissions
To check the permissions of a file or directory, use the command ls -l
. This command will display a list of all files and directories in the current directory, along with their permissions, owners, and other information.
In conclusion, these basic commands will enable you to effectively manipulate files and directories in Linux. By becoming familiar with these commands, you’ll be able to efficiently manage your files and directories with ease.
Working with Text Files
Now that we’ve covered some basic Linux commands, let’s explore working with text files.
As we mentioned earlier, the Nano text editor is a great tool for editing files. To open a file using Nano, simply type nano
followed by the file name. For example, if we wanted to edit a file named example.txt
we would type:
nano example.txt
Once the file is open, we can make changes as needed. To save the changes and exit, press CTRL + X
and then type Y
to confirm the changes.
Sometimes we might need to search through a text file for specific content. The grep
command is perfect for this. Let’s say we have a text file named data.txt
that contains a list of names and phone numbers. If we want to find all the entries that include the name “meilana”, we would type:
grep "meilana" data.txt
This will return all lines in data.txt
that contain the name “John”.
Another useful command is sort
. This command allows us to sort the contents of a file alphabetically or numerically. For example, if we have a file named numbers.txt
that contains a list of integers, we can sort the file in ascending order by typing:
sort -n numbers.txt
We can also sort the file in descending order by adding the -r
flag:
sort -nr numbers.txt
As we work with text files, it’s important to keep track of file sizes. The wc
command allows us to count lines, words, and characters in a file. For example, to count the number of lines in a file named report.txt
, we would type:
wc -l report.txt
These simple text commands can help us efficiently work with and manipulate text files in Linux.
Managing User Accounts and Permissions
As beginners, one of the primary tasks you will encounter while using Linux services is managing user accounts and associated permissions. Linux operating systems allow for multiple user accounts, and each account may have distinct permissions to access various resources. In this section, we’ll take you through some of the basic commands you can use to manage user accounts and associated permissions.
Creating a User Account
To create a new user account, you can use the following command:
$ sudo adduser username
Replace “username” with the name you want to assign to the new user. Upon executing the command, you will be prompted to enter a password for the user account and provide additional information such as full name, phone number, and others if needed.
Managing User Accounts
The userdel command can be used to delete an existing user account. The syntax for removing an account is as follows:
$ sudo userdel username
Replace the “username” with the name of the user account you want to delete. Keep in mind that if the user is currently logged in, you won’t be able to delete the account, thus you may require to log the user out first.
Setting User Permissions
Permissions are an essential aspect of Linux systems that determine the level of access and operations that a user can perform. User accounts can be classified into three distinct categories: owner, group, and other. By default, the owner of a file or directory has full permissions. However, you may need to modify these permissions for different user accounts. This can be done with commands such as chmod
, chown
, and chgrp
.
Changing Ownership
The chown command can be used to modify ownership of a file or directory in the Linux system. For instance, the following command changes ownership of a file to a new user:
$ sudo chown newuser filename
Where “newuser” is the name of the user you’d like to assign the file, and “filename” is the name of the file you want to change ownership.
Changing Permissions
The chmod
command is used to modify the permissions of a file or a directory. For instance, you can use the following command to add write access to the file only for a new user:
$ sudo chmod u+w filename
In the example above, “u” indicates owner, “+w” means add write access, and “filename” is the name of the file you want to change permissions.
In summary, new user accounts can be created using the adduser command, existing user accounts can be deleted using userdel command, and permissions can be modified using chown and chmod command. These basic Linux commands are useful for managing user accounts and permissions for beginners.
Table Basic Linux Commands
cat [filename] | Display the file’s contents to the standard output device (usually your monitor). |
cd /directorypath | Change to the directory. |
chmod [options] mode filename | Change a file’s permissions. |
chown [options] filename | Change who owns a file. |
clear | Clear a command-line screen/window for a fresh start. |
cp [options] source destination | Copy files and directories. |
date [options] | Display or set the system date and time. |
df [options] | The display used and available disk space. |
du [options] | Show how much space each file takes up. |
file [options] filename | Determine what type of data is within a file. |
find [pathname] [expression] | Search for files matching a provided pattern. |
grep [options] pattern [filesname] | Search files or output for a particular pattern. |
kill [options] pid | Stop a process. If the process refuses to stop, use kill -9 pid. |
less [options] [filename] | View the contents of a file one page at a time. |
ln [options] source [destination] | Create a shortcut. |
locate filename | Search a copy of your filesystem for the specified filename. |
lpr [options] | Send a print job. |
ls [options] | List directory contents. |
man [command] | Display the help information for the specified command. |
mkdir [options] directory | Create a new directory. |
mv [options] source destination | Rename or move the file(s) or directories. |
passwd [name [password]] | Change the password or allow (the system administrator) to change any password. |
ps [options] | Display a snapshot of the currently running processes. |
pwd | Display the pathname for the current directory. |
rm [options] directory | Remove (delete) file(s) and/or directories. |
rmdir [options] directory | Delete empty directories. |
ssh [options] user@machine | Remotely log in to another Linux machine, over the network. Leave an ssh session by typing exit. |
su [options] [user [arguments]] | Switch to another user account. |
tail [options] [filename] | Display the last n lines of a file (the default is 10). |
tar [options] filename | Store and extract files from a tarfile (.tar) or tarball (.tar.gz or .tgz). |
top | Displays the resources being used on your system. Press q to exit. |
touch filename | Create an empty file with the specified name. |
who [options] | Display who is logged on. |
Congratulations! You have successfully learned basic commands for Linux. Basic Linux commands help users execute tasks easily and effectively. It might take a while to remember some of the basic commands, but nothing is impossible with lots of practice. In the end, knowing and mastering these basic Linux commands will be undoubtedly beneficial for you.