how to remove recursilvely from a directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to remove recursilvely from a directory
# 1  
Old 04-08-2009
how to remove recursilvely from a directory

I can remove blank lines at end of file using following code

for files in `ls * 2>/dev/null`
do
sed -e :a -e '/^\n*$/N;/\n$/ba' $files > newfile
mv newfile $files
done

How can I change above code, so that it recursively executes that code for all files under a directory?
# 2  
Old 04-08-2009
Use find with option '-type f' so it only returns files relative to the starting point.
Code:
for files in `find . -type f 2>/dev/null`
do
    sed -e :a -e '/^\n*$/N;/\n$/ba' $files > newfile
    mv newfile $files
done

# 3  
Old 04-09-2009
I also want to modify this code so that it executes mv command only if there is a blank line at end of file ie otherwise file time stamp remains untouched, is it possible?
# 4  
Old 04-09-2009
Quote:
Originally Posted by ruchimca
I also want to modify this code so that it executes mv command only if there is a blank line at end of file ie otherwise file time stamp remains untouched, is it possible?
I don't believe you can do that with mv. You may have to settle for a copy and remove the old one.

Code:
cp -p

The above 'preserves' (hence the 'p') attributes, including the timestamp.
# 5  
Old 04-09-2009
Put this in the for statement replacing the 'mv' statement.

Code:
if (( $(wc -l newfile) < $(wc -l $files) ))
then
 mv newfile $files
fi

If blank lines were removed from $files, then newfiles should have a smaller line count, since blank lines still count as lines. Then your time stamp changes only if blanks were removed. If the file was copies over in full, then newfile wont be less than $files and the 'mv' wont happen.
# 6  
Old 04-09-2009
Above code gives me syntax error

+ find . -type f
+ sed -e :a -e /^\n*$/N;/\n$/ba ./Makefile
+ wc -l newfile
+ wc -l ./Makefile
+ let 9 newfile < 9 ./Makefile
let: r 11: syntax error in arithmetic expression " 9 newfile" near n
# 7  
Old 04-09-2009
Sorry, change the if line to :

Code:
if (( $(cat newfile | wc -l) < $(cat $files | wc -l) ))
then 
  mv newfile $files
fi

In my haste to avoid using 'cat' unnecessarily I forgot that the file name would be part of the output of the 'wc' command when specified as an argument.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to remove directory of a particular user?

How to remove directory of a particular user (1 Reply)
Discussion started by: pspriyanka
1 Replies

2. HP-UX

Directory can't remove!

Hello my friends, I have a directory called log_old when I try to delete it, it tells me no such file or directory. Then I tried to run this command: ls -lb It tells me the directory name, it is: log_old1\177 !! I tried to remove it using inode, using this... (4 Replies)
Discussion started by: Mohannad
4 Replies

3. Shell Programming and Scripting

Remove Directory

By Mistake i created a directory named "-lrt" in one of my working directories and now i am not able to delete it , please suggest hw to delete this directory now ? Thanks in advance. (1 Reply)
Discussion started by: neeraj617
1 Replies

4. Shell Programming and Scripting

Remove directory with exceptions

Hi, I want to remove a directory recursively except the inside directories calles .SYNC (designsync dirs) I am looking for something like: \rm -rf < find . * | grep -v .SYNC The find works ok but I do not know how to redirect it. Please help. Regards, Ziv (3 Replies)
Discussion started by: zivsegal
3 Replies

5. UNIX for Advanced & Expert Users

Remove directory with exceptions

Hi, I want to remove a directory recursively except the inside directories calles .SYNC (designsync dirs) I am looking for something like: \rm -rf < find . * | grep -v .SYNC The find works ok but I do not know how to redirect it. Please help. Regards, Ziv:rolleyes: (1 Reply)
Discussion started by: zivsegal
1 Replies

6. Shell Programming and Scripting

Remove contents of directory, but not directory

What's the best way to delete everything in a directory, but not the directory itself, without using shell wildcards? (9 Replies)
Discussion started by: pdc
9 Replies

7. Shell Programming and Scripting

remove empty directory

Hi, I need to delete an empty directory in a temp directory except "dir5" (keep everything that is not empty). Plese advise. Here is an example of my directory. /dir/temp/ dir1 - delete if this is empty dir2 - delete if this is empty dir3 - delete if this is empty dir4 - delete if this... (7 Replies)
Discussion started by: sirrtuan
7 Replies

8. Programming

Recursive remove directory.

What is the best way to completely remove dir with it's content ??? rmdir deletes only EMPTY dirs as i know. The man page of remove function says "remove() deletes a name from the file system." Can it remove any dir recursively ??? :rolleyes: (7 Replies)
Discussion started by: Trump
7 Replies

9. Solaris

Can't remove a directory

Hello, somehow a directory called -r was created on one of my servers and I can't remove is using either rm or rmdir. Is there a way to remove this directory? Thank you. -David (2 Replies)
Discussion started by: dkranes
2 Replies

10. UNIX for Dummies Questions & Answers

smitty, remove user, remove directory as well..

hi, i am on aix. i used smitty to remove a user.. but then found that its directory still exists.... so i have to remove the directory manually... am i doing it the right way? (2 Replies)
Discussion started by: yls177
2 Replies
Login or Register to Ask a Question