EzDevInfo.com

rm interview questions

Top rm frequently asked interview questions

ls | grep | rm -- How to format this command?

I'm trying to delete a bunch of files in a certain directory (on Mac OS X using Terminal)

ls | grep \([1-9]\) | xargs rm

should do the trick, but it doesn't. I'm trying to delete any file with a single digit in parentheses in the filename (duplicates downloaded from the web), but it ends up doing something like this:

> rm: 520syllabus2010: No such file or
> directory rm: (3).pdf: No such file or
> directory

because it doesn't interpret the space correct. It should delete "520syllabus2010 (3).pdf"

What's the proper way of doing this?

Thanks, Jeff


Source: (StackOverflow)

Removing files matching a numeric range regular expression

I have a folder which has many files. Some of the data files are labelled 1, 2, 3, ..., 300

I want to remove these data files using the terminal. How do I do this?

I want to do something like

rm some-regular-expression-giving-numbers-from-0--300

Source: (StackOverflow)

Advertisements

How to delete all files in a directory except some?

I need to delete all files in a directory, but exclude some of them. For example, in a directory with the files a b c ... z, I need to delete all except for u and p. Is there an easy way to do this?


Source: (StackOverflow)

Undo an "rm -rf ~" command?

I had a folder I didn't want named '~', so from the command line I typed

rm -rf ~

and accidentally deleted my home folder (since ~ resolved to /home/username). Is there any way back or do I basically need to recreate the account from scratch?


Source: (StackOverflow)

Prevent user typing accidental space between rm and wildcard

Intention:

rm -rf string*

Problem:

rm -rf string *

The first case is a legitimate and common use of rm, a small typo can cause a lot of problems in the second case. Is there a simple way to smartly protect against an accidental trailing or leading wildcard?


Source: (StackOverflow)

Mac -- remove all files with a certain extension from a directory tree [duplicate]

Possible Duplicate:
Delete files recursively matching a name on the command line (OS X)

I want to remove all files with extension .orig from my tree. The tree is deep. Is there an easy way to do that?

I will probably have to do this many times a day, with different trees. So ease is important.


Source: (StackOverflow)

Deleting millions of files

I had a dir fill up with millions of gif images. Too many for rm command.

I have been trying the find command like this:

find . -name "*.gif" -print0 | xargs -0 rm

Problem is, it bogs down my machine really bad, and causes time outs for customers since it's a server.

Is there any way that is quicker to delete all these files...without locking up the machine?


Source: (StackOverflow)

How to remove a file with unprintable charaters in Mac OS X Terminal [duplicate]

Possible Duplicate:
Removing file with strange characters in filename in OS X

I have a folder that has a file who's name is Icons and ends with an additiona unprintable character. I can see the unprintable if I ls with -B option:

$ ls -B
$ Icon\015

if I use ls -b I get:

$ ls -b
$ Icon\r

I would like to delete file but I cannot find a way to type the unprintable character. If I do:

$ rm Icon\015

I get:

$ Icon\015: No such file or directory

If I do:

$ rm Icon\r

I get:

$ Icon\r: No such file or directory

I could just delete the whole folder, but I need a more generic way to do this since such files pop up on many users folders.


Source: (StackOverflow)

Trying to delete directory with "rm -rf", but get message that it's not empty

I've tried deleting a directory using "rm -rf" and I'm getting the message "Directory not empty":

Bens-MacBook-Pro:please benjaminhocking$ ls -lart empty_directory/
total 16
drwxr-xr-x  5 benjaminhocking  staff  170 Aug 27 14:46 .
drwxr-xr-x  3 benjaminhocking  staff  102 Aug 27 15:28 ..
Bens-MacBook-Pro:please benjaminhocking$ rm -rf empty_directory/
rm: empty_directory/: Directory not empty
Bens-MacBook-Pro:please benjaminhocking$ rmdir empty_directory/
rmdir: empty_directory/: Directory not empty

If I try the same thing using Finder (dragging the folder to the Trash), I get the message

The operation can’t be completed because the item “empty_directory” is in use.

I've tried doing xattr -d com.apple.quarantine, purely out of superstition, but it did no good.

A probably important piece of context is that this directory was initially in a directory that should've been deleted by a "make clean" command I issued prior to Terminal locking up on me, after which a little over half of the other programs I had running also locked up, including Skype, and eventually the OS itself. I ended up having to reboot the computer by pressing and holding the power key.

Edit to add: Another important piece of information I left off was that this was happening in an encrypted folder à la encfs. I was able to track down the corresponding folder in the encrypted side of things and delete it there. I still don't know why I couldn't do it from the decrypted side of things like I normally do. I'll leave this unanswered for now in case anyone has a good answer for that.


Source: (StackOverflow)

Best practices to alias the rm command and make it safer

Some time ago I erroneously deleted my home folder because I ran a rm -rf * on the wrong terminal, whose working directory was the home folder!

I wish I had an alias for the rm command, but it was not the case.

Now, I am planning to make a script as an alias for rm.

Do you have any best practice to suggest?

Thanks.


Source: (StackOverflow)

`alias rm="rm -i"` considered harmful?

I have read some time ago (can't find the reference) that using such an alias as alias rm="rm -i" was very bad.

Is there historical evidence or common sense explanation for that fact?

I would imagine that it gives a user a bad habit of relying on the confirmation prompt to check his command, which could lead to disasters if he does so on another profile that doesn't have the alias.


Source: (StackOverflow)

Delete files with regular expression

I Tried to delete files that starts with A and ends with 2 numbers but It doesn't do a thing.
What I tried:

rm ^A*[0..9]2$

Where am I wrong?


Source: (StackOverflow)

use rm to remove files and directories recursively

is it possible to use rm to remove files and directories matching a pattern recursively without using other commands?


Source: (StackOverflow)

How can I remove a file or directory called "\"?

I'm using terminal on a Ubuntu machine and there is a file that I would like to delete. The file's name is \ (just a backslash).

Now usually I would just do

rm filename

However if I do rm \ then it thinks I'm trying to write a multi-line command.

How can I delete this file? I know that I could just use the GUI file system, but that's not very efficient.

So, how can I delete (in terminal) a file called \?


Source: (StackOverflow)

Linux – cannot remove owned file with 777 permissions

I've couple of files that I cannot remove using rf -Rf command. I'm the owner of those file and the group assigned to those files is also a group my user is in. What's even weirder is that I can edit their content and I can change the permissions for them, but I can't move or delete them.

ls -al
total 12
dr-xr-xr-x 3 rayell pg1083760 4096 2010-10-10 10:00 .
drwxr-xr-x 3 rayell pg1083760 4096 2011-09-02 04:33 ..
-rwxrwxrwx 1 rayell pg1083760    0 2011-09-02 06:38 default.settings.php
drwxrwxrwx 2 rayell pg1083760 4096 2011-09-02 04:33 files
-rwxrwxrwx 1 rayell pg1083760    0 2011-09-02 06:38 settings.php


rm -Rf *
rm: cannot remove `default.settings.php': Permission denied
rm: cannot remove directory `files': Permission denied
rm: cannot remove `settings.php': Permission denied

Can anyone tell me what is happening?


Source: (StackOverflow)