5

I have a directory where I have the following set of files:

cheatsheetold.aux
cheatsheetold.log
cheatsheetold.out
cheatsheetold.pdf
cheatsheetold.synctex.gz
cheatsheetold.tex
cheatsheetnew.aux
cheatsheetnew.log
cheatsheetnew.out
cheatsheetnew.pdf
cheatsheetnew.synctex.gz
cheatsheetnew.tex

What I want to do is to remove all the cheetsheetold.

cheatsheetold.aux
cheatsheetold.log
cheatsheetold.out
cheatsheetold.pdf
cheatsheetold.synctex.gz
cheatsheetold.tex

without touching the cheatsheetnew

cheatsheetnew.aux
cheatsheetnew.log
cheatsheetnew.out
cheatsheetnew.pdf
cheatsheetnew.synctex.gz
cheatsheetnew.tex

I already had a look at this other question, but it didn't solve the problem as there is a complete set of extension for both file names

ecjb
  • 379
  • 3
  • 12
  • which shell? in bash it works to run `rm -- cheatsheetold.*` – fraleone Feb 09 '20 at 14:25
  • Great! Many thanks @fraleone. that worked! I think I'm on bash. but I'm not shure. How can I know for next time? – ecjb Feb 09 '20 at 14:29
  • `echo $BASH` should print out something when you are on bash as should `echo $SHELL` in both cases might most likely be `/bin/bash` which is the quasi standard path to the bash shell (for instance on fedora and ubuntu) – fraleone Feb 09 '20 at 14:33
  • many thanks @fraleone – ecjb Feb 09 '20 at 14:35

3 Answers3

5

Unix does not have file-name-extensions, nor does Microsoft's Windows (not after Windows ME. However file-explorer still has the concept). What you need to do is find all files starting with ... (in your case starting with cheatsheetold..

You can do this with cheatsheetold.*. It will then pass the file list to rm. You can use it with any command. It is not an rm thing.

Practice with echo cheatsheetold.*

ctrl-alt-delor
  • 27,473
  • 9
  • 58
  • 102
  • thanks for your answer @ctrl-alt-delor – ecjb Feb 09 '20 at 14:45
  • 1
    what is the difference between `rm -- cheatsheetold.*` and `rm cheatsheetold.*`? – ecjb Feb 09 '20 at 14:46
  • 1
    That would be a new question: but it will tell you for free, just type `man rm`. It means end of options, and as on option in a lot of commands. Anything after it even if it starts with a `-` or `--` is not an option. It this case it will be a filename. – ctrl-alt-delor Feb 09 '20 at 18:34
  • thanks for your comment @ctrl-alt-delor – ecjb Feb 09 '20 at 18:58
  • @ecjb There is a urban legend/ story where somebody had a file named `--rf` in his home directory. Wanting to delete all files, he issued a `rm *` instead of `rm -- *` which the shell then expanded to something like `rm file1 file2 [......] --rf` meaning that even more files and subdirectories have been removed by "acccident" and unintentedly. Therefore there is no harm to use `--` for additional safety line in case of `*`. – fraleone Feb 10 '20 at 08:56
  • It is `-rf` (one hython). – ctrl-alt-delor Feb 10 '20 at 21:47
2

It was not stated in which shell (bash, fish, zsh, csh) you desire to use some sort of wildcard when removing files.

As bash is a prominent shell I state that it would be possible to simply use this command:

rm -- cheatsheetold.*

note that the wildcard * is used after the -- as to avoid any unindented mishaps by bash shell extensions

the command line env | grep 'SHELL' might show you the shell you use. I have also tested the command line in the more POSIX reduced dash shell.

I simply do not use any other shells (like csh and tsh or fish) so I cannot tell how globbing works there.

another way to get an idea which shell is running might be this ps -p $$ where the $$ should be the PID of the current process (the shell) and ps the tool to list processes limited to the PID of the current shell.

https://help.ubuntu.com/community/ShellGlobbing

fraleone
  • 737
  • 2
  • 9
  • 21
  • 1
    Many thanks for your answer @fraleone. Can you just tell me additionally, how to know which shell I used for the next time I ask a question about terminal? – ecjb Feb 09 '20 at 14:32
1

You can use the wildcard, but you got to be very careful as it can delete other files that may match.

rm cheatsheetold.*

The command above will delete any file whose name starts with cheatsheetold. and is followed by anything, so any extension.

Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
Heysus Escobar
  • 439
  • 3
  • 6