4

I have some files with really long names lying around on my server and writing the complete names is kind of a pita so I'm asking if my Linux system (Debian 7) can guess the complete filename? I want to write something like

rm some_file_1

instead of

rm_some_file_1_very_long_information_bla_bla_foo_bar_meow

There is no other file than some_file_1_very_long_information_bla_bla_foo_bar_meow whose name contains some_file_1

  • There is also tab-completion. Have you tried typing some_file_1 followed by tab? – jw013 Nov 27 '13 at 20:28
  • TAB works great, too. I think both (jw013's and Joseph R.'s) answers are good and simple ways to do it. I'll probably stick to the wildcard (*) method as my intent will be clearer. The TAB method is great because you will see the full filename (in case it was even found). – user53287 Nov 27 '13 at 20:48
3

That's what shell globs are for (among other things):

echo some_file_1*

Note that I'm using echo instead of rm deliberately. When you are satisfied your glob has matched exactly the file(s) you wish to remove, you can then replace echo with rm.

  • That's exactly what I have been looking for. – user53287 Nov 27 '13 at 20:03
0

The feature you're looking for is autocompletion. Type the first few characters of the file name on the shell command line and press Tab. The file name is automatically completed if the prefix that you typed is unambiguous.

If there is an ambiguity, the shell will at least complete up to the point where there are multiple possibilities. For example, if there are files called some_file_1, some_file_2 and some_fiddlesticks, and you type some_ then press Tab, then the characters fi are inserted (at least). Depending on your shell configuration, this may choose a file name to complete rather than merely inserting fi, and the behavior if you press Tab repeatedly also varies.

Autocompletion is available out of the box in bash, zsh, (t)csh and fish as well as some BusyBox variants. On most ksh installations, you need to press Esc Esc instead of Tab.

If your shell doesn't have completion, you can use wildcards: enter rm some_file_1* to delete all files whose name begins with some_file_1. Compared to the completion method, this has the disadvantage of being very unforgiving to typos. For example, if there's another file beginning with the same prefix, it will be deleted. If you accidentally put a space before the *, all files in the current directory will be deleted. You can put echo in front of the command to see what it'll do, then run the command again without echo to perform the operation. Alternatively, run rm -i some_file_1* to get prompted for every file name.

Your Answer

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