Mastering Common Linux Commands: From Beginner to Pro

Mastering Common Linux Commands: From Beginner to Pro

The article begins with the basics, introducing fundamental commands for navigating the file system, managing files and directories, and working with processes. It then progresses to more advanced commands that enhance your Linux skills and enable you to perform complex tasks efficiently.

This article aims to share valuable knowledge with the community. By reading and applying the concepts discussed, readers will become more proficient in using Linux commands and be equipped to handle a variety of tasks confidently.

Whether you're a student, professional, or simply interested in Linux, Mastering Common Linux Commands: From Beginner to Pro is an excellent resource to enhance your Linux skills and empower you to make the most of this versatile operating system.

Most Common command used :

uname -aDisplay Linux system information
whoamiWho you are logged in as
uname -rDisplay kernel release information
uptimeShow how long the system has been running + load
hostnameShow system host name
last rebootShow system reboot history
dateShow the current date and time
calShow this month's calendar
wDisplay who is online
free -hDisplay free and used memory ( -h for human readable, -m for MB, -g for GB.)
topDisplay and manage the top processes
watch df -hExecute "df -h", showing periodic updates
lastDisplay the last users who have logged onto the system.
whoShow who is logged into the system.
useradd -c "John Smith" -m johnCreate an account named john, with a comment of "John Smith" and create the user's home directory.
userdel johnDelete the john account.
usermod -aG sales johnAdd the john account to the sales group
ls -alList all files in a long listing (detailed) format
pwdDisplay the present working directory
mkdir directoryCreate a directory
rm fileRemove (delete) file
rm -r directoryRemove the directory and its contents recursively
rm -f fileForce removal of file without prompting for confirmation
rm -rf directoryForcefully remove directory recursively
cp file1 file2Copy file1 to file2
cp -r source_directory destinationCopy source_directory recursively to destination. If destination exists, copy source_directory into destination, otherwise create destination with the contents of source_directory.
mv file1 file2Rename or move file1 to file2. If file2 is an existing directory, move file1 into directory file2
touch fileCreate an empty file or update the access and modification times of file.
cat fileView the contents of file
head fileDisplay the first 10 lines of file
tail fileDisplay the last 10 lines of file
psDisplay your currently running processes
grep pattern fileSearch for pattern in file
dfDisplay disk usage.
df -hShow free and used space on mounted filesystems
df -HDisplay disk usage in gigabytes, megabytes, or kilobytes.
rm -r *Delete every file and every directory.
killTerminate processes without having to log out or reboot.
loginCreate a new session on the system.
lsofList open files.
cal -yPrints current year's calendar.
sudo su root1Switch to user "root1".
useradd "root1"Creates a user "root1".
passwd "root1"Assign password to user "root1".
lognameDisplay the login name of the current user.
mkdir myfiles filesCreate two directories (myfiles, files).
diff 1.txt 2.txtCompare the contents of two files (1.txt, 2.txt).
wc 1.txtTells you how many lines, words, and characters there are in a file (1.txt).
cp 1.txt 0.txtReplace the contents of 0.txt with that of 1.txt.
mv 1.txt 0.txtRename a file named 1.txt to 0.txt.
echo 'Hello World' > myfiles.txtCreate a file (myfiles.txt) containing a text (Hello World).

Last blog, we learn some basic Linux commands and their uses. Today we will learn more about command and their uses. So, let's learn about basic and some advanced Linux command.

To view what's written in a file.

Cat is a command through which you can review what's written in a file.

For creating Linux Files we used different commands like

cat command: to view what is written in the file we used the cat command

vim command
touch command
nano command

File Systems Permissions and Ownership

File System Hierarchy:

Linux follows a hierarchical file system structure, starting from the root directory / and branching into various directories:

  • Root Directory (/): The top-level directory containing all other directories and files.

  • /bin, /sbin, /usr, /etc: Directories holding essential binaries, system binaries, user binaries, and system configuration files, respectively.

  • /home: Houses user home directories.

  • /var: Contains variable files like logs, databases, mail, and more.

  • /tmp: Used for temporary files.

  • /opt: Optional software applications often reside here.

  • /dev: Device files for hardware components.

  • /proc: Virtual file system that provides access to kernel and process information.

File Permissions and Ownership:

Linux file permissions are categorized into three levels: owner, group, and others. These permissions are represented by read (r), write (w), and execute (x).

Types of Permissions:

  1. Read (r): Allows reading and viewing the contents of a file or directory.

  2. Write (w): Permits modifying, appending, or deleting a file's contents, and creating or deleting files within a directory.

  3. Execute (x): Grants the ability to execute files or traverse through directories.

