LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 02-26-2016, 08:28 AM   #1
gabrielpasv
LQ Newbie
 
Registered: Feb 2016
Posts: 14

Rep: Reputation: Disabled
Interesting issue while using sudo to delete files and folders


Hi Guys,

I have a script that runs as user ubuntu. It needs to delete some files and folder inside /root.

If I run as root
Code:
rm -rf /root/node-test/*
It will delete all the files and folders inside /root/node-test/ but the directory "/root/node-test/" will remain. That's what I want.

However, if I run this with ubuntu user using sudo, it just doesn't work, if I run as ubuntu user:

Code:
sudo rm -rf /root/node-test/*
it doesn't delete anything.

That's an interesting issue that I came across. I tried to google for it without any luck. I am keeping looking, but decide to post here to see if you guys could help me.

Thanks
 
Old 02-26-2016, 08:32 AM   #2
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 678

Rep: Reputation: 105Reputation: 105
The issue here, is my educated guess, that the shell first tries to expand the "*" before running sudo; since the normal user cannot access this directory, the command set is empty.
 
Old 02-26-2016, 08:39 AM   #3
gabrielpasv
LQ Newbie
 
Registered: Feb 2016
Posts: 14

Original Poster
Rep: Reputation: Disabled
Thank you very much for your prompt reply. Are you aware of any workaround for that ?

I tried to use "sudo -i " but it also doesn't work.


Quote:
Originally Posted by Ramurd View Post
The issue here, is my educated guess, that the shell first tries to expand the "*" before running sudo; since the normal user cannot access this directory, the command set is empty.
 
Old 02-26-2016, 08:54 AM   #4
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 678

Rep: Reputation: 105Reputation: 105
You'd have to work around shell expansion...

was trying to think something up for you ;-) but a bit of a hangover makes it a bit hard to think clearly...

something along these lines might work:
Code:
sudo ls /root/node-test/ | xargs sudo rm -rf
... probably not exactly what you need; for testing you could use 'ls -l' instead of 'rm -rf'; so that if it's not correct, it is at least not destructive :-)
 
Old 02-26-2016, 09:08 AM   #5
gabrielpasv
LQ Newbie
 
Registered: Feb 2016
Posts: 14

Original Poster
Rep: Reputation: Disabled
Didn't work

Look what the commands I have tried but didn't work

sudo bash -c "rm -rf /root/node-licensing/*" -> doesn't work
sudo ls /root/node-test/ | xargs sudo rm -rf -> doesn't work

sudo find /root/node-test/ -type f -exec rm -rf {} \; -> Works partially. It only delete the files, not the folders inside it.


I am getting close

Last edited by gabrielpasv; 02-26-2016 at 09:16 AM.
 
Old 02-26-2016, 09:23 AM   #6
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 678

Rep: Reputation: 105Reputation: 105
this one is really nice;

Code:
sudo find /root/node-test/ -type f -exec rm -rf {} \; -> Works partially. It only delete the files, not the folders inside it.
that is because of the '-type f'; it will not find the directories;
If you omit the -type, it's possible that you will get some errors that files cannot be found (they're deleted by the execute part);
So you can do it in two phases as well:

Code:
sudo find /root/node-test/ -type f -exec rm -rf {} \;
sudo find /root/node-test/ -type d -exec rmdir {} \;
The directories in the second run should be empty, and thus rmdir can be used.
 
Old 02-26-2016, 09:37 AM   #7
gabrielpasv
LQ Newbie
 
Registered: Feb 2016
Posts: 14

Original Poster
Rep: Reputation: Disabled
This way it will work however the second command throws an error and it only doesn't delete /root/node-test because it contains other folders

gabriel@ubuntu:~$ sudo find /root/node-test
/root/node-test
/root/node-test/dir1
gabriel@ubuntu:~$ sudo find /root/node-test/ -type d -exec rmdir {} \;
rmdir: failed to remove ‘/root/node-test/’: Directory not empty
find: `/root/node-test/dir1': No such file or directory
gabriel@ubuntu:~$
gabriel@ubuntu:~$
gabriel@ubuntu:~$ sudo find /root/node-test
/root/node-test
gabriel@ubuntu:~$



It is able to delete sub-folders.

That's one workaround but not a good solution I think. I am keeping searching...
 
Old 02-26-2016, 09:54 AM   #8
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 678

Rep: Reputation: 105Reputation: 105
That's easily fixed:

add '-mindepth 1' to find; som that the 'main' directory is skipped:
Code:
sudo find /root/node-test -mindepth 1 -type d -exec rmdir {} \;
 
Old 02-26-2016, 10:02 AM   #9
gabrielpasv
LQ Newbie
 
Registered: Feb 2016
Posts: 14

Original Poster
Rep: Reputation: Disabled
I was able to make it work with only 1 command

See

sudo find /root/node-test -mindepth 1 -delete


Thank you very much for your help
 
Old 02-26-2016, 10:09 AM   #10
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,714

Rep: Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280
Quote:
Originally Posted by gabrielpasv View Post
Hi Guys,

I have a script that runs as user ubuntu. It needs to delete some files and folder inside /root.

If I run as root
Code:
rm -rf /root/node-test/*
It will delete all the files and folders inside /root/node-test/ but the directory "/root/node-test/" will remain. That's what I want.

However, if I run this with ubuntu user using sudo, it just doesn't work, if I run as ubuntu user:

Code:
sudo rm -rf /root/node-test/*
it doesn't delete anything.

That's an interesting issue that I came across. I tried to google for it without any luck. I am keeping looking, but decide to post here to see if you guys could help me.

Thanks
try using the "-c" option of sudo, as in:
Code:
sudo -c "rm -rf /root/node-test/*"
 
Old 02-26-2016, 10:14 AM   #11
gabrielpasv
LQ Newbie
 
Registered: Feb 2016
Posts: 14

Original Poster
Rep: Reputation: Disabled
Hi, thanks for your help too

but it didn't work


gabriel@ubuntu:~$
gabriel@ubuntu:~$ sudo -c "rm -rf /root/node-test/*"
usage: sudo -h | -K | -k | -V
usage: sudo -v [-AknS] [-g group] [-h host] [-p prompt] [-u user]
usage: sudo -l [-AknS] [-g group] [-h host] [-p prompt] [-U user] [-u user] [command]
usage: sudo [-AbEHknPS] [-r role] [-t type] [-C num] [-g group] [-h host] [-p prompt] [-u user] [VAR=value] [-i|-s] [<command>]
usage: sudo -e [-AknS] [-r role] [-t type] [-C num] [-g group] [-h host] [-p prompt] [-u user] file ...
gabriel@ubuntu:~$




Quote:
Originally Posted by jpollard View Post
try using the "-c" option of sudo, as in:
Code:
sudo -c "rm -rf /root/node-test/*"
 
Old 02-26-2016, 01:07 PM   #12
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,714

Rep: Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280
Well, the last ditch version:
Code:
sudo bash -c "rm -rf /root/node-test/*"
Apologies for leaving out the shell....
 
Old 02-26-2016, 01:11 PM   #13
gabrielpasv
LQ Newbie
 
Registered: Feb 2016
Posts: 14

Original Poster
Rep: Reputation: Disabled
It doesn't work also. I have tested it. If you see one of my previous posts I stated it does'n t work.

The only one that worked for me is

sudo find /root/node-test -mindepth 1 -delete
 
Old 02-26-2016, 01:19 PM   #14
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 2,848

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
This works for me:

Code:
sudo bash -c 'rm -rf /root/node-test/*'

Pass a single-quoted string so it is only expanded by the bash running sudo.
 
Old 02-26-2016, 02:39 PM   #15
gabrielpasv
LQ Newbie
 
Registered: Feb 2016
Posts: 14

Original Poster
Rep: Reputation: Disabled
Yeah... it does work too with single quotes


Thanks
 
  


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
[SOLVED] Delete empty files and folders trintukaz Programming 9 10-14-2011 12:16 PM
find -exec command to recursively delete files and folders in folders with X name Joan Murt Linux - Newbie 2 07-08-2009 05:35 PM
Can't delete certain files or folders...how do I change this? xsithlordx SUSE / openSUSE 10 03-05-2006 11:32 PM
HELP: can't delete files/folders :( kevingpo General 1 12-07-2004 08:51 PM
Delete old files and folders Script? AsteX Linux - General 4 11-11-2004 07:26 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 06:53 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