When I tried
from any other directory, It was unable to delete, exited normally, without yielding any ,messages with 0 status.
But when I executed the same command after doing a cd to TGTDIR, It worked.
OK. So the ls -t $TGTDIR gives you filenames in $TGTDIR, not absolute pathnames. So you either removed files (of any type; not necessarily directories) in the directory where you ran this script (instead of from $TGTDIR), or it didn't find files in the current directory with the names of the files you were trying to remove (but the rm -f option precluded writing diagnostic messages for nonexistent files).
I'm surprised that the 1st code suggestion MadeInGermany gave you worked (unless you were already located in $TGTDIR). The cd command it used was in a command substitution (which runs in a subshell environment, which should not affect the working directory of the rm command). However, the following slight modification of that command:
will probably do what you wanted. The 2nd code suggestion he made should work because the directories to be removed would be given to the rm command as absolute pathnames.
These 2 Users Gave Thanks to Don Cragun For This Post:
Thanks for the fix, Don. I had typed that in a hurry.
I guess it was the second suggestion that worked.
Another suggestion, do the cd in a sub shell so it does not harm the main shell:
And the following can handle file names with spaces:
These 2 Users Gave Thanks to MadeInGermany For This Post:
Yes Don, The second suggestion given by MadeInGermany worked for me.
Thank you for sharing the solution.
Hello Devendra,
Code given in POST#5 is working fine for me, not sure(surprised) why it is not working for you.
Let me know if you got any error after executing it. The only difference between 1st command and 2nd command given in
POST#5 is | sh will perform the deletion and 1st command will only print the directories to be deleted
as follows.
Thanks,
R. Singh
Last edited by RavinderSingh13; 01-29-2015 at 07:14 AM..
Reason: Added comments
This User Gave Thanks to RavinderSingh13 For This Post:
I have figured it out(sorry for confusion), we need to give complete path while deleting the directories while passing it from awk to sh as follows.
Hope this helps.
Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
Can anyone help me with a short command or script for the below scenario
there is a path,
/a/b/c/home??
Inside the above path there are number of subdirectories such as
one
two
three
four
i need to take all the subdirectories inside home?? with full path. i need only one level of... (4 Replies)
Hii,
Could someone help me to append string to the starting of all the filenames inside a directory but it should exclude .zip files and subdirectories.
Eg.
file1: test1.log
file2: test2.log
file3 test.zip
After running the script
file1: string_test1.log
file2: string_test2.log
file3:... (4 Replies)
I need to write a script to :
list files with *.i extension in a directory and all its subdirectories + 30days old, save it in a file and then remove (2 Replies)
hi
pls give me a script to compress all directories inside a directory and remove the original uncompressed version...
>>
please also tell the single commmand to uncompress all the directories back...whemn needed (2 Replies)
Assume I want to remove a whole directory tree beginning with /foo/bar/
The directory or sub-directories may contain files.
The top directory /foo/bar/ itself should not be deleted.
rm -f- r /foo/bar
does not work because it requires a directory tree without files.
How does it work... (3 Replies)
My company has a policy that employees can't keep music on our servers so im looking for a line or script that I can run as part of a cron job that will remove all mp3's in the users home directories.
Does anyone have any idea how I might accomplish this? (3 Replies)
Hi,
I am a newbie in shell scripting. I have to copy a particular sub-directory (data) from a large no. of directories (all in the same folder) and paste them to another directory ( /home/hubble/data ) and then rename all the subdirectories (data) as the name of its parent directory.
please... (8 Replies)
I Need help for one requirement,
I want to move the latest/Older file in the folder to another file. File have the datetimestamp in postfix.
Example:
Source Directory : \a
destination Directory : \a\b
File1 : xy_MMDDYYYYHHMM.txt (xy_032120101456.txt)
File2: xy_MMDDYYYYHHMM.txt... (1 Reply)
Hi
I have a directory with two subdirectories and also have a code like below to search files modified in last 2 minutes.
# ls
hello080909.txt inbox outbox
# find . -type f -mmin +2
./inbox/hello2080909.txt
./outbox/hi0080909.txt
./hello080909.txt
The above code just searches and... (3 Replies)
I'm trying to remove several directories which contains sun-dirs and files inside.
I used the command rm -r <dirname>
But, it always ask "examine file in directory <dirname> yes/no?" line by line.
So, i need to write "y" for every line.
How can i skip this step and remove all directories with... (9 Replies)