bash - For loop using find lacks doesn't properly handle directory names having white space character -


OK I'm really stuck on this.

I have dirs.txt that is as follows:

  / var / tmp / old files.txt / var / tmp / old backups.bak  < / Pre> 

The dirs.txt file is generated by the script

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

  ` Dirs.txt` for dir; Command throws: ls: can not use / var / tmp / old: no such file or directory  

I want

Full filenames can be used by LS-L'Aid But it does try LS-L / var / tmp / old /

how can I fix it with a loop?

  cat dirs.txt | While reading DIR; When you use a bactic operator, the output is divided into tokens on the character of each white spot. I ls -al "$ dir" done  
  1. The second On the side, read , reads one line instead of one word at a time. So the solution, which looks odd, is in the loop in the loop so that it can be read by line.

  2. You need the quote "$ Dir" in the ls command to the full filename, the white location and all If you do not do this, then ls two filenames, / var / tmp / old and files.txt < / Code>.

Maybe I'm going to honor you. You can cat :

  It can make it even easier by eliminating what LS-L "$ dir" did & lt; Dirs.txt  

This works because < Strong> Whole while the loop acts like a large command, so you can also use file redirection in the cat like a pipe.


Taking it even further ...

how dirs.txt is generated, you can get rid of it completely You can do everything in one command and if it is just a temporary file, you can reduce it by piping and make dirs.txt in . Whereas loop directly, leaving a temporary file. For example, replace

  / var -name '* old *' & gt; Dirs.txt while reading dir; Did LS-L "$ dir" & lt; Dirs.txt  

with

  find / var -name '* old *' | While reading DIR;  lin-l "$ dir" done  

excuse find whatever order you make to create a file list.

And in fact, if you are actually using search then you might find a large order in the order without any end Or can do anything! For example, with search and while can be done with a single search command:

  Find / var-name '* Old *' -exec ls -al {} \;  

search is actually a flexible command that can detect files that match all kinds of complex criteria, and those files are called command line logic You can pass by using the other commands as -exec option when you use -exec then {} each file name Gets replaced with.


Comments

Popular posts from this blog

c++ - Linux and clipboard -

Visual Studio 2005: How to speed up builds when a VSMDI is open? -

booting ubuntu from usb using virtualbox -