LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-21-2011, 08:04 AM   #1
moviecarpet
LQ Newbie
 
Registered: May 2011
Posts: 7

Rep: Reputation: Disabled
How to delete all files with specific word in filename?


I need a command to find the all files which filename contains the text "SomeText" and to delete that files!

From /home/movie/wp-content/uploads/

this folder I have lots of files and folders .


Also I need that for folders and subfolders who contains some text in folder name "someTextInFolderName"

Tnx in advance!
 
Old 05-21-2011, 08:09 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Hi and welcome to LinuxQuestions! What about the find command? It has features that match exactly what you're looking for: searching file names by pattern, recursion, ability to delete the items found. Take a look at man find or at http://www.gnu.org/software/findutils/manual/find.html. Check the -delete option (but be careful to use it only when you're absolutely sure of the search results). Feel free to post your attempts if in doubt.

Last edited by colucix; 05-21-2011 at 08:11 AM.
 
1 members found this post helpful.
Old 05-21-2011, 08:18 AM   #3
moviecarpet
LQ Newbie
 
Registered: May 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
Can this command be "transformed" to do delete instead of grep?

find /home/movie/wp-content/uploads/ -type f -exec grep -l "SomeText" {} \;
 
Old 05-21-2011, 08:22 AM   #4
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Hanover, Germany
Distribution: Main: Gentoo Others: What fits the task
Posts: 15,650
Blog Entries: 2

Rep: Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095
You should really have a look at the manpage for find, especially the options -name and -delete. No need for a grep the way you are doing it, you can do all that with the available options of find.
 
Old 05-21-2011, 08:23 AM   #5
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, Ubuntu, SLES, CentOS
Posts: 1,790

Rep: Reputation: 324Reputation: 324Reputation: 324Reputation: 324
@ Reply

Hi there,

Yes colucix is right you can use find command to get the listing of files and folders with a particular pattern. As he mentioned look at the man page of find command using "man find" to get the listing of switches that you can use with it. In your case you are looking for files with SomeText under /home/movie/wp-content/uploads/ so you can run the following command:

find /home/movie/wp-content/uploads/ -name 'SomeText*'

In which text mentioned between single quotes will be the patter you are looking for.

Once you are sure that you found the files & folders you can run the following command to delete them:

rm -r -f /home/movie/wp-content/uploads/SomeText*

Beware rm -r -f the matched pattern without any user intervention. If you want to be prompted before deletion then just use rm -r /Pattern. -r is used for recursive and -f is used for force

Also I would suggest you to backup the files in USB stick or other partition if you think you will be required the files at later point. You can use the following command to copy them over

cp -arvp /home/movie/wp-content/uploads/SomeText* /tmp

where,

a=all including hidden files
r=recursive
v=verbose
p=preserve permissions

Addition:

You can delete using the following command:

find / -name 'Sometext*' -print -delete

Last edited by T3RM1NVT0R; 05-21-2011 at 08:27 AM.
 
Old 05-21-2011, 08:42 AM   #6
moviecarpet
LQ Newbie
 
Registered: May 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
This command works:
find /home/movie/wp-content/uploads/ -name 'SomeText*'


but with this nothing happen?
rm -r -f /home/movie/wp-content/uploads/SomeText*
 
Old 05-21-2011, 08:44 AM   #7
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Hanover, Germany
Distribution: Main: Gentoo Others: What fits the task
Posts: 15,650
Blog Entries: 2

Rep: Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095
Because you didn't gave it the -delete option. It would help if you look at the manpage, we won't spoonfeed you.
 
Old 05-21-2011, 08:51 AM   #8
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, Ubuntu, SLES, CentOS
Posts: 1,790

Rep: Reputation: 324Reputation: 324Reputation: 324Reputation: 324
@ Reply

@ Tobi,

Thanks Tobi.

@ moviecarpet

rm -r -f /home/movie/wp-content/uploads/SomeText* will only work if those 'SomeText*' files are located directly under directory /home/movie/wp-content/uploads/ it won't scan some directories to match the pattern.

Else go with the other command -delete that I have mentioned in my previous post.
 
Old 05-21-2011, 09:12 AM   #9
moviecarpet
LQ Newbie
 
Registered: May 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by T3RM1NVT0R View Post
@ Tobi,

Thanks Tobi.

@ moviecarpet

rm -r -f /home/movie/wp-content/uploads/SomeText* will only work if those 'SomeText*' files are located directly under directory /home/movie/wp-content/uploads/ it won't scan some directories to match the pattern.

Else go with the other command -delete that I have mentioned in my previous post.
I understand, just one more question...will this command delete all files and folders starting from "root" folder or only under /uploads/ folder?

find / -name 'Sometext*' -print -delete
 
Old 05-21-2011, 09:22 AM   #10
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Quote:
Originally Posted by moviecarpet View Post
I understand, just one more question...will this command delete all files and folders starting from "root" folder or only under /uploads/ folder?

find / -name 'Sometext*' -print -delete
This command will delete any file starting from the root folder, i.e. effectively system-wide. You probably want something like
Code:
find /path/to/uploads/ -name 'Sometext*' -print -delete
Be careful with this command. Run it without the -delete first to verify that it really only matches unwanted files.
 
1 members found this post helpful.
Old 05-21-2011, 09:39 AM   #11
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, Ubuntu, SLES, CentOS
Posts: 1,790

Rep: Reputation: 324Reputation: 324Reputation: 324Reputation: 324
@ Reply

Exactly!!!

When you run find / -name it searches from / if you give the specified path like crts said find /home/user1h -name 'SomeText*' or find /tmp/downloads/test/ -name 'SomeText*' it will search only under those directories.
 
Old 05-21-2011, 09:39 AM   #12
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by moviecarpet View Post
I understand, just one more question...will this command delete all files and folders starting from "root" folder or only under /uploads/ folder?

find / -name 'Sometext*' -print -delete
The first you've mentioned and very dangerous indeed. The first(s) argument(s) of the find command are the item you search for, basically the starting point (directory) from which you want to perform a search:
Code:
find /home/movie/wp-content/uploads
without any option you will find all the objects below the specified search path. Then you may want to refine your search to find 1) only files (exclude directories, links and so on) using the -type predicate, 2) only files whose name matches a specific pattern using -name. These are the predicates of the find command:
Code:
find /home/movie/wp-content/uploads -type f -name \*SomeText\*
This gives you the result of your search. Then find offers a bunch of actions you can perform on every single item found, for example -ls to retrieve a detailed list, -delete to remove the items or -exec to run a custom command. As previously stated, first check the result of the search to be sure your criteria have matched only the files you really want to delete. If this is true, run the command again adding the -delete action and your files will vanish!
 
Old 05-26-2011, 09:17 AM   #13
moviecarpet
LQ Newbie
 
Registered: May 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
Big Tnx all work
 
Old 05-26-2011, 09:44 AM   #14
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Hanover, Germany
Distribution: Main: Gentoo Others: What fits the task
Posts: 15,650
Blog Entries: 2

Rep: Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095
If you problem is solved please mark this thread as solved with the thread tools on top of this thread.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete all files on a server with a particular filename Robbeh Linux - Newbie 39 03-24-2011 08:37 AM
Find/grep/wc command to find matching files, print filename and word count dbasch Linux - Newbie 10 09-14-2009 06:55 PM
How to delete files tat contains a specific word?? linuq Linux - Newbie 4 06-30-2008 01:12 AM
ow do I delete all files which were created on a specific date marsguy Linux - General 9 08-21-2007 09:15 AM
deleting files containing a specific word Sleb Programming 5 05-25-2006 02:19 PM


All times are GMT -5. The time now is 06:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration