0

I have to work with path contain space character in Linux, My command below are works fine for non-space path, but it will fail if path contain space.

#!/bin/bash
for i in $( find "." -maxdepth 1 -mindepth 1 -type d ); do
    b=$(echo "$i" | awk '{print substr($1,3); }' )
    echo "This file contain <Modified date> <ActualSize Byte>   <FullPath>" > "$i"/"$b".txt
    find "$i" -type f ! -iname '*thumbs.db*' -print0 | xargs -0 stat -c "%y %s %n" >> "$i"/"$b".txt
done

This is my folders list.

.
./Folder 1
./Folder 2
./Folder 3

The error of script is below.

./xyz.sh: line 4: ./Folder/Folder.txt: No such file or directory
find: ./Folder: No such file or directory
./xyz.sh: line 5: ./Folder/Folder.txt: No such file or directory
./xyz.sh: line 4: 1/.txt: No such file or directory
find: 1: No such file or directory
./xyz.sh: line 5: 1/.txt: No such file or directory
./xyz.sh: line 4: ./Folder/Folder.txt: No such file or directory
find: ./Folder: No such file or directory
./xyz.sh: line 5: ./Folder/Folder.txt: No such file or directory
./xyz.sh: line 4: 2/.txt: No such file or directory
find: 2: No such file or directory
./xyz.sh: line 5: 2/.txt: No such file or directory
./xyz.sh: line 4: ./Folder/Folder.txt: No such file or directory
find: ./Folder: No such file or directory
./xyz.sh: line 5: ./Folder/Folder.txt: No such file or directory
./xyz.sh: line 4: 3/.txt: No such file or directory
find: 3: No such file or directory
./xyz.sh: line 5: 3/.txt: No such file or directory
NNOPP
  • 107
  • 11

2 Answers2

5

Do not loop over find command as for i in $(find . -type f) syntax, see Bash Pit-falls Use find with -print0, to preserve special characters on folder/file names on a while-loop as below:-

See what the man page of find says about the -print0 option:-

   -print0
          True; print the full file name on the standard output, followed by
          a null character (instead of the newline character that -print
          uses).  This allows file names  that  contain newlines or other
          types of white space to be correctly interpreted by programs that
          process the find output.  This option corresponds to the -0 option 
          of xargs.

Use the same in a script as follows.

#!/bin/bash

while IFS= read -r -d '' folder; do

    # Your script goes here

done < <(find . -maxdepth 1 -mindepth 1 -type d -print0)
Inian
  • 80,270
  • 14
  • 142
  • 161
  • Dear Inian, i'm now using `while IFS= read -r -d '' folder; do echo $folder find "$folder" -type f > "$folder"/File.txt done < <(find . -maxdepth 2 -mindepth 2 -type d -print0)` Now i can only output as "File.txt". I want to use the depth 2 as file name of the text file, is it possible ? – NNOPP Aug 04 '16 at 16:18
  • @NoppornPhantawee: Please post it as a separate question. Thanks! – Inian Aug 04 '16 at 19:24
  • Thanks Inian for the reply, i posted new tread here >> http://stackoverflow.com/questions/38780041/how-to-use-the-depth-level-2-or-maxdepth-as-output-text-file-for-my-bash-script – NNOPP Aug 05 '16 at 02:08
0

Just change IFS to non-space (found there: How to loop through file names returned by find? ):

IFS="\r\n"

Alsol Lame (but works) just adding a sed filter/unfilter on space chars

for ix in $( find "." -maxdepth 1 -mindepth 1 -type d | sed "s/ /%SPC%/g"); do
    i=$(echo "$ix" | sed "s/%SPC%/ /g")
    b=$(echo "$i" | awk '{print substr($1,3); }' )
    echo "This file contain <Modified date> <ActualSize Byte>   <FullPath>" > "$i"/"$b".txt
    find "$i" -type f ! -iname '*thumbs.db*' -print0 | xargs -0 stat -c "%y %s %n" >> "$i"/"$b".txt
done
Community
  • 1
  • 1
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219