Little Girl's Mostly Linux Blog

DeleteFiles&Directories

Delete files and directories

This page was last updated on July 31, 2011.

COMMAND WHAT IT DOES
rm -i filename
Delete files (with confirmation)
rm filename
Delete files (no confirmation)
rmdir directoryname
Delete empty directory
rm -ir filename(s)
Delete files, directories and subdirectories recursively (with confirmation)
rm -r filename(s)
Delete files, directories and subdirectories recursively (no confirmation)
rm -rf /*
Delete all files and directories in the operating system (no confirmation)
Run from the root directory using sudo.

 

Delete files by age

The find command allows you to use the -exec argument to execute a command on any files that are found. If you combine find and rm, you have a rather powerful method of removing files.

  1. For safety’s sake, find all files fitting a certain age criteria first, and look them over to make sure those are really the files you want to delete:
    find /path/to/files/* -mtime N

    Replace N in this command with a number preceded by either a plus (+) or a minus () to indicate whether you want files older than or younger than a certain number of days.

  2. Delete the files.
    find /path/to/files/* -mtime N -exec rm {} \;

    Replace N in this command with a number preceded by either a plus (+) or a minus () to indicate whether you want files older than or younger than a certain number of days.

    Examples

    • To delete all files that are more than 5 days old:
    • find /path/to/files/* -mtime +5 -exec rm {} \;
    • To delete all files that were changed today:
    • find /path/to/files/* -mtime -1 -exec rm {} \;

Delete specific directories recursively from this directory (no confirmation)

This command will delete all sub-directories by the specified name in the current directory and all its sub-directories recursively with no confirmation.

Replace NAMEOFDIRECTORY in this command with the directory name to delete:

rm -rf `find . -type d -name NAMEOFDIRECTORY`

For example, those of you who use the Subversion version control system know that every directory and its sub-directories gets a .svn directory inside it that contains the information Subversion needs to do its thing. If you had a copy of a Subversion project directory and wanted to get rid of all the .svn directories inside it and all of its subdirectories, you would type this in a terminal window while inside the project directory:

rm -rf `find . -type d -name .svn`


Obligatory Happy Ending

And they all lived happily ever after. The end.

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Comment:

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: