Categories
shellinfo tips

WGET with examples

The wget command is a free utility in the Linux command-line interface for non-interactive download of files from the web. It supports HTTP, HTTPS, and FTP protocols, and can retrieve files through HTTP proxies. In this blog post, we’ll delve into the wget command, providing a comprehensive understanding with practical examples.
Understanding the ‘wget’ Command

The wget command is primarily used to download files from the internet. The basic syntax of the wget command is as follows:

wget [option]... [URL]...

Downloading Files with ‘wget’

To download a file from a URL, you simply provide the URL as an argument:

wget https://example.com/file.txt

This command will download the file file.txt from https://example.com.
Downloading in the Background with ‘wget’

If you’re downloading a large file, you may want to run wget in the background. You can do this with the -b (background) option:

wget -b https://example.com/large-file.zip

This command will download large-file.zip in the background.
Limiting the Download Rate with ‘wget’

To prevent wget from using all available bandwidth, you can limit the download rate with the –limit-rate option:

wget --limit-rate=200k https://example.com/large-file.zip

This command will limit the download rate to 200 KB/s.
Downloading Multiple Files with ‘wget’

To download multiple files, you can provide multiple URLs:

wget https://example.com/file1.txt https://example.com/file2.txt

This command will download file1.txt and file2.txt.
Conclusion

The wget command is a powerful tool for downloading files in Linux. Whether you’re downloading a single file, running in the background, limiting the download rate, or downloading multiple files, wget provides a flexible way to download files directly from the command line. With the examples provided in this guide, you’re well on your way to mastering the wget command.

Categories
shellinfo tips

Using the Linux ‘curl’ Command

The curl command is a versatile tool in the Linux command-line interface, used for transferring data to or from a server. It supports a multitude of protocols, including HTTP, HTTPS, FTP, and more. In this blog post, we’ll delve into the curl command, providing a comprehensive understanding with practical examples.
Understanding the ‘curl’ Command

The curl command is primarily used to download or upload data. The basic syntax of the curl command is as follows:

curl [options] [URL...]

Downloading Data with ‘curl’

To download data from a URL, you simply provide the URL as an argument:

curl https://example.com

This command will send a GET request to https://example.com and print the response to the standard output.

If you want to save the response to a file, you can use the -o (output) option followed by the filename:

curl -o example.html https://example.com

This command will save the response to example.html.
Sending POST Requests with ‘curl’

The curl command can also send POST requests. To do this, you can use the -d (data) option followed by the data you want to send:

curl -d "param1=value1¶m2=value2" https://example.com

This command will send a POST request to https://example.com with the data param1=value1&param2=value2.
Sending Headers with ‘curl’

To send headers with your request, you can use the -H (header) option:

curl -H "Content-Type: application/json" -d '{"param1":"value1","param2":"value2"}' https://example.com

This command will send a POST request to https://example.com with a JSON payload and the Content-Type: application/json header.
Conclusion

The curl command is a powerful tool for data transfer in Linux. Whether you’re downloading a webpage, sending a POST request, or setting headers, curl provides a flexible way to interact with servers directly from the command line. With the examples provided in this guide, you’re well on your way to mastering the curl command.

Categories
shellinfo tips

LN – Using symbolic and hard links in Linux

The ln command is a fundamental tool in the Linux command-line interface, used for creating links between files. It supports creating two types of links – hard links and symbolic (or soft) links. In this blog post, we’ll delve into the ln command, providing a comprehensive understanding with practical examples.
The Basics of ‘ln’ Command

The ln command is primarily used to create links between files. The basic syntax of the ln command is as follows:

ln [OPTION]... [-T] TARGET LINK_NAME

Here, TARGET is the file you want to link to, and LINK_NAME is the name of the link.
Creating Hard Links

A hard link is essentially a mirror of the original file. When you create a hard link, you’re creating a new file that points to the same data as the original file. Here’s how you can create a hard link:

ln file.txt link_to_file.txt

In this example, link_to_file.txt is a hard link to file.txt. Any changes made to file.txt will be reflected in link_to_file.txt, and vice versa.
Creating Symbolic Links

