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 four 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 Linux file system. In this article, we will discuss four useful tips on mkdir command in Linux.

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 -p option followed by directory path.

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 following command −

mkdir -p my_folder/docs my_folder/images my_folder/videos

The -p option ensures that if directory "my_folder" does not exist, it will be created along with subdirectories.

Creating Directories with Different Permissions

To create directories with different permissions, you can use chmod command along with mkdir command. chmod command is used to change permissions of files and directories in Linux. To use it with mkdir command, you can use 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 777. To do this, you can use following command −

mkdir my_folder && chmod 777 my_folder

This will create a directory called "my_folder" with permissions 777, which means that anyone can read, write, and execute files inside directory.

Creating Directories with Timestamps

To create directories with timestamps, you can use date command along with mkdir command. date command is used to display current date and time in Linux. To use it with mkdir command, you can use following syntax −

mkdir folder_name_$(date +format)

For example, let's say you want to create a directory called "my_folder" with a timestamp in its name. To do this, you can use following command −

mkdir my_folder_$(date +%Y-%m-%d_%H:%M:%S)

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 -p option followed by directory path. For example, let's say you want to create a directory called "my_folder" with a subdirectory called "docs" inside it. To do this, you can use following command −

mkdir -p my_folder/docs

This will create a directory called "my_folder" with a subdirectory called "docs" inside it. -p option ensures that if directory "my_folder" does not exist, it will be created along with subdirectory "docs".

“tar” Command

The tar command is used to compress and archive files in Linux. It is a very powerful tool that can be used for various tasks, such as creating backups, extracting archives, and compressing files. In this article, we will discuss 4 useful tips on tar commands in Linux.

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 following command −

tar -cvf archive.tar file1 file2 file3

In above command, "c" stands for create, "v" stands for verbose, and "f" stands for file. "f" option is used to specify filename of archive. archive filename should end with .tar extension.

For example, to create an archive of all files in a directory, you can use following command −

tar -cvf backup.tar /home/user/documents/

Extracting a tar Archive

The tar command can also be used to extract contents of an archive. To extract an archive, use following command −

tar -xvf archive.tar

In above command, "x" stands for extract. "v" option is used to display progress of extraction process, and "f" option is used to specify filename of archive.

For example, to extract contents of a "backup.tar" archive, you can use following command −

tar -xvf backup.tar

Compressing a tar Archive

The tar command can also be used to compress an archive. compression reduces size of archive and makes it easier to transfer or store. tar command supports various compression algorithms, such as gzip, bzip2, and xz. To compress an archive using gzip, use following command −

tar -czvf archive.tar.gz file1 file2 file3

In above command, "z" stands for gzip compression. ".tar.gz" extension is used to indicate that archive is compressed using gzip.

For example, to compress an archive of all files in a directory using gzip, you can use following command −

tar -czvf backup.tar.gz /home/user/documents/

Extracting a Compressed tar Archive

To extract a compressed tar archive, use following command −

tar -xzvf archive.tar.gz

In above command, "x" stands for extract, "z" stands for gzip compression, and "v" stands for verbose. ".tar.gz" extension is used to indicate that archive is compressed using gzip.

For example, to extract contents of a "backup.tar.gz" archive, you can use following command −

tar -xzvf backup.tar.gz

“kill” Command

It's a powerful tool that allows you to terminate running processes on your system. However, using kill command can be a bit tricky if you're not familiar with its syntax and options. In this article, we'll cover four useful tips on using kill command in Linux.

Understanding Basics

Before we dive into different ways you can use kill command, it's essential to understand basics. 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 ps command to list all running processes on your system, along with their corresponding PIDs.

The basic syntax of kill command is as follows −

kill [signal or option] PID

By default, when you run kill command without specifying a signal, it sends TERM signal to process. This signal requests process to terminate gracefully, allowing it to clean up after itself. If process doesn't respond to TERM signal, you can use other signals, such as KILL or HUP, to forcefully terminate it.

Sending Signals

As mentioned earlier, you can send different signals to a process using kill command. Here are some of most commonly used signals −

  • TERM − This signal requests process to terminate gracefully.

  • KILL − This signal forcefully terminates process without allowing it to clean up after itself.

  • HUP − This signal is often used to restart a process.

  • INT − This signal is sent when you press Ctrl+C in terminal. It requests process to terminate gracefully.

To send a signal to a process, you need to specify signal name or number using -s option. For example, to send TERM signal to a process with PID 1234, you can run following command −

kill -s TERM 1234

Alternatively, you can use signal number instead of signal name. For example, to send KILL signal to a process with PID 5678, you can run 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 kill command. For example, to kill processes with PIDs 1234, 5678, and 9012, you can run following command −

kill 1234 5678 9012

Alternatively, you can use a combination of kill command and pgrep command to kill all processes that match a specific pattern. For example, to kill all processes that contain word "firefox" in their name, you can run following command −

kill $(pgrep firefox)

Killing Processes by Name

Sometimes, you may not know PID of process you want to kill. In this case, you can use pkill command to kill processes by name. pkill command sends specified signal to all processes that match a given pattern.

For example, to kill all processes that contain word "firefox" in their name, you can run following command −

pkill firefox

Conclusion

The mkdir, tar, and kill commands are essential tools for any Linux user. With these 4 tips, you should be able to use them more effectively and efficiently. Remember to use caution when stopping processes and always make sure you are targeting correct process before using kill or killall commands. With practice and experience, you'll become a pro at using these commands in no time!

Updated on: 31-Mar-2023

201 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements