Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I need to remove entire directories that contain subdirectories on a bunch of windows machines from a Linux machine. Is there a way to do an "rm -rf" (or something similar) using smbclient?
bump... anyone? maybe a suggestion of a clever script that could do it? I've got a couple directories that contain about 100 sub directories, each with about 100 files.... I need to delete them all (at the very least, all the files - if the directories stay there, it won't concern me too terribly much).
Originally posted by no2nt No way that I know of, but if I have to rm -fr multiple dir I just mount the
share and do it from a command line.
not a bad idea (and something I hadn't thought of before); unfortunately, I can't mount because I'm not root & don't have access to root.
FYI: This is all taking place on a render farm in Alabama - I have access to a linux box on the farm and administer all of the windows boxes (which do all the rendering) from there - so I can't physically sit down at any of them and don't have root access on the Linux box.
Well if you can't mount it then you could either modify the smbclient code to make it possible to recursively delete directories. Or you could make a simple script which recursively walks through the directory contents and deletes all the files. I wrote a quick example script. It recursively walks through a directory listing using smbclient. You might have to modify it a bit since the output of the directory listing might look different. And depending on what kind of permissions you have on the share you might need to modify the smbclient commands to connect using a password or something else. And obviously you will have to remove the comment symbols in the removefile and removedir functions. You might want to test it first without deleteing before you actually start deleting the wrong files. Anyways here is the script:
Code:
#/bin/bash
share=//machinename/sharename
# if you want to delete the complete share you could enter just
# a / for directory
directory=directory/to/delete
removefile ()
{
echo "Remove file $1 $2"
#smbclient $share -N -D $1 -c rm $2 > /dev/null
}
removedir ()
{
echo "Remove dir $1 $2"
#smbclient $share -N -D $1 -c rmdir $2 > /dev/null
}
scandir ()
{
outputfile=~dirlist`echo $1 | sed -e 's/\///g'`
smbclient $share -N -D $1 -c ls > $outputfile
IFS=$'\n'
for line in `cat $outputfile`
do
if [ `echo $line | cut -b1-2` == " " ]
then
name=`echo $line | awk {'print $1'}`
dir=`echo $line | awk {'print $2'}`
if [ "$name" != "." -a "$name" != ".." ]
then
case $dir in
D)
scandir ${1}/$name
removedir $1 $name
;;
*)
removefile $1 $name
;;
esac
fi
fi
done
outputfile=~dirlist`echo $1 | sed -e 's/\///g'`
rm $outputfile
}
scandir $directory
No there is no way to delete a directory and its content using smbclient !
Many thanks for the script Mik, I had a recursive problem using it, the $name was overwritten by the recursive call...
Here is a modified version of the script proposed
Do not forget to set the parameters at the beginning of the script
Code :
Code:
#/bin/bash
REMOTE_SERVERNAME=?
REMOTE_DIR=?
TODELETE_DIR=? # if you want to delete the complete share you could enter just a / for directory
REMOTE_USER=?
REMOTE_PASSWORD=?
# Send both stderr and stdout to /dev/null to avoid connection info message on the output
function removefile () {
smbclient \\\\$REMOTE_SERVERNAME\\$REMOTE_DIR -U $REMOTE_USER%$REMOTE_PASSWORD -D $1 -c "rm $2" &> /dev/null
}
function removedir () {
smbclient \\\\$REMOTE_SERVERNAME\\$REMOTE_DIR -U $REMOTE_USER%$REMOTE_PASSWORD -D $1 -c "rmdir $2" &> /dev/null
}
function deletedir () {
outputfile=~dirlist`echo $1 | sed -e 's/\///g'`
smbclient \\\\$REMOTE_SERVERNAME\\$REMOTE_DIR -U $REMOTE_USER%$REMOTE_PASSWORD -D $1 -c "ls" > $outputfile 2> /dev/null
IFS=$'\n'
for line in `cat $outputfile`
do
echo $line
if [ `echo $line | cut -b1-2` == " " ]
then
name=`echo $line | awk {'print $1'}`
dir=`echo $line | awk {'print $2'}`
if [ "$name" != "." -a "$name" != ".." ]
then
case $dir in
D)
deletedir ${1}/$name
removedir $1 $name
;;
*)
removefile $1 $name
;;
esac
fi
fi
done
name=${1##*/}
outputfile=~dirlist`echo $1 | sed -e 's/\///g'`
rm $outputfile
}
deletedir $TODELETE_DIR
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.