LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 09-10-2002, 11:42 PM   #1
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
Can you do recursive commands in smbclient?


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?

thanks
 
Old 09-12-2002, 02:14 PM   #2
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
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).

 
Old 09-12-2002, 03:24 PM   #3
no2nt
Member
 
Registered: Aug 2001
Location: South Carolina, USA
Distribution: Redhat 8.0/Custom
Posts: 96

Rep: Reputation: 16
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.

mount -t smb -o username=bmenking,password=xxxxx //<remoteip>/<share> /<mount point>

cd /<mount-point>
rm -fr <desired dir>

Hope this helps.

smbclient does support recursion but only for the mput and mget commands.
check out the command recurse in the smbclient man page.

Last edited by no2nt; 09-12-2002 at 03:25 PM.
 
Old 09-12-2002, 05:06 PM   #4
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
Quote:
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.

any other suggestions are welcome.
 
Old 09-13-2002, 04:02 AM   #5
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 47
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
 
Old 04-12-2012, 08:08 AM   #6
lsdev_
LQ Newbie
 
Registered: Apr 2012
Posts: 1

Rep: Reputation: Disabled
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
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
no smbclient wildbob Fedora 2 04-24-2005 12:49 PM
smbclient help m0nk3yb0y Linux - Networking 5 03-15-2005 12:12 AM
How to use SMBclient? cathodion Linux - Networking 4 12-22-2004 03:48 AM
smbclient subaruwrx Linux - Networking 7 09-02-2004 08:43 AM
rm -r what is recursive wogga Linux - Software 3 05-28-2004 02:29 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:22 AM.

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