Files Processing
Learn how to process files using for and while loops.
We'll cover the following
Files processing
The for
loop works well when we need to process a list of files. The only point here is to compose the loop condition correctly. There are several common mistakes when writing this condition. Let’s consider them by examples.
The first example is a script that reads the current directory and prints the types of all files there. We can call the file
utility to get this information for each file.
When composing the for
loop condition, the most common mistake is to neglect patterns (known as “globbing”). Users often call the ls
or find
utility to get the STRING
, like this:
for filename in $(ls)
for filename in $(find . -type f)
However, both of these for
conditions are wrong. They lead to the following problems:
- Word splitting breaks the names of files and directories with spaces.
- If the filename contains an asterisk, Bash performs globbing before it starts the loop. Then, it writes the globbing result to the
filename
variable. This way, we lose the actual filename. - The output of the
ls
utility depends on the regional settings. Therefore, we can get question marks instead of the national alphabet characters in filenames. In that case, thefor
loop cannot process the files.
We must always use patterns in the for
condition when we need to enumerate filenames. It is the only correct solution for this task.
We should write the following for
loop condition for our script:
for filename in *
The for-file.sh
shows the complete script.
Get hands-on with 1400+ tech skills courses.