Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
4 Useful Tips on mkdir, tar and kill Commands in Linux
As a Linux user, you will likely encounter situations where you need to create directories, compress files, or terminate processes. These tasks can be accomplished using three essential commands: mkdir, tar, and kill. In this article, we will discuss useful tips that will help you master these commands and simplify your Linux experience.
mkdir Command
The mkdir command is one of most commonly used commands in Linux. It allows users to create directories and subdirectories in the Linux file system.
Creating Multiple Directories at Once
The mkdir command can be used to create multiple directories at once. This is useful when you need to create several directories with similar names. To create multiple directories at once, you can use the -p option followed by directory paths.
For example, let's say you want to create three directories named "docs", "images", and "videos" inside a directory called "my_folder". To do this, you can use the following command
mkdir -p my_folder/docs my_folder/images my_folder/videos
The -p option ensures that if the directory "my_folder" does not exist, it will be created along with the subdirectories.
Creating Directories with Different Permissions
To create directories with different permissions, you can use the chmod command along with the mkdir command. The chmod command is used to change permissions of files and directories in Linux. You can use the following syntax
mkdir folder_name && chmod permissions folder_name
For example, let's say you want to create a directory called "my_folder" with permissions 755. To do this, you can use the following command
mkdir my_folder && chmod 755 my_folder
This will create a directory called "my_folder" with permissions 755, which gives read, write, and execute permissions to the owner, and read and execute permissions to group and others.
Creating Directories with Timestamps
To create directories with timestamps, you can use the date command along with the mkdir command. The date command is used to display the current date and time in Linux. You can use the following syntax
mkdir folder_name_$(date +format)
For example, let's say you want to create a directory called "backup" with a timestamp in its name. To do this, you can use the following command
mkdir backup_$(date +%Y-%m-%d_%H-%M-%S)
This creates a directory with the current date and time in the format: backup_2024-01-15_14-30-45
Creating Nested Directories
The mkdir command can also be used to create nested directories. Nested directories are directories that are located inside other directories. This is useful when you need to organize files and directories in a hierarchical structure.
To create nested directories, you can use the -p option followed by the directory path. For example, let's say you want to create a nested structure like "project/src/main/java". To do this, you can use the following command
mkdir -p project/src/main/java
This will create the entire directory structure in one command. The -p option ensures that all parent directories are created if they don't exist.
tar Command
The tar command is used to compress and archive files in Linux. It is a powerful tool that can be used for various tasks, such as creating backups, extracting archives, and compressing files.
Creating a tar Archive
The tar command can be used to create an archive of one or more files. To create a tar archive, use the following command
tar -cvf archive.tar file1 file2 file3
In the above command, c stands for create, v stands for verbose, and f stands for file. The f option is used to specify the filename of the archive. The archive filename should end with the .tar extension.
For example, to create an archive of all files in a directory, you can use the following command
tar -cvf backup.tar /home/user/documents/
Extracting a tar Archive
The tar command can also be used to extract the contents of an archive. To extract an archive, use the following command
tar -xvf archive.tar
In the above command, x stands for extract. The v option is used to display the progress of the extraction process, and the f option is used to specify the filename of the archive.
For example, to extract the contents of a "backup.tar" archive, you can use the following command
tar -xvf backup.tar
Compressing a tar Archive
The tar command can also be used to compress an archive. Compression reduces the size of the archive and makes it easier to transfer or store. The tar command supports various compression algorithms, such as gzip, bzip2, and xz. To compress an archive using gzip, use the following command
tar -czvf archive.tar.gz file1 file2 file3
In the above command, z stands for gzip compression. The ".tar.gz" extension is used to indicate that the archive is compressed using gzip.
For example, to compress an archive of all files in a directory using gzip, you can use the following command
tar -czvf backup.tar.gz /home/user/documents/
Extracting a Compressed tar Archive
To extract a compressed tar archive, use the following command
tar -xzvf archive.tar.gz
In the above command, x stands for extract, z stands for gzip compression, and v stands for verbose. The ".tar.gz" extension indicates that the archive is compressed using gzip.
For example, to extract the contents of a "backup.tar.gz" archive, you can use the following command
tar -xzvf backup.tar.gz
kill Command
The kill command is a powerful tool that allows you to terminate running processes on your system. However, using the kill command can be tricky if you're not familiar with its syntax and options.
Understanding the Basics
Before we dive into different ways you can use the kill command, it's essential to understand the basics. The kill command sends a signal to a process, requesting it to terminate. Each process in Linux has a unique process ID (PID), which is used to identify it. You can use the ps command to list all running processes on your system, along with their corresponding PIDs.
The basic syntax of the kill command is as follows
kill [signal or option] PID
By default, when you run the kill command without specifying a signal, it sends the TERM signal to the process. This signal requests the process to terminate gracefully, allowing it to clean up after itself.
Sending Different Signals
You can send different signals to a process using the kill command. Here are some of the most commonly used signals
| Signal | Number | Description |
|---|---|---|
| TERM | 15 | Requests process to terminate gracefully |
| KILL | 9 | Forcefully terminates process immediately |
| HUP | 1 | Often used to restart a process |
| INT | 2 | Sent when pressing Ctrl+C in terminal |
To send a signal to a process, you need to specify the signal name or number using the -s option. For example, to send the TERM signal to a process with PID 1234, you can run the following command
kill -s TERM 1234
Alternatively, you can use the signal number instead of the signal name. For example, to send the KILL signal to a process with PID 5678, you can run the following command
kill -9 5678
Killing Multiple Processes
Sometimes, you may need to kill multiple processes at once. You can do this by specifying multiple PIDs after the kill command. For example, to kill processes with PIDs 1234, 5678, and 9012, you can run the following command
kill 1234 5678 9012
Alternatively, you can use a combination of the kill command and pgrep command to kill all processes that match a specific pattern. For example, to kill all processes that contain the word "firefox" in their name, you can run the following command
kill $(pgrep firefox)
Killing Processes by Name
Sometimes, you may not know the PID of the process you want to kill. In this case, you can use the pkill command to kill processes by name. The pkill command sends the specified signal to all processes that match a given pattern.
For example, to kill all processes that contain the word "firefox" in their name, you can run the following command
pkill firefox
You can also use killall to terminate processes by their exact command name
killall firefox
Conclusion
The mkdir, tar, and kill commands are essential tools for any Linux user. These tips will help you use them more effectively and efficiently. Remember to use caution when stopping processes and always make sure you are targeting the correct process before using kill or pkill commands.
