LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-01-2015, 05:44 PM   #1
kevz
LQ Newbie
 
Registered: Mar 2015
Posts: 4

Rep: Reputation: Disabled
Removing files starting with dot


Hello,

I would like to remove those files:

MyDir/boo.tmp
MyDir/.zip.tmp

rm MyDir/*.tmp removes only boo.tmp but not .zip.tmp (No such file or directory).

I want to be able to remove any file that finishes with the .tmp extension even if the filename starts with .something.tmp

Thanks for your help :}
 
Old 04-01-2015, 05:49 PM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
Code:
find -iname ".*.tmp" -delete
But be careful with the -delete option to find as you can SERIOUSLY fubar your system, read the man page for find first.
 
Old 04-01-2015, 06:23 PM   #3
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
In addition to what Keith suggested. I would say before you go ahead with delete get the list. So here is how you have to do:

Code:
find -iname ".*.tmp" -print >> list_of_files_to_be_deleted
Once you have checked that whatever showing up in the list are indeed the files you want to delete and nothing useful, then you can run a for loop to delete those files like this:

Code:
for i in cat `list_of_files_to_be_deleted`
do
{
     rm -rf $i
}
done
 
Old 04-01-2015, 10:13 PM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by kevz View Post
Hello,

I would like to remove those files:

MyDir/boo.tmp
MyDir/.zip.tmp

rm MyDir/*.tmp removes only boo.tmp but not .zip.tmp (No such file or directory).

I want to be able to remove any file that finishes with the .tmp extension even if the filename starts with .something.tmp

Thanks for your help :}
This is because files starting with "." are skipped by the shell evaluating "*". It prevents bad things like "rm -rf *" from trying to delete "." and/or "..". Actually deleting these that way would cause you to loose all your files (an rm "..", would remove the parent directory...).

The safer way is to do an "rm .*.tmp", and though it requires a second command, you could do a "rm *.tmp .*.tmp" which will delete both.
 
1 members found this post helpful.
Old 04-07-2015, 05:35 PM   #5
kevz
LQ Newbie
 
Registered: Mar 2015
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thank you for your replies.
I'll probably go with the safer way "rm *.tmp .*.tmp"

Also...
How do you safely remove a file called "*." ?
 
Old 04-07-2015, 05:44 PM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
You just delimit the *

Code:
rm \*.
 
Old 04-07-2015, 05:47 PM   #7
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by kevz View Post
Thank you for your replies.
I'll probably go with the safer way "rm *.tmp .*.tmp"

Also...
How do you safely remove a file called "*." ?
I usually do it with a "rm './*.'", where the single quotes disable the substitutions that the shell would do. But if you have a mix characteristics where you want to use wildcards as well as non-wildcards you have to escape the non-wildcards...
 
Old 04-07-2015, 06:36 PM   #8
timl
Member
 
Registered: Jan 2009
Location: Sydney, Australia
Distribution: Fedora,CentOS
Posts: 750

Rep: Reputation: 156Reputation: 156
I guess
Quote:
rm -i
is an option. This prompts before removal
 
  


Reply



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
file name starting with dot sryzdn Linux - Newbie 6 06-11-2013 10:22 PM
rsync problems due to dot files (.*.ext) from Mac OSX created files anon091 Linux - Server 11 03-29-2013 01:19 PM
How to remove all (dot) files without removing toplevel dir? mattengland Linux - Software 8 07-19-2011 02:57 PM
Make awk detect lines starting with a dot BlueSpirit Programming 12 05-15-2011 10:50 AM
du -hs .* does not show size of directories starting with a dot (anymore) Hannes Linux - Desktop 2 03-22-2011 12:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:12 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
Open Source Consulting | Domain Registration