LinuxQuestions.org
Review your favorite Linux distribution.
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 03-09-2018, 12:03 PM   #1
bmxakias
Member
 
Registered: Jan 2016
Posts: 164

Rep: Reputation: Disabled
Question How to exclude a folder from rm -rf ?


Hello

I have a path like:

Code:
/home/folder/folder2/folder3/folder4/
and i have there many files and folders with subfolders.....

I am using a command like:

Code:
rm -rf /home/folder/folder2/folder3/folder4
and i am looking for a way to exclude a folder (that contains files and subfolders):

Code:
/home/folder/folder2/folder3/folder4/uploads/
How can i do that?

Thank you
 
Old 03-09-2018, 12:10 PM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724
Move uploads dir elswhere, delete folder 4, recreate it and move uploads into it?
 
1 members found this post helpful.
Old 03-09-2018, 12:32 PM   #3
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.6
Posts: 3,654

Rep: Reputation: 1214Reputation: 1214Reputation: 1214Reputation: 1214Reputation: 1214Reputation: 1214Reputation: 1214Reputation: 1214Reputation: 1214
Don't use the force (-f) option. You'll be prompted for y or n for each deletion, just answer n when asked to decend into the uploads/ folder

Obviously tedious if there are a lot of files in folder4/, or many other sub-folders besides uploads/

I've taken to almost never using the -f option from the command line...you get no information about what has been deleted, and no error messages. Very risky thing to use.
 
Old 03-09-2018, 12:53 PM   #4
bmxakias
Member
 
Registered: Jan 2016
Posts: 164

Original Poster
Rep: Reputation: Disabled
Quote:
Move uploads dir elswhere, delete folder 4, recreate it and move uploads into it?
400GB folder so not that easy

Quote:
Don't use the force (-f) option. You'll be prompted for y or n for each deletion, just answer n when asked to decend into the uploads/ folder
I am trying to use it on a bash script so that will not help.Also to clicking (y) for a ton of files and folders also is not an easy one
 
Old 03-09-2018, 01:02 PM   #5
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.6
Posts: 3,654

Rep: Reputation: 1214Reputation: 1214Reputation: 1214Reputation: 1214Reputation: 1214Reputation: 1214Reputation: 1214Reputation: 1214Reputation: 1214
Quote:
Originally Posted by bmxakias View Post
400GB folder so not that easy
Moving a folder should be a blink, no matter what the size, because (if I understand correctly) mv doesn't actually relocate the folder, just changes the pointer to it in the metadata. I don't have really big folder or file to confirm that with, tho. Copying (cp) is different, as the files are relocated. Try it out. Let us know what you experience.

And yes, not using -f would be tedious for a large number of files, as I said.
 
Old 03-09-2018, 01:04 PM   #6
bmxakias
Member
 
Registered: Jan 2016
Posts: 164

Original Poster
Rep: Reputation: Disabled
Thanks for your reply !

I will wait for a few more recommendations that may help and then i will test it.....
 
Old 03-09-2018, 01:25 PM   #7
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724
Quote:
Originally Posted by bmxakias View Post
400GB folder so not that easy
As scasey said, it's a quick operation if you move it on same file system (ie not in another partition). It's like as if you renamed it.

If it's repeatable action, maybe write a script
 
Old 03-09-2018, 01:38 PM   #8
mrmazda
Senior Member
 
Registered: Aug 2016
Location: USA
Distribution: openSUSE, Debian, Knoppix, Mageia, Fedora, others
Posts: 1,712

Rep: Reputation: 529Reputation: 529Reputation: 529Reputation: 529Reputation: 529Reputation: 529
Using mc to select directories to be deleted surely must be simpler and safer than typing some complex risky rm command.
 
Old 03-09-2018, 02:05 PM   #9
bmxakias
Member
 
Registered: Jan 2016
Posts: 164

Original Poster
Rep: Reputation: Disabled
Quote:
Using mc to select directories to be deleted surely must be simpler and safer than typing some complex risky rm command.
The files and folders on that directory are not the same always
 
Old 03-09-2018, 02:15 PM   #10
mrmazda
Senior Member
 
Registered: Aug 2016
Location: USA
Distribution: openSUSE, Debian, Knoppix, Mageia, Fedora, others
Posts: 1,712

Rep: Reputation: 529Reputation: 529Reputation: 529Reputation: 529Reputation: 529Reputation: 529
Quote:
Originally Posted by bmxakias View Post
The files and folders on that directory are not the same always
Exactly the reason for using mc and selecting each time.
 
Old 03-09-2018, 02:29 PM   #11
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: FreeBSD/Slackware-14.2+/ArcoLinux
Posts: 9,083

Rep: Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903
Quote:
Originally Posted by scasey View Post
Moving a folder should be a blink, no matter what the size, because (if I understand correctly) mv doesn't actually relocate the folder, just changes the pointer to it in the metadata. I don't have really big folder or file to confirm that with, tho. Copying (cp) is different, as the files are relocated. Try it out. Let us know what you experience.

And yes, not using -f would be tedious for a large number of files, as I said.
that is only if it is on the same partition, is moves really fast, but if one needs to relocate to another partition or drive, it copies, then deletes the original. which takes a lot longer.
 
Old 03-09-2018, 02:35 PM   #12
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: FreeBSD/Slackware-14.2+/ArcoLinux
Posts: 9,083

Rep: Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903
with the move on to the same partition delete leftovers

puts uploads in /home/user/uploads
removes the remainder.

Code:
deleteme@slackssh64:~$ pwd
/home/deleteme

deleteme@slackssh64:~$ mkdir -p base/fol1/fol2/fol3/subbase
deleteme@slackssh64:~$ ls
base/
deleteme@slackssh64:~$ mv  base/fol1/fol2/fol3/subbase/ ~/
deleteme@slackssh64:~$ ls
base/  subbase/
deleteme@slackssh64:~$ rm -r base/ 
deleteme@slackssh64:~$ ls
subbase/

Last edited by BW-userx; 03-09-2018 at 02:45 PM.
 
Old 03-10-2018, 05:08 AM   #13
bmxakias
Member
 
Registered: Jan 2016
Posts: 164

Original Poster
Rep: Reputation: Disabled
If my uploads folder is 400GB and my disk size is 500GB it will work to move it on another directory or it will need to allocate first the full size and then to free it as that will not work?
 
Old 03-10-2018, 05:18 AM   #14
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: FreeBSD/Slackware-14.2+/ArcoLinux
Posts: 9,083

Rep: Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903
you could go down into that dir, then just move a few at a time, if not one at a time until you've freed up enough to do a move all of these at the "same time" command. if and or it gets to that point.

The move command will delete the source file when it is done moving it, so you can pick up where you left off easier. In case you have to stop for whatever reason, and pick up where you left off at a later date.

remember the move command in Linux operates like this, (probably windows and MAC too) copy to destination then remove source if you are crossing partitions. If not it just writes the "new" location to the MFT (master file table), or whatever "they" decided to call it on the disk if you are staying in the same partition. which is a lot faster.

with approx 100GB free space. you might want to start with move a few at a time. even though in all reality it does one at a time, using a list of what you told it to move for reference.

you could ever write a little script to move x amount of files at a time. then stop, then just run it again unto you've freed up enough space to do a move all of these at once command. Using a file manager or command line or script.

a script to use if you want to.
Code:
#!/bin/bash

source_files=
destination=
count=0

#if one tries to run it without first telling it how many files to move
#it will tell op (operator) that message then exit. 
[[ "$1" -lt '1' ]] && (echo "add amount to move" ; exit )

while read F && [[ $count -lt $1 ]] ;
do

    c=$F
    xpath=${c%/*} 
    xbase=${c##*/}
    xfext=${xbase##*.}
    xpref=${xbase%.*}
    #to see what each one does
    echo $xpath
    echo $xbase
    echo $xpref
    echo $xfext
   

#Creates destination directory(ies) and sub-directory 
#by the same name as the file
mkdir -p "$destination"/"$xpref"

echo "mv -v "$F" "$destination"/"$xpref" "
((count++))

done<<<"$(find "$source_files" -type f -name "*.mp4" -o -name "*.mkv" -o -name "*.avi")"
just add whatever file types you want and mod it to how ever you want if you want to use it.

Last edited by BW-userx; 03-10-2018 at 05:53 AM.
 
Old 03-10-2018, 05:21 AM   #15
bmxakias
Member
 
Registered: Jan 2016
Posts: 164

Original Poster
Rep: Reputation: Disabled
Not practical at all

Yes i am talking to move it at the same partition....
 
  


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
Exclude a folder on tar ASTRAPI Linux - Newbie 6 11-17-2014 10:54 AM
[SOLVED] exclude nested folder from tar threezerous Linux - Newbie 4 11-01-2014 02:20 PM
Find dont exclude folder postcd Linux - General 7 08-23-2014 09:24 PM
TAR Exclude everything in folder except this folder rationalthinker1 Linux - Newbie 5 04-30-2012 08:20 PM
Exclude a folder from a recursive delete? raz230 Linux - General 6 12-23-2011 01:57 PM

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

All times are GMT -5. The time now is 03:44 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration