How do I refresh directory in BASH?

25

10

I have a directory, containing files generated by compiler. During each rebuild this directory gets cleaned and, after build process ls gives me empty output. After I cd out of directory and then back to it ls works right. The questions are:

  1. Is there any other, more elegant way to refresh directory?
  2. What actually happens when files deleted and recreated? Why do I need to cd to see actual directory content?

Roman

Posted 2014-01-16T07:34:34.240

Reputation: 353

Answers

10

Your script is most likely removing the directory, and not just the files which are there. So, when you have cd'd into it, and the directory is removed, you do ls on a directory which does not actually exist.

By cd .. and cd (directory) you move up and back into the (newly created) directory, and the files are there as you expect.

The best alternative is instead of cding in and out of the directory, is to add the directory name to your ls command, and run it from the parent directory. So, ls YourSubdirectory instead of just ls.

Kent

Posted 2014-01-16T07:34:34.240

Reputation: 1 354

47

There is even a shorter way: cd .

wjr

Posted 2014-01-16T07:34:34.240

Reputation: 471

3Interesting. Like the hummingbird, this, logically, should not work -- and yet it appears that it does. I presume that you know that it works because you've tried it. Do you have any documentation for it? I.e., if I were on the POSIX/bash development team, I would be tempted to consider this a bug. Do you know of any documentation that says that it's supposed to work this way? – G-Man Says 'Reinstate Monica' – 2015-03-21T18:27:29.133

3Like the falcon, this, beautifully, floats above the flowers -- and yet it devours hummingbirds. – Matt Faus – 2015-09-24T19:24:31.623

Yes! so perfect – chrismarx – 2017-03-04T15:39:36.933

1This is the better answer. – deddebme – 2017-03-10T19:14:36.827

Ah. I knew it! There had to be a way. Thanks! – andersoyvind – 2017-11-14T13:42:25.523

10

It looks odd, but it is short and refreshes the directory:

cd `pwd`

Note those are back ticks, not single quotes around the pwd

user339088

Posted 2014-01-16T07:34:34.240

Reputation: 101

2

What @Kent said or: don't remove the directory but only its contents, this way you can stay in the directory (without "cd .. && cd -") and "ls" does what you expected it to do.

ckujau

Posted 2014-01-16T07:34:34.240

Reputation: 515

2

It would be better if you did not remove the directory, but instead of cd ..; cd - you could do cd $PWD.

Teddy

Posted 2014-01-16T07:34:34.240

Reputation: 5 504

2

create an alias refresh="cd .. && cd -" that will go a directory up and then again cd into the previous directory

if you are sure that you will need a listing again you can have

alias refresh="cd .. && cd - && ls -lrt"

once the ailas is create store the alias into your ~/.bashrc file by doing

alias | grep refresh >> ~/.bashrc

so that it is available next time you login.

SK176H

Posted 2014-01-16T07:34:34.240

Reputation: 121

In Unix refresh is out-of-box. – kenorb – 2015-03-21T16:16:09.910

What do you mean by "out-of-box"? When I saw it, I interpreted it to mean "standard"; now I'm wondering what you mean. Define "UNIX". Cygwin does not have a "refresh" command standard (OK, Cygwin is missing a lot of things that are standard in POSIX/*nix/whatever), and neither does my Linux system. – G-Man Says 'Reinstate Monica' – 2015-03-21T18:15:39.060