Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to iterate over a list of files with spaces in Linux?
In order to iterate over a list of files we will need to make use of the find command that Linux provides us with. When dealing with filenames containing spaces, special handling is required to avoid issues with word splitting.
Linux find statement is one of the most widely used commands that allows us to walk a file hierarchy. It is used to find specific files or directories and we can append different flags and options to enhance functionality or perform complex operations.
Basic Find Command Example
Let's start with a simple example of the find statement:
find sample.sh
sample.sh
If the find command locates the file, it prints the filename. If not found, no output is returned and the terminal process terminates normally.
The Problem with Spaces in Filenames
Consider a directory dir1 with the following files:
immukul@192 dir1% ls -ltr total 5232 -rwxrwxrwx 1 immukul staff 446966 Sep 23 1998 wget-1.5.3.tar.gz drwxrwxrwx 3 immukul staff 96 Jul 7 17:42 d1 -rwxrwxrwx 1 root staff 106 Jul 8 13:10 sample2.sh -rwxrwxrwx 1 root staff 946 Jul 12 18:45 sample.sh -rwxrwxrwx 1 root staff 718 Jul 12 18:48 sample1.sh drwxr-xr-x 2 immukul staff 64 Jul 13 11:36 dr1 drwxr-xr-x 3 immukul staff 96 Jul 13 11:36 dr2 -rw-r--r-- 1 immukul staff 661 Jul 14 09:00 newZip.zip -rw-r--r-- 1 immukul staff 2201512 Jul 14 09:19 zipContent.zip drwxrwxrwx 4 immukul staff 128 Jul 14 09:34 d2 -rwxrwxrwx 1 immukul staff 24 Jul 14 15:52 sss.sh -rw-r--r-- 1 immukul staff 122 Jul 14 16:10 somefile.txt -rw-r--r-- 1 immukul staff 0 Jul 15 09:44 sample 1.txt
Notice the file sample 1.txt contains a space in its name. When iterating over files with spaces, we need special techniques to handle them properly.
Method 1: Using find with while read
The most reliable method to iterate over files with spaces uses find with a while read loop:
find . -iname "s*" | while read file
do
echo "$file"
done
./sample.sh ./somefile.txt ./sample2.sh ./sample 1.txt ./sss.sh ./sample1.sh
The file with a space in its name (sample 1.txt) is correctly handled and printed.
Method 2: Using find with -print0 and while IFS
For maximum reliability, especially with unusual characters, use null-terminated output:
find . -iname "s*" -print0 | while IFS= read -r -d '' file
do
echo "Processing: $file"
done
Method 3: Using find with -exec
Another approach is to use find with the -exec option:
find . -iname "s*" -exec echo "Found: {}" \;
Comparison of Methods
| Method | Handles Spaces | Handles Special Characters | Performance |
|---|---|---|---|
| while read | Yes | Limited | Good |
| -print0 with IFS | Yes | Excellent | Good |
| -exec | Yes | Excellent | Slower |
Key Points
Always quote variables when dealing with filenames:
"$file"Use
-print0andread -d ''for files with unusual charactersThe
-execmethod is safest but slower for large file setsAvoid simple loops like
for file in $(find ...)as they break on spaces
Conclusion
When iterating over files with spaces in Linux, use find with while read or the -print0 method for reliable results. Always quote variables and consider using null-terminated output for maximum compatibility with unusual filenames.