Let's use an example to illustrate this:

To change the access permissions of files

Chmod is the command used to change the access permissions of a file.

chmod

Example:
rwx rwx rwx
In that values are r=4,w=2,x=1 so 4+2+1=7 then total value is 777
chmod 777 permission says all permission are given like we read, write and execute

chmod 777 test-file.txt

The command chmod 777 test-file sets the access permissions of the file test-file to read, write, and execute for the owner, group, and others.

chmod 600 filename

To give read and write permissions to the owner, and no permissions to group and others

chmod 750 filename

To give full permissions (read, write, and execute) to the owner, and read and execute permissions to the group, and no permissions to others

chmod 700 filename

To give full permissions (read, write, and execute) to the owner, and no permissions to the group and others:

chmod 444 filename

To give only read permissions to the owner, group, and others. not write and execute permissions to owner, group and others.

or you can simply says: to remove write and execute permissions from the owner, group, and others

This permission setting is commonly used when you want to make a file read-only for everyone, preventing any modifications or execution.

Now, let start deeply discuss file access permission.

chmod 755 filename

To give read, write, and execute permissions to the owner, and read, execute permission to group and others

Let's break down the permission code:

  • The leftmost digit, 7, represents the permission for the owner of the file.

  • The middle digit, 5, represents the permission for the group that the file belongs to.

  • The rightmost digit, 5, represents the permission for other users (not the owner or group).

Each digit in the permission code represents a combination of read (4), write (2), and execute (1) permissions.

Here's a breakdown of the permission codes:

  • 7: This grants the owner of the file full permissions, which include read, write, and execute.

  • 5: This grants the group read and execute permissions, but not write permissions.

  • 5: This grants other users read and execute permissions, but not write permissions.

In summary, executing chmod 755 filename sets the file permissions as follows:

  • The owner of the file has full control (read, write, and execute).

  • The group has read and execute permissions.

  • Other users have read and execute permissions.

This permission setting is commonly used for executable files or scripts that should be readable and executable by all users, but not writable to maintain security.

Chown Command file Permissions:

chown Command:

chown stands for "change owner" and is used to change the owner of a file or directory.

sudo chown new_owner filename
  • new_owner: The new user or user ID to assign as the owner.

  • file(s): The file or list of files/directories to which ownership will be changed.

Example:

Let's assume you want to change the owner of a file named test-file.txt to a user named niloy [ my younger brother name ]:

sudo chown niloy test-file.txt

Here, after executing the "ls -l" command, we noticed that the owner of our test-file.txt is 'ubuntu,' right?
Now, let's change the file owner from the 'ubuntu' user to a new user named 'niloy' and grant permission.

Let's attempt to implement this.

sudo chown niloy test-file.txt

After executing this command, we see an error message stating "invalid user" because we don't have any user named 'niloy.' So, first, let's create a user named 'niloy,' and then let's try again to see if we can change the file's owner.

After creating the user, when we execute the same command, we can observe that the file's owner name has indeed been changed.

chgrp group Ownership file Permissions :

chgrp Command:

chgrp stands for "change group" and is used to change the group ownership of a file or directory.

chgrp new_group filename
  • new_group: The new group or group ID to assign as the group owner.

  • file(s): The file or list of files/directories to which group ownership will be changed.

Example:

Changing the group ownership of the test-file.txt file to newgroup:

chgrp newgroup test-file.txt

Here, after executing the "ls -l" command, we noticed that the group of our test-file.txt is 'ubuntu,' right?
Now, let's change the file group from the 'ubuntu' user to a new user named 'niloy' and grant permission.

Let's attempt to implement this.

We had created a new user named 'niloy' earlier to change the file's owner, right?

We know that when creating any user, a default group with the same name is automatically created for that user.
To verify this, we executed the following command.

id niloy

After executing it, we observed that 'niloy' indeed has a default group.

So now, we will change the group ownership of our file to a new group named 'niloy' using the command below.

sudo chgrp niloy test-file.txt

Now, after running the 'ls -l' command again, we can see that the group owner of our file 'test-file.txt' has changed.

Permissions and Use Cases:

  • File Ownership Change: Use chown when you want to change both the user and group ownership of a file or directory.

  • Group Ownership Change: Use chgrp when you only want to modify the group ownership without altering the user owner.

These commands are typically used by system administrators to manage file and directory ownership, especially when users need access to specific files or when group permissions require modification for collaboration purposes. Proper ownership and group assignments are critical for effective access control and data security in Linux systems.

Access Control Lists (ACLs)

