6

I have a question about the * character at the end of a directory path in a bash script.

I have a script that's supposed to automatically delete some archives from a server once they get old enough. The script is on machine A and I need to run it on machine B. I access both machines remotely through ssh (no sudo, just regular user). One of the rules of the script is that it needs to only delete archives in the folders beginning with dirA/dirB/dirC/dirD/dirE*.

However, there is no dirE in that location so I'm guessing the * stands for some variable. This is what I'd like to know, what does the * mean at the end of the directory path and what does it make the script do?

9

The * here is a "globbing character" and means "match 0 or more characters". To illustrate, consider this directory:

$ ls
dirA  dire  dirE  dirEa  dirEEE
$ echo dirE*
dirE dirEa dirEEE

As you can see above, the glob dirE* matches dirE, dirEa and dirEEE but not dirA or dire (*nix systems are case sensitive).

So, in your script, that means it will delete archives from any directory in dirA/dirB/dirC/dirD/ whose name begins with dirE.

  • hey, not sure if i need to make another thread but i have another question on the same script i thought i'd ask here. so that script that's on machine A has the location of the archives that need to be checked defined as "dirA/dirB/dirC/dirD". However, i need to run the script on machine B with the dirE* condition as well. I suppose i need to edit the script to add this dirE* condition. i – kabras Oct 18 '16 at 8:20
  • 4
    @kabras yes, please ask a new question. If you need help in editing a script, make sure you include the script in the question. I also suggest you read a simple shell scripting tutorial first to get at least a basic idea of how the language works. – terdon Oct 18 '16 at 8:25
6

I'll just add a note here for those that come to this Q&A for another reason.

If you see a * at the end of a filename in the output of ls (actually of ls -F, but ls is sometimes aliased to ls -F (or the ls-F builtin in tcsh), or of zsh or tcsh completions, that's something completely different.

With the -F option, ls adds a trailing character at the end of some special file name to help identify their specialness. zsh and tcsh do the same when listing file name completions.

If you see:

$ ls -F
dir/  fifo|  file  link@  ls*  socket=

Those /, |, * and = are not part of the file name (though they might be if someone tried to trick you), but are appended by ls to tell you that:

  • dir is a directory (/)
  • fifo is a named pipe/fifo (|)
  • link is a symbolic link (@)
  • ls is an executable regular file (*) (has at least one execution bit in its permissions)
  • socket is a Unix domain socket (=)

Some ls implementations (and zsh's completion) can also do that differentiation via colours for terminals that support them with different options.

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.