Open In App

Use the Lines of a File as Arguments of a Linux Command

Last Updated : 07 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Handling multiple arguments for a command can be difficult, especially when there are a lot of parameters. One way to make this easier is to put the arguments in a text file and use each line of the file as an argument for the command. This can simplify the process and save time.

For example, let’s say you have a text file with a list of 100 client names, and you want to create a folder for each client. Writing mkdir <client_name> for 100 clients is not a productive approach. Instead, What if we could pass the text file of client names as an argument to the mkdir command? This article will explore ways to do this.

There are three methods to operate. Let’s understand them one by one :

1. Using Shell input redirection

We have a list of clients, stored in a file named clients.txt and we want to create a folder for each client. So instead of giving the name of every client in the mkdir command, we’ll pass the file ‘clients.txt’ as an argument to the mkdir command using the redirection operator ‘<‘.

Syntax:

command `<FILE_NAME`

You can replace the command with the operation that you want to perform. And FILE_NAME with the name of the file you want to use as an argument.

Command:

mkdir `<clients.txt`

Output:

Creating a folder for each client using the shell redirection operator

Creating a folder for each client using the shell redirection operator

We can see after running the command mkdir `< clients.txt` 4 folders for each client Robin, Bob, Martin, and Jack are created. The < operator instructs the shell to read the contents of clients.txt and feed it as input to mkdir.

Note: We can rewrite the above command without backticks as mkdir $(< clients.txt) it will give same output.

Rewriting the command inside

Rewriting the command inside

Limitation of shell input redirection

The shell redirection operator ‘<‘ has a limitation, it does not care about the spaces or new lines, to be specific it doesn’t have any option to set a delimiter for the input.

In clients.txt we have full names of the clients like “Alison George”, “Zora Hood”, when we’ll execute the same command it will generate 4 separate folders named “Alison”, “George”, “Zora” and “Good”, but we want two folders for each client.

Limitation of shell redirection operator

Limitation of shell redirection operator

Xargs command overcomes this limitation of shell input redirection.

2. Using xargs command

Syntax:

xargs [options] command

The xargs command allows us to specify a delimiter on the redirected input using -d option. This capability is useful when you need precise control over how input data is separated and processed. Thus for creating folders with full names the command will be,

Command:

xargs -d '\n' mkdir < clients.txt

Using xargs Command

Explanation:

  • < clients.txt: It tells the shell to take the contents of the clients.txt file as the input for the xargs command.
  • xargs command: reads the contents of the clients.txt passed by ‘<‘ operator.
  • -d ‘\n’: tells xargs to use a newline character as the delimiter.
  • mkdir: For each line in the file, xargs invokes the mkdir command, passing the line as an argument to create a directory will full client name.

To learn more about xargs checkout xargs command in Linux.

3. Using read command and while loop

We can use a command called “read” to read one line at a time from a file we specify. When we read each line, we also consider any spaces or special characters by using something called “Internal Field Separator” (IFS).

Then, we can use a “while” loop to keep doing this for each line in the file, and as we read each line, we use the “mkdir” command to create a new folder with the name we just read. This way, we can create separate folders for each line in the file, ensuring that we handle each line correctly, even if it contains spaces or special characters.

Command:

while IFS= read -r client_name;
do
mkdir "$client_name"
done < clients.txt

Output:

Using Read Command and While Loop

Explanation:

Let’s understand this line by line,

while IFS= read -r client_name;

  • while marks the starting of a while loop in the shell script.
  • IFS= We are setting IFS to empty string.
    IFS (Internal Field Separator) is a special variable used to separate items within a line of text. By default, IFS is set to whitespace characters. When we set IFS to an empty string, it means that each line from the clients.txt file should be treated as a single, complete unit. This way we ensure that spaces or special characters in a line won’t be accidentally removed. It keeps everything in a line intact.
  • read -r client_name: The read command is used to read a line from the file and store it in the client_name variable.
    Using -r option we are like telling the computer, “Please read the name exactly as it is, without changing anything.” or treat it as a raw string. If you don’t use the “-r” option and your names or text contain special characters or backslashes ‘\’, the computer might interpret those characters differently and may give unexpected results or errors.

do: This word marks the beginning of our action in a while loop. Whatever we put after “do” is what we do for each line we read from the file.

mkdir “$client_name”: We are creating folder for each client name stored in the variable client_name.

done < clients.txt

  • done: This word tells us we’re finished with our action for one line. After we’ve created the folder, we’re ready to do the same for the next line.
  • < clients.txt: Here we are using redirection operator to redirect file as an input to the read command. It’s like saying, “Hey, read these lines from the ‘clients.txt’ file.”

Conclusion

In this article we discussed how to deal with lots of command parameters, a practical way is to store them in a text file and use each line as a command argument. This saves time and effort. For example, if you have a text file with 100 client names, you can create a folder for each client without manually typing each command. The article explores three methods to achieve this: shell input redirection, xargs command, and read command with a while loop. Each method simplifies using file lines as command arguments, making command-line tasks more efficient.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads