LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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
 
LinkBack Search this Thread
Old 02-07-2013, 03:34 AM   #1
{set}
LQ Newbie
 
Registered: Feb 2013
Posts: 1

Rep: Reputation: Disabled
Deleting certain files in the home directory from another directory


Hi, so like the topic says, I'm wondering how you can delete all files of a particular type, like .html, in the home directory while being in a different directory.

Thanks.
 
Old 02-07-2013, 03:53 AM   #2
mimorek
LQ Newbie
 
Registered: Feb 2013
Posts: 2

Rep: Reputation: Disabled
Not sure, but thought this should work:

find ~ -name "*html" -exec rm {} \;

The "~" stands for your home directory.
 
Old 02-07-2013, 04:38 AM   #3
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,778
Blog Entries: 4

Rep: Reputation: 282Reputation: 282Reputation: 282
In order to manage files that are not in your current directory, you will need to use absolute path of the target file. Absolute path is nothing but full path i.e. starting from "/" i.e. root directory.

Let's say you're in /home/jack and want to delete /tmp/sample.html, then invoke:-
Code:
user@hostname{/home/jack}~$ rm /tmp/sample.html
Use of a simple filename will give you error of 'no such file or directory'.

Second, in order to delete particular type of files, use globbing.
Code:
user@hostname{/home/jack}~$ rm /tmp/*.html
OR, If you're already in parant folder of .html files, then you can:-
Code:
user@hostname{/home/jack}~$ rm *.html
A * in simple words, means all. In your case, a *.html means all having a .html extension.

Warning: Be carefull while invoking rm command. A wrong use of rm command may destroy the whole system.

Last edited by shivaa; 02-07-2013 at 04:42 AM.
 
Old 02-09-2013, 09:44 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
Just a bit more to add. As said, an absolute path is one that starts from '/', the root directory. A relative path is one that starts from the current working directory.

The simplest relative path is just the name of a file or directory. The shell assumes that it exists in the current directory.

In addition, '.' is a hardlink that always refers to the current directory, and '..' is a link to the current directory's parent, i.e. one level up.

So './filename' is the same as 'filename'. To descend into subdirectories, just include their paths: './subdir1/subdir2/filename'.

To go up, and over, to other directories, you can use patterns like '../../grandparentdir/newsubdir/filename', or else use an absolute path.

Globbing patterns can be used in any kind of path.


'~' is a kind of alias that refers to the user's HOME directory, generally '/home/username'. It's a bit limited in what patterns it can be used in, so a more reliable solution is often to insert the "$HOME" variable into your commands instead, which holds the same string.


All of the following are equivalent.
Code:
rm "/home/username/subdir/"*.html
rm ~"/subdir/"*.html
rm "$HOME/subdir/"*.html

cd "$HOME/subdir" && rm *.html

Be sure to quote any string or variable that can contain spaces, but don't quote globbing patterns or '~'. See here for more on shell quoting and argument processing:

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

Last edited by David the H.; 02-09-2013 at 09:47 AM. Reason: formatting
 
Old 02-09-2013, 10:37 AM   #5
brianL
LQ 5k Club
 
Registered: Jan 2006
Location: Oldham, Lancs, England
Distribution: Slackware & Slackware64 14.1
Posts: 6,702
Blog Entries: 51

Rep: Reputation: 1243Reputation: 1243Reputation: 1243Reputation: 1243Reputation: 1243Reputation: 1243Reputation: 1243Reputation: 1243Reputation: 1243
Wouldn't this work? A simple:
Code:
rm ~/*.html
 
Old 02-09-2013, 11:07 AM   #6
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,778
Blog Entries: 4

Rep: Reputation: 282Reputation: 282Reputation: 282
Quote:
Originally Posted by brianL View Post
Wouldn't this work? A simple:
Code:
rm ~/*.html
Of course it will work fine, but all depends upon the location of .html file. David has given a more versatile answers.

IMO, OP should only use absolute path of specified files, wherever those files are located. It will be more safe, and prevent any damage to other non-relevent files accidently.
 
Old 02-09-2013, 11:11 AM   #7
brianL
LQ 5k Club
 
Registered: Jan 2006
Location: Oldham, Lancs, England
Distribution: Slackware & Slackware64 14.1
Posts: 6,702
Blog Entries: 51

Rep: Reputation: 1243Reputation: 1243Reputation: 1243Reputation: 1243Reputation: 1243Reputation: 1243Reputation: 1243Reputation: 1243Reputation: 1243
Quote:
Originally Posted by shivaa View Post
Of course it will work fine, but all depends upon the location of .html file.
The OP specified his home directory.
 
Old 02-09-2013, 11:23 AM   #8
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,778
Blog Entries: 4

Rep: Reputation: 282Reputation: 282Reputation: 282
Quote:
Originally Posted by brianL View Post
The OP specified his home directory.
Agree. But OP has also mentioned ...while being in a different directory.

Although, a '~' will point to his home directory only. But being in a different directory could also mean that he's switch to some other user account. And in that case, '~' will point to other user's home directory, but an absolute path will always point to the correct file on a system irrespective of from where and who's invoking the cmd. (possibilities are infinite ).
 
Old 02-09-2013, 11:28 AM   #9
brianL
LQ 5k Club
 
Registered: Jan 2006
Location: Oldham, Lancs, England
Distribution: Slackware & Slackware64 14.1
Posts: 6,702
Blog Entries: 51

Rep: Reputation: 1243Reputation: 1243Reputation: 1243Reputation: 1243Reputation: 1243Reputation: 1243Reputation: 1243Reputation: 1243Reputation: 1243
Quote:
Originally Posted by shivaa View Post
(possibilities are infinite ).
Yeah, you're right.
 
Old 02-10-2013, 01:53 AM   #10
smbhandary
LQ Newbie
 
Registered: Jan 2013
Distribution: RHEL and AIX ( sort of )
Posts: 18

Rep: Reputation: Disabled
best practise is to specify absolute path.

Run as root

find /home/<id in focus> -xdev -type f -name \*.htm | xargs rm -f

find /absolute/path/ -type f -name \*.htm | xargs rm -f - if you have soft linked files to all over the system.

Last edited by smbhandary; 02-10-2013 at 01:54 AM. Reason: addenda..
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] deleting directory with files and subdirectory kawaikx15 Linux - Newbie 1 04-16-2012 09:33 PM
Rsync deleting files from destination directory Russell P Linux - Newbie 2 07-14-2011 11:07 AM
[SOLVED] Deleting all files from a directory except for two nonshatter Linux - Newbie 2 11-25-2010 08:57 AM
Deleting home directory of deleted user Nemesissparadise Linux - Newbie 3 04-16-2009 11:06 AM
Reinstalling FC 4 without deleting /home directory BerryBlocksta9 Fedora 4 01-25-2006 02:50 AM


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

Main Menu
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