I wanted to delete some package in my home file, but the filename was too long (google-chrome-stable_current_i386.deb). So, I decided to use the command ls|grep chrome|rm to pipe the files to grep to filter out the chrome file, and then remove it. It didn’t work, so I would like to see how I can do this.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
This almost made me wince.
- You might want to stop pointing that shotgun at your foot. Basically any kind of parsing of
lsis going to be more complicated and error-prone than established methods likefind [...] -execor globs. - Unless someone installed a troll distro for you, your shell has Tab completion. Just type
rm googleand press Tab. If it doesn’t complete immediately, press Tab again to see a list of matching files. Type more characters of the filename to narrow it down until it does complete, then run the command. - Pipes != parameters. Standard input is a binary data stream which can be fed to a command asynchronously. Parameters are space separated strings which are passed once and only once to a command when running it. These are very rarely interchangeable.
Method 2
You had the right idea, just missed some details. Since you’re dealing with a list coming to STDIN and rm expects parameters, you need to use xargs.
Thus:
ls | grep chrome | xargs rm
Should give you what you want.
Note that if you want to delete everything other than the chrome file, you can simply add -v to the grep statement.
Note that, per the other answers to this question, this is probably a bad way of accomplishing what you want to accomplish.
Method 3
You can also use the find command with a wildcard:
find . -maxdepth 1 -name '*chrome*' -delete
Note that the “-maxdepth” argument ensures that find only works in the current directory, and doesn’t recurse into subdirectories.
Method 4
Never parse the output of ls
My suggestion is to avoid to parse the output of ls [1], even more if in conjunction with the del command. This for many reasons mainly related to unexpected and not usual characters allowed in the file name.
Even when you should expect that the filenames belonging to Linux packages will “behave well”, this problem can nonetheless appears if other files are present in the same directory but you didn’t know or notice.
It’s better to use find, the tab expansion (start to write the name and press Tab), the file name expansion [2] as *MyKey*…
A fast solution
Since you want to select all the packages (that finish with .deb) with “google” inside you can build your request with the wildcard * *google*.deb and do a simple
rm -i *google*.deb
that will select each filename with “google” in the middle that will finish for .deb present in the current directory. The option -i (interactive) will prompt for the confirmation, a good habit when you delete files with the parameter expansion.
A solution close to the philosophy of your attempt
If your purpose is to build your commandline piece after piece, so you have done ls, after ls | grep google, and only after you checked your output you can execute it in a subshell $(...) with
rm -i $(ls | grep google)
A faster and more dangerous way [3], is to use !!
ls | grep google rm -i $(!!)
that will execute the last command finished in your history. You can protect yourself from the fact that you have no visual control of the line that you are going to execute if you have enabled in advance the shell options histverify with shopt -s histverify.
Method 5
touch 0 1 2 3 4 5 6 7 8 9
find . -name [0-9] -ok rm {} ;
< rm ... ./0 > ? y < rm ... ./9 > ? y < rm ... ./8 > ? y < rm ... ./7 > ? y < rm ... ./6 > ? y < rm ... ./5 > ? y < rm ... ./4 > ? y < rm ... ./3 > ? y < rm ... ./2 > ? y < rm ... ./1 > ? y ^C
…use -name '*c*.deb' or some other pattern as suits you instead.
Method 6
rm doesn’t accept input from stdin. You’ll need to do something like ls google-chrome* | xargs rm
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0