If you run a command on a file, is there a way to reference the file without typing its full name?

Interesting question!
That has multiple replies, depending on the context in which you type that name (command line, shell script, program,…), to which command you want to pass that file, and also what exactly you mean by “full name”.

First of all, each file in Unix/Linux has a (unique) “absolute pathname” that always starts with a ” / “.If you mean “full name” that absolute pathname, then there is a simple way of not having to type it.

An example: Suppose my home directory /home/peter is. And that the file /home/peter/test/voorbeeld.txt exists (so in a subdirectory of my home directory).If my current directory is the same as my home directory (e.g. immediately after logging in), then I can delete this file by one of the following two commands:


In The second case, I did not have to type the full name.

It can of course be a little shorter, after I cd test have completed the instruction:

RM example. txt\n

But is it also possible to voorbeeld.txt not have to type those “” in full, e.g. to delete that file?

Well, that also depends on the shell context: interactive (with e.g. bash) you can also type the following:

RM Test/voo \ n

, and then press the TAB key.If no other file in the subdirectory test begins with the letters voo , the additional keystroke will suddenly have the following:

RM Test/example. txt \ n

And so you only have to press ENTER (or not of course;-)

Actually, you have called the shell for help, which quickly asks (behind the scenes) a file list of the file system, discovers that only one file begins with it voo , and thus the rest of the “typing” takes its account.

Besides, it is not necessary to type the first three letters: one is sufficient, or even none, if there is no other (non-hidden) file in that directory.

There is a second way of calling the shell for help, a way that can be used both interactively and also in e.g. a shell script:
Use matchsoundslike in file names.
So one of the following instructions will re-delete (hopefully only that one) file:

RM Test/VOO * \NRM Test/* \NRM */VOO * \NRM T */v * \NRM */* \n

Finally, it also depends on the desired instruction.So can e.g rm . “” also recursively delete files.Supposing the subdirectory test contains three files, and you want to delete all of them, and then that directory itself as well. The “full written” instruction would then be:

RM test/Sample. txt test/. Still. A. Sample test/. ABC \ Nrmdir test\n

But (because rm it is more flexible than that) it can be much shorter:

RM-R test\n

Some other instructions are likewise (partially or completely) recursively.For instance, a “full listing” of all files in SubDir can be tested with

LS-lA test\n

While you should have been typing something in the following sentence:

LS-l test/Sample. txt test/. Still. A. Sample test/. ABC \ n

Other instructions will not be recursive.And you can only save typing by using one of the previous three techniques mentioned (en: First cd test run, or use the TAB key, or work with MatchSoundsLike).

For example: To see the contents of all three files on the screen, cat all three need the names on its commandline:

Cat test/Sample. txt test/. Still. A. Sample test/. ABC \ n

Of course also something like

Cat Test/V * Test/. [na * \n

Because the shell will now first interpret the matchsoundslike and will replace the commandline (in a way that is invisible to us, but visible to it cat ) by the full written version of the net (but then the two last files in the “wrong” Order shown…)

Also e.g. the following will work (now again in the good order):

CD Test\ncat v *. n *. A * \n

or by pressing the TAB key instead of typing a star.

Leave a Reply