Skip to content
View in the app

A better way to browse. Learn more.

Unraid

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Help with the 'find' command

Featured Replies

I'm working on a shell script.  One of the things it's supposed to do is traverse a directory tree looking for files whose name is unknown:

 

	
cd /some/dir/
find . -type f -name '*' -print | while read i 	
do
   if grep $SOMETHING $i > /dev/null
   then
   echo "Do something with file:" $i
fi
done

 

The problem I'm having is that when 'find' stumbles on a file with spaces in its name it returns only the first word in the name (up to the first space).  Then, the subsequent grep trips up because it is trying to act on a filename that doesn't exist.

 

Does anyone know how to deal with finding files, as I am doing above, taking into account the possibility of filenames with spaces?

I'm working on a shell script.  One of the things it's supposed to do is traverse a directory tree looking for files whose name is unknown:

 

	
cd /some/dir/
find . -type f -name '*' -print | while read i 	
do
   if grep $SOMETHING $i > /dev/null
   then
   echo "Do something with file:" $i
fi
done

 

The problem I'm having is that when 'find' stumbles on a file with spaces in its name it returns only the first word in the name (up to the first space).  Then, the subsequent grep trips up because it is trying to act on a filename that doesn't exist.

 

Does anyone know how to deal with finding files, as I am doing above, taking into account the possibility of filenames with spaces?

 

My guess is that you are going to have to use regular expressions and search for things between two forward slashes to define the folder names into a variable that way.  Then you might need to tr " " "\ " the variable to be able to use them properly.

 

and watch your quoting.. it could cause bring up a whole set of problems on their own.

 

Cheers,

Matt

You need to use -print0 and xargs -0 or use -exec

 

 

I'm working on a shell script.  One of the things it's supposed to do is traverse a directory tree looking for files whose name is unknown:

 

	
cd /some/dir/
find . -type f -name '*' -print | while read i 	
do
   if grep $SOMETHING $i > /dev/null
   then
   echo "Do something with file:" $i
fi
done

 

The problem I'm having is that when 'find' stumbles on a file with spaces in its name it returns only the first word in the name (up to the first space).  Then, the subsequent grep trips up because it is trying to act on a filename that doesn't exist.

 

Does anyone know how to deal with finding files, as I am doing above, taking into account the possibility of filenames with spaces?

find . -type f -name '*' -print | while read i

 

Will print the filename and store all of it into i correctly because i is the only variable in the read command

 

however, because $i may contain spaces or special characters you need to double quote it here

 

>>    if grep $SOMETHING "$i" > /dev/null

 

and here

>>    echo "Do something with file:" "$i"

 

 

Also you may not need the -name "*" as the default i

 

you can also do something like

 

find . -type f -exec grep -l "$SOMETHING" {} \;  | while read i

do   "Do something with file " "$i"

done

 

The $i following the   read i   must be double quoted to be resolved by the shell as a whole string without being interpreted by the shell.

 

 

  • Author

Thank you, everyone.  The hardest part for me with shell scripting is when to use ``, or "", or '', or whatever else to ensure that my variables expand correctly.  I've survived by trial-&-error, but I'd love to find a good website (or even better a book) that explains it once and for all.  It's about as confusing & unclear to me as split-levels in unraid.

Thank you, everyone.  The hardest part for me with shell scripting is when to use ``, or "", or '', or whatever else to ensure that my variables expand correctly.  I've survived by trial-&-error, but I'd love to find a good website (or even better a book) that explains it once and for all.  It's about as confusing & unclear to me as split-levels in unraid.

 

Here are some of the links I used to learn/reference.

 

http://bash-hackers.org/wiki/doku.php/syntax/quoting

http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_03.html

http://www.mpi-inf.mpg.de/~uwe/lehre/unixffb/quoting-guide.html

 

Cheers,

Matt

Archived

This topic is now archived and is closed to further replies.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.