36

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?

6 Answers 6

11

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.

1
  • In my case, I had to .. up 3 levels to get the replaced directories. So the solution here won't always work. Surprisingly, cd . does. And the cd pwd even makes sense.
    – gbarry
    Feb 26, 2021 at 22:05
72

There is even a shorter way: cd .

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

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

cd `pwd`

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

3

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

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.

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.

2
  • In Unix refresh is out-of-box.
    – kenorb
    Mar 21, 2015 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. Mar 21, 2015 at 18:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .