Help with recursive command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with recursive command
# 1  
Old 07-01-2012
Help with recursive command

Hi,
I need help with recursive function. This is not a part of any homework or classroom related. I am trying to learn unix and working on a question posted on site. basically i have to create a script which does what rm -r does.
my code so far:

Code:
  1 #!/bin/bash
  2
  3 function recursive () {
  4 if ! [ "$(ls -A "$1" )" ]
  5 then
  6       mv -f $1 ~/deleted
  7 else
  8            cd $1
  9            local files=$(ls -A)
 10            echo $files
 11            for f in $files
 12            do
 13
 14               if [ -d $f ]; then
 15                   removeDir
 16                else
 17                    mv -f $f ~/deleted
 18               fi
 19            done
 20 fi
 21 }
 22 recursive example

problem I face is: either files get deleted or directory gets deleted. How do i fix this.
I need to make sure that dirctory gets deleted along with the files inside it.
thanks in advance

Last edited by Scrutinizer; 07-01-2012 at 10:43 AM.. Reason: code tags instead of quote tags
# 2  
Old 07-01-2012
If you are using a recursive function you need to call the function itself inside the function, I don't see that happening here (perhaps you meant to write "recursive" instead of removeDir or vice versa?) Also it is good to use function parameters and/or local variables..

What is a bit confusing is the use of the mv command, since then you would not need recursion, but you could do just one mv. The rm command is another matter..

Lastly, pay attention to proper quotation around variable references or it will not work for files with spaces in the name (do you really need the $files variable?)..

Good luck,

S.

Last edited by Scrutinizer; 07-01-2012 at 01:20 PM..
# 3  
Old 07-01-2012
yeah i did meant recursive instead of removedir. I called removedir first. i am creating a replica of rm command so i am just deleted files to destination folder ~/deleted.

my updated code looks like this:
Code:
1 #!/bin/bash
  2
  3 function recursive () {
  4 if ! [ "$(ls -A "$1" )" ]
  5 then
  6       mv -f $1 ~/deleted
  7 else
  8         cd $1
  9         local files=$(ls -A)
 10         echo $files
 11         for f in $files
 12         do
 13
 14                 if [ -d $f ]; then
 15                 recursive $f
 16         else
 17                 cd ..
 18                 mv $1 ~/deleted
 19                 mv $f ~/deleted/$1
 20
 21         fi
 22         done
 23 fi
 24 }
 25 recursive example

It works perfectly now...It deletes the directory and all folders subsided in it. another problem I come across is errors. It moves my dir/sub files correctly the way I want but after execution it says:
Code:
[bluebird ~]$ sh recursive
file1 file2
mv: cannot stat `dir': No such file or directory
mv: cannot stat `file1': No such file or directory
mv: cannot stat `file2': No such file or directory

file1 and file2 resides in dir

so it works but throws this error. what shall i do?

Last edited by Scrutinizer; 07-01-2012 at 01:21 PM.. Reason: code tags instead of quote tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Command failed for target 'check-recursive' error

Sorry if this is the wrong place for this. I haven't done UNIX Admin in a long time and am trying to update the utilities on a Solaris server so that I can get Apache 2.4.25 installed. I am finding a lot of utilities that are just too far out of date and some versions are no longer supported. So... (0 Replies)
Discussion started by: PJ_Namias
0 Replies

2. UNIX for Dummies Questions & Answers

Help needed - find command for recursive search

Hi All I have a requirement to find the file that are most latest to be modified in each directory. Can somebody help with the command please? E.g of the problem. The directory A is having sub directory which are having subdirectory an so on. I need a command which will find the... (2 Replies)
Discussion started by: sudeep.id
2 Replies

3. UNIX for Dummies Questions & Answers

Non Recursive Find Command

Hello Unix Gurus, I am using the following find commands: 1) find Input_Path -name '*.' -exec mv -f {} Outputpath \; 2) find Inputpath -name '*.' -exec cp {} Outputpath \; 3) find Somepath -name '*.' Now the problem is my Unix version does not support maxdepth Option for find... (7 Replies)
Discussion started by: pchegoor
7 Replies

4. Solaris

date command, loop/recursive

is it possible to use output of one date command as input of another? I would like to know the date of Monday two weeks ago. so, the idea is that one date command subtracts two weeks, and the other finds the Monday. (2 Replies)
Discussion started by: orange47
2 Replies

5. Shell Programming and Scripting

Command running for all recursive files

hi, I have installed ACL(access control list) in my ubuntu box in order to know which all are the users having permissions to read and write the files; If u run the command like; $getfacl /root/ It will give following output: # file: root/ # owner: root # group: root user::rwx... (2 Replies)
Discussion started by: ajaypadvi
2 Replies

6. Shell Programming and Scripting

Command to sort directories after a recursive find

find -type d -name "TC_*" | sort That's what I have so far... it finds the appropriate directories and then sorts them. But, when it comes to nested subdirectories, it only sorts relative to the first subdirectory. I want it to sort based on the directory at the end of the path. Does anyone know... (3 Replies)
Discussion started by: crimsondarkn
3 Replies

7. UNIX for Dummies Questions & Answers

Recursive Permissions???

Is there anyway that I can change permissions on a directory and all its sub-directories and files using one single "chmod" command?? (5 Replies)
Discussion started by: the_red_dove
5 Replies

8. UNIX for Dummies Questions & Answers

help with recursive copy command

Hi Guys, I am experiencing a problem right now while copying a directory as well as its subdirectories to my target directory. I know this is a very simple UNIX command using cp -R source directory target directory. but unfortunatley while doing this an error comes up on the command line saying... (2 Replies)
Discussion started by: Knowledge_Xfer
2 Replies

9. UNIX for Advanced & Expert Users

Non recursive find command

Hi, I have question is related to find command. I want to find command should search in current folder only not recursive mode(sub-folders). I found a one way of, find . \( -name success -prune \) -o -name "Rajini*" How ever, my current folder is having lots sub-folders and am not... (7 Replies)
Discussion started by: Nagapandi
7 Replies

10. UNIX for Advanced & Expert Users

recursive nice value

Hi all, I have a running process that will spawn a large number of perl processes. How can I set that these all get spawned with a low priority nice value? I don't mind if all perl related processes take this level. Note, the executing script is compiled and can not be altered at a code... (1 Reply)
Discussion started by: nhatch
1 Replies
Login or Register to Ask a Question