Not able to put ls -lR command in background

Hi All,

i am trying to put ls -lRt command to run in the background but it fails.
i continue to get output on screen and didnt get the command prompt right after i put the command in background.
command i am using is

 
ls -lRt &

i am using bash.

Can someone let me know how to resolve this.

And where do you want to fetch the data?

You might like/use:

ls -lRt > ~/fetched-ls.txt &

That will redirect all output (of stdout) to the file $HOME/fetched-ls.txt, and doing so in background.
Just be sure the process is done before using the outputfile.

Hope this helps

thanks 'sea'...is it possible to put the output of command (stdout) in the background without redirecting.
in a nutshell after i fire command ls -lRt & , command prompt should return so that i can execute another command. and once the execution of the ls -lRt is over in the background it should display the stdout on the screen.

AFAIK:
You can either redirect the output somewhere else or not.
But you cannot 'buffer' it, unless you use a file, variable or reduce the output by using pipes.

In case of a variable you could try: DATA=$(ls -1Rt) & then continue your work, and when its done, type: echo "$DATA"

Otherwise, please explain what you are trying to achieve.

hth

thanks sea...i am new to this unix world..so trying to learn the actual difference between the foreground and background process.

Now i have clear idea about it..thanks for the information and knowledge:)

That will not work, since the variable will get set in a subshell and therefore will not retain its value once it returns..

You could use a named pipe:

mkfifo somepipe
ls -lR > somepipe &
echo hello
cat < somepipe
rm somepipe

Or in bash / ksh93

exec 3< <(ls -lR)
echo hello
cat <&3

In ksh93 you can use a co-process

ls -lR |&
echo hello
while IFS= read -rp line
do
  printf "%s\n" "$line"
done

In bash 4 also:

coproc ls -lR
echo hello
cat <&${COPROC[0]}

thanks Scrutinizer :slight_smile: