Thursday, 3 July 2014

How to Delete All Files In a Directory Except Few Files in LINUX /UNIX

First let’s go through the RM command in UNIX:
The rm command is used in Unix / Linux to remove a file or a directory. The syntax is as follows:
Syntax:
rm [OPTION] [FILE] 
OPTION’s include
> rm –f {file-name} – To remove files forcefully without prompting to the user.
> rm –i {file-name} – To remove files by prompting to the user for confirming.
> rm –r {file-name} – To remove files in directories recursively.


Now we will see how to delete All Files in Directory except Few files>
1) USING RM COMMAND:
To remove all files from directory /home/opt/class/ except .log and .zip
> rm /home/opt/class/ !(*.zip|*.log)

To remove all files except a specific file (file123)
> rm  !(file123)


2) USING FIND COMMAND:
Find command is used to search the files in a particular directory.
To remove all files from directory /home/opt/class/ except .log
> find /home/opt/class/  -iname "*.log" –delete
You can use ‘-name’ command for case sensitive search and ‘-iname’ for search  not considering case.

Some other applications of using rm command;
To delete all files in the current directory;
> rm /home/opt/class/ *   (Use interactive mode ‘i’ to be on safer side)

To delete files in a directory that has any of the enclosed characters. In this example characters to be checked are A, B OR C
> rm *[ABC]*


2 comments:

  1. Good stuff, but why the contents cannot be copy to save? Is that design like that in purpose to keep copyright?

    ReplyDelete
    Replies
    1. Hi,.I think you can copy it.Please try again :)

      Delete

Related Posts Plugin for WordPress, Blogger...

ShareThis