GabryRome
GabryRome

Reputation: 95

Bash print informations on the same point

In bash how can I print informations on the same point ???

For example how can I do if I want to loop over all files in all subdirectories of an hypothetical root and I want to print on the terminal all the informations always on the same location without printing new lines ??

DIR example

--> root
   --> Dir 1
     --> File 1
     --> File 2
   --> Dir 2
     --> File 3
     --> File 4

The shell output must be a thing like this :

Infos before that I don't want to clear

-------------------------------
Directory    : dirName
File count   : x
File         : fileName
-------------------------------

"Count" will remain the same in accord to the current dir but the other informations must be changed in accord to the current subdir (element) of the loop

thanks in advance

Upvotes: 0

Views: 98

Answers (1)

Michael Jaros
Michael Jaros

Reputation: 4681

Consider the following example:

for file in *
do
  printf "\r%${COLUMNS}s\r%s" "" "$file"
  sleep 1
done
echo

This prints all files in the current directory on the same line, sleeping for one second after each filename.

In this example, printf is used to print an empty argument with a field width that corresponds to the columns of the terminal. This is to clear any previous output still in the line before printing each new $file.

Upvotes: 1

Related Questions