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?

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.

| improve this answer | |

There is even a shorter way: cd .

| improve this answer | |
  • 3
    Interesting. 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' Mar 21 '15 at 18:27
  • 3
    Like the falcon, this, beautifully, floats above the flowers -- and yet it devours hummingbirds. – Matt Faus Sep 24 '15 at 19:24
  • Yes! so perfect – chrismarx Mar 4 '17 at 15:39
  • 1
    This is the better answer. – deddebme Mar 10 '17 at 19:14
  • Ah. I knew it! There had to be a way. Thanks! – andersoyvind Nov 14 '17 at 13:42

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

cd `pwd`

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

| improve this answer | |

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.

| improve this answer | |

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

| improve this answer | |

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.

| improve this answer | |
  • In Unix refresh is out-of-box. – kenorb Mar 21 '15 at 16:16
  • 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' Mar 21 '15 at 18:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.