Hello, FOSSLinux readers! Hope you’re all having a splendid day. I’ve recently been thinking about some of my favorite commands in the Linux universe. If you ask me what my top 5 favorite commands are, the ‘for’ loop would definitely be one of them. But here’s a confession, it’s also one of those commands that used to terrify me. I mean, let’s face it, it’s not exactly the most intuitive command, is it? Today, we’re going to tackle this beast together.
The ‘For’ command – The Linux workhorse
Before we get to the nitty-gritty, let’s remind ourselves what the ‘for’ command is. If you’ve ever worked with any programming language, chances are you’ve met the ‘for’ loop. This command is Linux’s way of letting us execute a series of commands for a set number of times. Think of it as a diligent little worker, tirelessly performing tasks for you, one iteration at a time.
Basic syntax of ‘For’ command
To start with, let’s look at the basic syntax of the ‘for’ command in Linux:
for VARIABLE in PARAMETER_1 PARAMETER_2 … PARAMETER_N do COMMAND1 COMMAND2 COMMAND_N done
In this command, VARIABLE is the name of the variable that changes its value in every iteration. PARAMETER values are the list of values that VARIABLE can take. COMMAND is the set of commands that are executed for each parameter.
Our first ‘For’ command – Let’s be brave!
I still remember my first time using the ‘for’ command. I was a little apprehensive (okay, a lot apprehensive), but let’s try this together:
for i in 1 2 3 4 5 do echo "Hello FOSSLinux, iteration number $i" done
In this simple ‘for’ command, we have a variable i that takes the values 1, 2, 3, 4, 5 in each iteration. The echo command prints a sentence with the iteration number in it. The result would be five lines of greetings to FOSSLinux with a different iteration number on each line.
‘For’ command with range
I must admit, I was not a big fan of manually entering all the numbers for the iteration. It felt like a waste of time. Then I discovered the range option! Let’s try that out:
for i in {1..5} do echo "Hello FOSSLinux, iteration number $i" done
It does the same thing as the previous example, but this time, we’ve used a range {1..5} instead of manually entering the numbers. Isn’t it much cleaner?
‘For’ command with step value
I do like the ‘for’ command, but sometimes, I need to skip some iterations. That’s where the step value comes in. Let’s see this in action:
for i in {0..10..2} do echo "Hello FOSSLinux, iteration number $i" done
Here we’re printing every even number between 0 and 10. The ..2 is our step value. This made my life so much easier and less cluttered!
‘For’ command with files and directories
I’m not going to lie, I love playing around with files and directories using the ‘for’ command. It gives me a sense of power! Here’s an example:
for file in /home/user/Documents/* do echo $file done
This command lists all the files in the specified directory. The wildcard ‘*’ matches all files and directories.
‘For’ command in batch mode
I can’t complete this post without talking about batch mode. Batch mode allows us to run ‘for’ loops in the background or schedule them for later. Here’s how you can do it:
echo 'for i in {1..5}; do echo "Hello FOSSLinux, iteration number $i"; done' | at now + 1 minute
This command will run our ‘for’ loop in one minute. You might be thinking, “Well, that’s odd! Why would I want to run a loop in the future?” Imagine having a resource-intensive task. You could schedule it to run overnight using this feature.
Practical example showing ‘For’command usage
Let’s assume you have a directory filled with text files and you need to rename all of them to have a “.bak” extension. Doing this manually would be quite a chore if you have a lot of files. But with the ‘for’ loop, you can do this task in one command. Here’s how:
for file in /home/user/documents/*.txt do mv "$file" "${file%.txt}.bak" done
In this command, we’re going through every “.txt” file in the /home/user/documents/ directory. The mv command is used to rename the file. We use ${file%.txt}.bak to change the extension of the file from “.txt” to “.bak”.
The ${file%.txt} is a form of parameter expansion which removes the shortest match of .txt from the end of the $file variable.
Here’s the output:
# Before running the script /home/user/documents/file1.txt /home/user/documents/file2.txt /home/user/documents/file3.txt # After running the script /home/user/documents/file1.bak /home/user/documents/file2.bak /home/user/documents/file3.bak
And voila! All your text files are now backup files. You see, the ‘for’ command isn’t just a versatile tool, it’s a time-saver, and it’s little tasks like these that make it one of my favorite commands in Linux. I hope you find it as useful as I do!
Closing thoughts
If you asked me what I love about the ‘for’ command, it’s the versatility. Whether it’s dealing with numbers or files, the ‘for’ command has got you covered. The batch mode? Simply genius.
However, no relationship is perfect. The ‘for’ command can be a bit intimidating for beginners, and its syntax is not the most intuitive. But like with most things in life, the more you use it, the more you understand it.
Remember, it’s all about practice. Give these examples a try, tweak them, break them, and fix them again. That’s the beauty of learning. Happy coding, everyone!