Archive File (Tar)
When your file is very big and you want to move it to some other computer or you want to send it through the internet you need an archived (compressed) file. Another use case can be if you want to take a backup of your data. That's where archive files come in handy. One popular archive file format is tar, which stands for "tape archive".
Creating a tar file is simple. First, open a terminal on your Linux machine. Then, navigate to the directory that contains the files you want to add to the tar file. For example, if you have a bunch of pictures in your Pictures directory, you could navigate to that directory. Once you're in the directory, you can create the tar file using the below command:
Command: tar -czvf <target_folder>/<file_name>.tar.gz <source file>
Here,
-c
: This flag tells tar to create a new archive file.-z
: This flag tells tar to compress the archive using gzip, which reduces the size of the file and makes it faster to transfer.-v
: This flag stands for "verbose" and tells tar to print out information about the files as they are being added to the archive.-f
: This flag stands for "file" and tells tar to use the next argument as the name of the archive file.
So, putting it all together, the command tar -czvf <target_folder>/<file_name>.tar.gz <source file>
creates a new compressed archive file <target_folder>
called <file_name>.tar.gz
that contains all the files in the <source file>
directory.
Automation in Linux (corn)
Cron is a utility in Linux that allows you to schedule and automate commands or scripts to run at specified intervals. It's an essential tool for system administrators and power users who need to automate repetitive tasks such as backups, log file cleanups, system maintenance, and more.
How does cron work?
Cron works by reading a configuration file called the crontab file. This file contains a list of commands or scripts to be executed at specified intervals, along with the timing information for each task. The crontab file is maintained by the crontab command, which provides a set of utilities for creating, editing, and managing the file.
This is how crontab looks and as the bottom (0 8 * * * . /home/ubuntu/scripts/backup.sh
) is a command.
The crontab file is divided into six fields, separated by spaces, which specify the timing information for each task. The fields are as follows:
Minute (0-59)
Hour (0-23)
Day of the month (1-31)
Month (1-12)
Day of the week (0-6, where 0 = Sunday)
Command or script to be executed
In the above example (0 8 * * *) is a schedular to execute the shell (. /home/ubuntu/scripts/backup.sh
)
Using cron:
Using cron to automate tasks is relatively straightforward. Here are the basic steps:
Edit the
crontab
file using thecrontab -e
command.Add a new line to the
crontab
file with the timing information and command or script to be executed.Save the
crontab
file and exit the editor.To view cron jobs
crontab -e
command is used.
Sudo
Have you ever tried to run a command in Linux, only to be told that you don't have the necessary permissions? This is where the sudo
command comes in. sudo
stands for "superuser do", and it allows you to run commands with elevated privileges, also known as root or administrator permissions.
By default, Linux is designed to run with multiple users, each with their own set of permissions. The root user, also known as the system administrator, has access to all files and can perform any action on the system. However, for security reasons, it's generally not recommended to run as the root user all the time. Instead, users are assigned to different groups with different levels of permissions.
But what if you need to perform an action that requires elevated permissions? This is where sudo
comes in. By using it, you can temporarily elevate your permissions to perform a specific action.
You can use it by adding sudo
in front of any command.
Example: sudo apt install docker.io
Daemon
Have you ever wondered how Linux manages to run all of those background processes and services that keep your system running smoothly? The answer lies in the concept of daemons.
A daemon is a type of background process that runs continuously on a Linux system, performing various tasks such as handling network requests, managing system resources, and running scheduled tasks. Daemons are typically started at system boot and run in the background without any user interaction.
The name "daemon" is derived from Greek mythology, where it referred to a supernatural being that acted as an intermediary between the mortal world and the divine. In Linux, daemons act as intermediaries between the system and the user, performing tasks that are critical to the proper functioning of the system.
One of the key features of daemons is that they run in the background, meaning they don't require a user to interact with them. This allows them to perform tasks without interrupting the user's work.
For example:
httpd: This is responsible for handling incoming web requests on a web server. This daemon runs continuously in the background, ensuring that web requests are handled quickly and efficiently.
sshd: is responsible for handling incoming SSH connections, and is started automatically when the system starts up.
Managing daemons in Linux is typically done using the systemd service manager. This tool allows you to start, stop, and restart daemons, as well as configure them to start at system boot. You can also view the status of running daemons and see any error messages they generate.
Command to check: systemctl status <srevice name>
Package Management
In a nutshell, package management is the process of installing, updating, and removing software packages on a Linux system. Packages are collections of software files that are designed to work together to provide a particular function. For example, a package might include an application, along with any necessary libraries or dependencies.
Package management is important because it makes it easy to keep your system up-to-date and secure. By using a package manager, you can install and update software with just a few simple commands, without having to manually download and install each package yourself.
One of the most popular package managers in Linux is apt, which stands for "Advanced Package Tool". Below are the commands using apt.
To install Package: sudo apt-get install <package-name>
To update and upgrade: sudo apt-get update && sudo apt-get upgrade
To remove Package: sudo apt-get remove
In addition to apt, there are many other package managers available for Linux, including yum, pacman, and zypper. Each package manager has its own set of commands and syntax, but they all work in a similar way.
Overall, package management is an essential part of using Linux, and can make it easy to keep your system up-to-date and secure.
SSH/SCP
Secure Shell (SSH) and Secure Copy (SCP) are two important tools used in Linux for secure remote access and file transfer.
SSH is a network protocol that allows you to securely access a remote computer over an unsecured network. It encrypts all data sent between the client and server, making it a secure way to access remote resources. To use SSH, you'll need to have an SSH client installed on your local machine and an SSH server running on the remote machine.
To access a remote machine using SSH, you'll use the following command:
ssh username@remote_host
Replace "username" with the username you want to use to connect to the remote machine, and "remote_host" with the hostname or IP address of the remote machine. You'll then be prompted to enter the password for the specified user. Once you've successfully logged in, you'll have access to the remote machine's command line interface.
SCP, on the other hand, is a tool used for secure file transfer between a local and remote machine over an SSH connection. It allows you to securely copy files and directories between machines, without the need for additional software or services.
To transfer a file from your local machine to a remote machine using SCP, you would use the following command:
scp /path/to/local/file username@remote_host:/path/to/remote/directory
Replace "/path/to/local/file" with the path to the file you want to transfer, "username" with the username you want to use to connect to the remote machine, "remote_host" with the hostname or IP address of the remote machine, and "/path/to/remote/directory" with the path to the directory on the remote machine where you want to copy the file. You'll then be prompted to enter the password for the specified user.
Overall, SSH and SCP are powerful tools that allow you to securely access and transfer files between remote machines. By using these tools, you can safely manage and transfer files over unsecured networks, without having to worry about the security of your data.
Please like this blog if it was able to add value to your knowledge.
I would appreciate your feedback, as it is valuable to me in improving my blog content.
I would love to connect with you on LinkedIn: Abhinav Pathak