For a loop that uses drawbacks of find, directory names that have a white space are not properly processed

Well, I'm really stuck with that.

I have a dirs.txt that looks like this:

/var/tmp/old files.txt
/var/tmp/old backups.bak

The dirs.txt file is generated by the script itself.

When I want to use dirs.txt in a for loop, for example:

for dir in `cat dirs.txt` ; do 
    ls -al $dir
done

This is what the command throws: ls: cannot access / var / tmp / old: no such file or directory

I want all the file names to be ls -al'ed, but it is trying to use ls -al / var / tmp / old

How can I fix this from a for loop?

+5
source share
2 answers
cat dirs.txt | while read dir; do
    ls -al "$dir"
done
  • backtick, . read, , , . , , , , , .

  • "$dir" ls, , . , ls , /var/tmp/old files.txt.

, , Cat. , cat:

while read dir; do
    ls -al "$dir"
done < dirs.txt

, while , , , cat , .


...

, dirs.txt, . , , dirs.txt while, . ,

find /var -name '*old*' > dirs.txt
while read dir; do
    ls -al "$dir"
done < dirs.txt

find /var -name '*old*' | while read dir; do
    ls -al "$dir"
done

Pretend find - , .

, find, , , find - - ! , find while find, :

find /var -name '*old*' -exec ls -al {} \;

find - , , , -exec. -exec, {} .

+12

, . , , "", . "ls" "/bin/ls", .

, , , (,/var/tmp/old/files.txt).

, "ls -al", . , , , , - "ls -al/var/tmp/old" ( dirs.txt), , , , files.txt backups.bak.

2 , , :)

0

All Articles