Access Control Lists (ACLs) in Linux are an extension of the standard file permission system, allowing for more granular control over access to files and directories. ACLs enable you to define permissions for specific users or groups beyond the traditional owner, group, and others classifications. They provide a finer level of access control and are useful in multi-user environments or when more complex permission settings are required.

Usage of ACLs:

Viewing ACLs:

To view ACLs for a file or directory, use the getfacl command:

$getfacl filename

We've previously applied file permissions to the file named 'test-file'.

Now, we'll use the 'getfacl' command to check the permissions of this file. This command will display the file name, the file's user owner, group owner, and the permissions represented by 'read' (r), 'write' (w), and 'execute' (x) for three categories: owner, group, and others.

For example---

getfacl test-file.txt

file: test-file.txt

owner: niloy

group: niloy

user::rwx 
group::rwx
other::rwx

After executing the command "getfacl test-file.txt," we noticed in the terminal that the command "getfacl" wasn't found but suggested to be installed with: "sudo apt install acl."

Since we need the 'acl' command to check permissions, we must install 'acl' first. Therefore, let's execute the following command:

sudo apt install acl

Now, we'll execute the command "getfacl test-file.txt" again to check the information related to the "test-file.txt" file.

Once executed, it will display all the information about the file, including its name, owner's name, group's name, and permission categories for users, groups, and others.

For a clearer understanding of ACL permissions, let's create a new file named "nahid-test-file.txt."

Now, to view the access control list of this file, we'll execute the command 'getfacl nahid-test-file.txt.'

getfacl nahid-test-file.txt

After execution, we see that the owner and group name of this file are 'ubuntu' because, by default, we're working in the 'ubuntu' environment. Also, observing the permissions of this file, we find that both the owner and group have read and write permissions, while others have only read permissions.

Setting ACLs:

setfacl command

The setfacl command is used to set or modify ACLs for files and directories:

If we want to modify our file's permissions easily, we'll use the "setfacl" command:

setfacl -m u:username:permissions filename

Let's add a new user for our file and grant full permissions to that user using the "setfacl" command.

For instance, we'll assign a new user named "nahid" for our file and grant full permissions to that user.

sudo setfacl -m u:nahid:rwx nahid-test-file.txt

The issue here is that we haven't created a user named "nahid" yet, so I've faced this problem. Thus, I'll create the "nahid" user now.

After executing the command, we can see that the new user named "nahid" has been given "rwx" full permissions.

Removing ACL Entry:

Remove an ACL entry for a user on a file:

setfacl -x u:username filename

Now, if we want to remove the "nahid" user specifically for this file, we'll use the following command:

sudo setfacl -x u:nahid nahid-test-file.txt

After executing the removal command, we can observe that the user list for this specific file no longer shows any user named "nahid," meaning that there are no permissions for the "nahid" user for this file.

ACLs provide a powerful means of managing access control in Linux, allowing administrators to define fine-grained permissions for users and groups. They are particularly useful in scenarios where more detailed access control is required beyond the scope of standard permissions. However, they should be used thoughtfully and documented carefully to maintain system security and integrity.

Histroy:

To check which commands you have run till now.

history command is used to check the commands you have run till now.

histroy

To remove a directory/ Folder.

To remove a directory, we use the “rm” command. We use “rm -r” if the directory is non-empty.

Some common options that you can use with the rm command are:

  • -f: Force removal of files without prompting for confirmation.

  • -r: Recursively remove directories and their contents.

The rmdir command in Linux is used to remove empty directories from the file system.

rm -rf It is used to forcefully and recursively remove files and directories from the file system, including non-empty directories.

rm-rp = Remove non-empty directory including parent and subdirectory

rm-r = Removes empty directories

To create a fruits.txt file and view the content.

Add content in devops.txt (One in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava.

To Show only the top three fruits from the file.

To show only the top three fruits we will use the head command. The Linux head command prints the first lines of one or more files to standard output. By default, it shows the first 10 lines.

Show only the bottom three fruits from the file.

To show only the bottom three fruits we will use the tail command. Tail is a command which prints the last few lines (10 lines by default) of a certain file.

To create another file Colors.txt and view the content.

Add content in Colors.txt (One in each line) - Red, Pink, White, Black, Blue, Orange, Purple, and Grey.

We used touch and vim commands for creating the colors.txt file

Add content in Colors.txt (One in each line) - Red, Pink, White, Black, Blue, Orange, Purple, and Grey.

To find the difference between the fruits.txt and Colors.txt files.

diffCommand to find the difference between the content of two files.

Thank you for reading this blog. I hope you learned something new today! If you found this blog helpful, please like, share, and follow me for more blog posts like this in the future.

— Happy Learning !!!

Let’s connect!!!!

Linkedin

Github

Mail

Medium