Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

For example, I do

cd Music
dir

and get

123456789.mp3
qweerkrtrkgljdjfkdjfdkf.mp3
a.mp3
b.mp3
blabla.mp3

how do I delete, say, files qweerkrtrkgljdjfkdjfdkf.mp3 and blabla.mp3 with least effort?

UPD: Key idea is that file names can be long so I actually dont want to type them.

share|improve this question

7 Answers 7

up vote 4 down vote accepted

Try this:

rm -f 2.mp3 blabla.mp3

rm removes files, and -f forces it to (so that it wont stop, asking you if you want to delete the file). If this not in your home directory, prepend sudo. Here is another way that might require less typing (a bit harder to read though)

rm -f {2,blabla}.mp3

This expands to 2.mp3 blabla.mp3. If you want to use larger filenames, you can use the wildcard character (*), which will return all items starting/ending with the filename you chose. For example:

rm -f bla*

will remove blabla.mp3. If you used:

rm -f *.mp3

It will remove all files ending with mp3. If you used this:

rm -f bla*.mp3

It will remove all files starting with blah and ending with .mp3. Possibilities are nearly endless with the * character :P

share|improve this answer
1  
I knew someone would just get in. Stupid HTC Keyboard! –  saleemrashid1 Nov 14 '12 at 19:29
    
lol, it's always a race in AskUbuntu :P –  MiJyn Nov 14 '12 at 19:30
2  
If the files are owned by you in your own home folder, you don't need to use sudo. –  user76204 Nov 14 '12 at 19:35
    
Good point. I updated my post. –  MiJyn Nov 14 '12 at 19:39
1  
Perhaps note in addition the ability to use the tab character to complete filenames? –  Darael Jan 20 at 15:01

Try this: rm qweer*.mp3 bla*.mp3

Caution: If there is a file name which is started with these letters,this command will detete that.

share|improve this answer

As @Hckr already mentioned there no similarities between the name. You can use wildcards, e.g. rm *.mp3 would remove all files whose names end with .mp3. If there are no such similarities you need to specify every file indivually.

You can in fact save some typing by using tab completion. In your example, if you type rm q and press the tabulator key it will be completed to rm qweerkrtrkgljdjfkdjfdkf.mp3. This works because its the only file starting with q. If you'd type rm b this would not be enough for completion because you have two files starting with b.

share|improve this answer

Easy, rm 2.mp3 blabla.mp3.

CAUTION: This will cause permanent deletion!


For files:

1234.MP3 1345.MP3 1234.MP4

rm -f 1*3*.MP3 would delete the first 2, no confirm, PERMANENTLY!

* is anything even blank that is why it still deletes the second one.

share|improve this answer
1  
Maybe -f after rm –  saleemrashid1 Nov 14 '12 at 19:28
    
If there is one file that begins with q then type q*, if there are more it will delete all q-begginers. –  saleemrashid1 Nov 14 '12 at 19:32
    
You know you can edit your own answer to include this extra information. –  user76204 Nov 14 '12 at 19:33
1  
Instead of commenting, you should use the edit button to add these great additions to your answer. –  Marco Ceppi Nov 14 '12 at 19:33
    
Done! Next time I'll use the laptop. –  saleemrashid1 Nov 14 '12 at 19:40

Just as everyone says, rm -f <file> is the way to go, however, as stonedsquirrel said, you can type the first few letters and hit <TAB> and it will autofill the file name.

share|improve this answer

What I would do is put the files you didn't want to delete in a folder before running these commands if there were less of them than you wanted to get rid of. But ultimately a combination of Wildcards ("*" and "?") and defining statements would get the job done easily. Just depends on what files you have that you want to keep, or get rid of.

Ever tried dragging the files to delete into terminal app window? It may clone the directory as well as the file-name path style for you. However, if you are only deleting 2 or 3 files I hope it is simply for learning purposes otherwise I would type the paths and file-name in manually using the "TAB" on my keyboard it should auto-complete file-names given in the current directory that start with what you already typed.

share|improve this answer

Tab completion is the way. It's enabled for bash in Ubuntu by default. It works like this:

$ rm bl<tab>

will automatically be changed to

$ rm blabla.mp3

If the file name is ambiguous, you need to press tab once more to see a list of possible file names:

$ rm b<tab><tab>
blabla.mp3  b.mp3

This also works for a number of other commands, such as apt-get, service and so on.

If you're interested in an even more sophisticated tab completion, check out zsh with the Grml config, it's mind-blowing.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.