A symbolic link, also known as a soft link, is a special kind of file that points to another file or directory. Unlike a hard link, a symbolic link can point to a file or directory on a different filesystem. You can create a symbolic link using the -s option:

ln -s file.txt symlink_to_file.txt

In this example, symlink_to_file.txt is a symbolic link to file.txt. If you delete file.txt, the symbolic link will still exist but will point to a file that no longer exists.
Overwriting Links

By default, the ln command will not overwrite existing files. If you want to overwrite an existing link, you can use the -f (force) option:

ln -sf file.txt symlink_to_file.txt

This command will create a symbolic link symlink_to_file.txt to file.txt, overwriting symlink_to_file.txt if it already exists.

Categories
shellinfo tips

The Linux ‘cut’ Command

The `cut` command is a powerful tool in the Linux command-line interface, used for cutting out sections from each line of files and writing the result to standard output. It can be used to cut parts of a line by byte position, character, and field. In this blog post, we’ll explore the `cut` command in detail, complete with practical examples.

Understanding the ‘cut’ Command

The `cut` command is primarily used to remove or “cut out” certain sections of each line in a file. It can be used with various options to specify the part of each line to remove.

The basic syntax of the `cut` command is as follows:

cut OPTION... [FILE]...

If no file is specified, `cut` reads from the standard input.

Cutting by Byte Position

The `-b` (bytes) option is used to cut by byte position. For example, the following command cuts out the first byte of each line in the file `file.txt`:

cut -b 1 file.txt

You can also specify a range of bytes. The following command cuts out the first through third bytes of each line:

cut -b 1-3 file.txt

Cutting by Character

The `-c` (characters) option is used to cut by character. This is similar to cutting by byte position, but it’s useful for multibyte characters. For example, the following command cuts out the first character of each line in the file `file.txt`:

cut -c 1 file.txt

Cutting by Field

The `-f` (fields) option is used to cut by field. A field is a unit of data separated by a special character, called the delimiter. By default, the delimiter is the tab character. For example, the following command cuts out the first field of each line in the file `file.txt`:

cut -f 1 file.txt

You can specify a different delimiter with the `-d` (delimiter) option. The following command cuts out the first field, with fields delimited by a comma:

cut -d ',' -f 1 file.txt

Conclusion

The `cut` command is a versatile tool for text processing in Linux. Whether you’re cutting by byte position, character, or field, `cut` offers a powerful way to manipulate text data directly from the command line. With the examples provided in this guide, you’re well on your way to mastering the `cut` command.

Categories
shellinfo tips

Mastering Basic Linux Commands: cd, pwd, ls, df, and du

Linux is a powerful operating system widely used in various environments, from personal desktops to servers and supercomputers. The power of Linux comes from its command-line interface, which allows users to perform complex tasks with just a few keystrokes. In this blog post, we will explore five basic but essential Linux commands: cd, pwd, ls, df, and du.
Navigating Directories with cd

The cd (change directory) command is used to navigate between directories in the Linux file system. It’s one of the most frequently used commands in Linux.

To change to a specific directory, you simply type cd followed by the path of the directory. For example:

cd /home/user/Documents

This command will change the current directory to /home/user/Documents.

To move up one directory level, you can use cd … To go back to the home directory, you can use cd ~ or simply cd without any arguments.
Displaying the Current Directory with pwd

The pwd (print working directory) command is used to display the current directory. It’s a handy command when you’ve navigated deep into the directory structure and want to know where you are.

Simply type pwd and press enter:

pwd

This command will print the full path of the current directory.

Exploring Directory Contents with ls

The ls (list) command is a cornerstone in the Linux command-line interface, used to display the contents of a directory. By default, executing ls with no arguments will list the contents of the current directory.

ls

However, you can also specify a different directory by providing its path as an argument:

ls /home/user/Documents

The true power of ls comes from its various options that allow you to customize the output. For instance, ls -l displays the output in a long listing format, providing detailed information such as file permissions, number of links, owner, group, size, and time of last modification.
ls -l

