Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between using ls and find to loop over files in a bash script

Tags:

find

bash

unix

ls

I'm not sure I understand exactly why:

for f in `find . -name "strain_flame_00*.dat"`; do
   echo $f
   mybase=`basename $f .dat`
   echo $mybase
done

works and:

for f in `ls strain_flame_00*.dat`; do
   echo $f
   mybase=`basename $f .dat`
   echo $mybase
done

does not, i.e. the filename does not get stripped of the suffix. I think it's because what comes out of ls is formatted differently but I'm not sure. I even tried to put eval in front of ls...

like image 650
FrenchKheldar Avatar asked Dec 04 '22 01:12

FrenchKheldar


2 Answers

The correct way to iterate over filenames here would be

for f in strain_flame_00*.dat; do
   echo "$f"
   mybase=$(basename "$f" .dat)
   echo "$mybase"
done

Using for with a glob pattern, and then quoting all references to the filename is the safest way to use filenames that may have whitespace.

like image 71
glenn jackman Avatar answered Dec 27 '22 22:12

glenn jackman


First of all, never parse the output of the ls command.

If you MUST use ls and you DON'T know what ls alias is out there, then do this:

(
COLUMNS=
LANG=
NLSPATH=
GLOBIGNORE=
LS_COLORS=
TZ=
unset ls
for f in `ls -1 strain_flame_00*.dat`; do
  echo $f
  mybase=`basename $f .dat`
  echo $mybase
done
)

It is surrounded by parenthesis to protect existing environment, aliases and shell variables.

Various environment names were NUKED (as ls does look those up).

One unalias command (self-explanatory).

One unset command (again, protection against scrupulous over-lording 'ls' function).

Now, you can see why NOT to use the 'ls'.

like image 42
John Greene Avatar answered Dec 27 '22 23:12

John Greene