0
0
Linux CLIscripting~20 mins

find with -exec for actions in Linux CLI - Mini Project: Build & Apply

Choose your learning style9 modes available
Using find with -exec for Actions
📖 Scenario: You are managing files in a folder on your Linux system. You want to find certain files and perform actions on them automatically.
🎯 Goal: Learn how to use the find command with the -exec option to search for files and run commands on each found file.
📋 What You'll Learn
Use the find command to locate files
Use the -exec option to run commands on found files
Understand how to use placeholders like {} in -exec
Print the results of the commands run on the files
💡 Why This Matters
🌍 Real World
System administrators often need to find files and run commands on them automatically, like cleaning up logs or checking file contents.
💼 Career
Knowing how to use find with -exec is a key skill for Linux system management and automation tasks.
Progress0 / 4 steps
1
Create a test directory with files
Create a directory called test_dir and inside it create three empty files named file1.txt, file2.log, and file3.txt.
Linux CLI
Need a hint?

Use mkdir to make a directory and touch to create empty files.

2
Set up a command to count lines
Create a variable called count_cmd and set it to the command wc -l which counts lines in a file.
Linux CLI
Need a hint?

Assign the string 'wc -l' to the variable count_cmd.

3
Use find with -exec to count lines in .txt files
Use the find command to search in test_dir for files ending with .txt and use -exec to run the command stored in count_cmd on each file. Use {} as a placeholder for the filename and end the -exec command with \;.
Linux CLI
Need a hint?

Use find test_dir -name '*.txt' -exec $count_cmd {} \; to run the command on each .txt file.

4
Display the output of the find command
Run the find command with -exec as before and print its output to see the line counts for each .txt file.
Linux CLI
Need a hint?

The output should show zero lines for each .txt file since they are empty.