If you want to view hidden files (those starting with a dot), you can use the -a option:

ls -a

You can even combine options. For example, ls -la will display all files (including hidden ones) in long format.

ls -la

To sort files by modification time, you can use the -t option:

ls -lt

And if you want to view the contents of directories recursively, you can use the -R option:

ls -R

In essence, the ls command is a versatile tool that can be tailored to your specific needs, making directory exploration efficient and user-friendly.
Checking Disk Space with df

The df (disk filesystem) command is used to display the amount of disk space used and available on the filesystems.

df

By default, df displays the disk space in 1K blocks. To make it display the disk space in human-readable form (i.e., in bytes, kilobytes, megabytes, etc.), you can use the -h option:

df -h

Estimating File and Directory Space Usage with du

The du (disk usage) command is used to estimate the space usage of files and directories.

du

By default, du displays the disk usage of the current directory and its subdirectories. To display the disk usage of a specific directory, you can provide the directory path as an argument:

du /home/user/Documents

Like df, du also has a -h option to display the disk usage in human-readable form.
Conclusion

The cd, pwd, ls, df, and du commands are fundamental to navigating and managing the Linux file system. Mastering these commands is the first step towards becoming proficient in Linux. Remember, the key to learning Linux commands is practice. So, open your terminal and start exploring!

Categories
shellinfo tips

SORT – Sorting content of files

The ‘sort’ is a commonly used Linux command for sorting lines of text files.

Sorting a file in alphabetical order

sort filename.txt

This command will sort the contents of the ‘filename.txt’ file in alphabetical order and display it on the terminal.

Sorting a file in reverse alphabetical order

sort -r filename.txt

The ‘-r’ flag is used to reverse the order. So, this command will sort the contents of the ‘filename.txt’ file in reverse alphabetical order and display it on the terminal.

Sorting a file numerically

sort -n numbers.txt

If the file contains numbers, you can use the ‘-n’ flag to sort them numerically.

Sorting a file in reverse numerical order

sort -nr numbers.txt

This will sort the contents of the ‘numbers.txt’ file in reverse numerical order.

Sorting a file based on the second column

sort -k 2 filename.txt

If the file contains multiple columns or fields, you can sort based on a particular field by specifying the column number or character position using the ‘-k’ flag. In this example, the file will be sorted based on the second column.

Sorting a file based on a delimited field

sort -t , -k 2 filename.csv

If the file is comma-separated, you can specify the delimiter using the ‘-t’ flag. In this example, the file will be sorted based on the second column which is delimited by commas.

Sorting only unique lines from a file

sort -u filename.txt

You can use the ‘-u’ flag to sort only the unique lines from a file.

Sorting a file without considering case

sort -f filename.txt

The ‘-f’ flag can be used to sort a file without considering case.

Merging two sorted files

sort -m file1.txt file2.txt

You can merge two sorted files into one sorted file using the ‘-m’ flag.

Sorting files in a directory based on filename

ls | sort

You can sort the contents of a directory based on filename by piping the output of the ‘ls’ command to the ‘sort’ command.

Categories
shellinfo tips

AWK

The awk command in Linux is a powerful tool for processing text files, particularly those formatted as columns of data. It’s a scripting language that’s designed for text processing and is included by default in most Unix-like operating systems.

Here are some of the things you can do with awk:

  • Print Columns: The most basic use of awk is to print columns of data. For example, if you have a file called data.txt with the following content:
  • John 25 Engineer
    Jane 28 Doctor
  • You can print the first column (names) with the following command:
  • awk '{print $1}' data.txt

    Output:

    John
    Jane
  • Filter Rows: You can use awk to filter rows based on some condition. For example, to print only the rows where the second column (age) is greater than 26:
  • awk '$2 > 26' data.txt

    Output:

    Jane 28 Doctor
  • Perform Calculations: awk can perform calculations on the data. For example, to add 5 to the age of each person:
  • awk '{$2 = $2 + 5; print}' data.txt

    Output:

    John 30 Engineer
    Jane 33 Doctor
  • Text Substitution: You can use awk to substitute text. For example, to replace “Engineer” with “Software Engineer”:
  • awk '{gsub("Engineer","Software Engineer"); print}' data.txt

    Output:

    John 25 Software Engineer
    Jane 28 Doctor
  • Pattern Matching: awk can also perform pattern matching. For example, to print lines that contain “Doctor”:
  • awk '/Doctor/ {print}' data.txt

    Output:

    Jane 28 Doctor
  • Multiple Commands: You can use multiple commands in a single awk script. For example, to print the names of people who are not doctors:
  • awk '!/Doctor/ {print $1}' data.txt

    Output:

    John
  • Built-in Variables: awk has several built-in variables. For example, NF (number of fields) represents the number of columns. To print the last column of each row:
  • awk '{print $NF}' data.txt

    Output:

    Engineer
    Doctor
  • User-Defined Variables: You can define your own variables in awk. For example, to calculate the average age:
  • awk '{total += $2; count++} END {print total/count}' data.txt

    Output:

    26.5
  • Functions: awk supports several built-in functions. For example, length returns the length of a string. To print the length of each name:
  • awk '{print length($1)}' data.txt

    Output:

    4
    4
  • Passing Variables: You can pass variables to awk using the -v option. For example, to print rows where the age is greater than a certain value:
  • awk -v age=26 '$2 > age' data.txt

    Output:

    Jane 28 Doctor
  • File Processing: awk can process multiple files. For example, if you have another file data2.txt:
  • Alice 30 Lawyer
    Bob 35 Engineer

    You can print the names from both files:

    awk '{print $1}' data.txt data2.txt

    Output:

    John
    Jane
    Alice
    Bob
  • Complex Conditions: awk supports complex conditions. For example, to print rows where the name starts with ‘J’ and the age is less than 30:
  • awk '/^J/ && $2 < 30' data.txt

    Output:

    John 25 Engineer
    Jane 28 Doctor

    These examples should give you a good idea of the power and flexibility of awk. It’s a very versatile tool for text processing in Linux.

    Categories
    shellinfo tips

    MDADM – Managing RAID in Linux

    mdadm is a Linux utility used to manage and monitor software RAID devices. The name is derived from the term “multiple device administrator”. It is a powerful tool that can be used for a variety of tasks related to RAID arrays, such as creating, managing, and monitoring them.

    Here are some examples of what you can do with mdadm:

    Create a RAID array: You can use mdadm to create a new RAID array. Here’s an example of how you might create a RAID 5 array with three devices:

    mdadm --create --verbose /dev/md0 --level=5 --raid-devices=3 /dev/sda1 /dev/sdb1 /dev/sdc1

    In this example, /dev/md0 is the name of the new RAID device, –level=5 specifies that it should be a RAID 5 array, and –raid-devices=3 indicates that there should be three devices in the array. The devices /dev/sda1, /dev/sdb1, and /dev/sdc1 are the partitions that will be included in the array.

    Monitor a RAID array: mdadm can also be used to monitor the status of a RAID array. For example, you can use the following command to check the status of the /dev/md0 array:

    mdadm --detail /dev/md0

    This command will display detailed information about the array, such as its level, size, and the status of each device in the array.

    Add a new device to an existing RAID array: If you want to add a new device to an existing RAID array, you can use the –add option. For example, to add a new device /dev/sdd1 to the /dev/md0 array, you would use the following command:

    mdadm --manage /dev/md0 --add /dev/sdd1

    Remove a device from a RAID array: Similarly, you can remove a device from an array using the –remove option. For example, to remove the device /dev/sdd1 from the /dev/md0 array, you would use the following command:

    mdadm --manage /dev/md0 --remove /dev/sdd1

    Stop and delete a RAID array: If you no longer need a RAID array, you can stop it and then delete it using mdadm. Here’s how you might do that:

    mdadm --stop /dev/md0
    mdadm --remove /dev/md0

    These are just a few examples of what you can do with mdadm. It’s a very powerful tool with many more options and capabilities. Always make sure to check the man page (man mdadm) or other documentation for more information and to understand the implications of any command before you run it.