Remove all the subdirectories except latest 5 inside any given directory


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Remove all the subdirectories except latest 5 inside any given directory
# 1  
Old 01-29-2015
Computer Remove all the subdirectories except latest 5 inside any given directory

I Want to remove all the sub-directories except latest five in any given TGTDIR.
Is there a way to do so without making a cd to TGTDIR?
I have tried the following but not worked.
Thank you.

Code:
rm -rf `ls -t $TGTDIR | awk 'NR>5'`


Last edited by Devendra Hupri; 01-29-2015 at 01:32 AM.. Reason: edit description
# 2  
Old 01-29-2015
Hello Devendra Hupri,

Welcome to forum, following may help you in same.
1st command:
Code:
TGTDIR=/tmp ##For ust an example
ls -ltr $TGTDIR | awk '/^d/ {A++;X[++j]=$NF} END{for(i=1;i<=A-5;i++){print "rm -rf " X[i]}}'

It will only print the results, if happy with results you can use following command then.
2nd command:
Code:
TGTDIR=/tmp
ls -ltr $TGTDIR | awk '/^d/ {A++;X[++j]=$NF} END{for(i=1;i<=A-5;i++){print "rm -rf " X[i]}}' | sh

NOTE: Please test 1st suggestion and if happy then only follow the 2nd suggestion.

Thanks,
R. Singh

Last edited by RavinderSingh13; 01-29-2015 at 02:33 AM.. Reason: Added equal condition now
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 01-29-2015
Not tested, but maybe some form of the following might work ? Please try on a dummy set first..
Waiting on a second opinion

Code:
find /TGTDIR/ -maxdepth 1 -type -d -name '*' -print0 | xargs -r0 ls -t | tail -n +5 | tr '\n' '\0' | xargs -r0 rmdir

This User Gave Thanks to senhia83 For This Post:
# 4  
Old 01-29-2015
Thanks RavinderSingh13, The first code worked,
I had 7 subdirs, it only listed 1(which was old) it was expected to report 2 old dirs
# 5  
Old 01-29-2015
Quote:
Originally Posted by senhia83
Not tested, but maybe some form of the following might work ? Please try on a dummy set first..
Waiting on a second opinion
Code:
find /TGTDIR/ -maxdepth 1 -type -d -name '*' -print0 | xargs -r0 ls -t | tail -n +5 | tr '\n' '\0' | xargs -r0 rmdir

Hello Senhia,

I didn't test your code but rmdir will work only if directories are empty(User didn't mention here about the same either directories are empty or not).

Hello Devendra Hupri,

Could you please be more clear on your requirement and give us more details like do we need to check each directory's subdirectory also and perform same deletion operation there except 5 directories?

EDIT: Also Devendra, I have added <= condition now in solution POST#2, could you please try those and let me know if that helps.
Code:
ls -ltr $TGTDIR | awk '/^d/ {A++;X[++j]=$NF} END{for(i=1;i<=A-5;i++){print "rm -rf " X[i]}}'

Then if happy with above command:
Code:
TGTDIR=/tmp
ls -ltr $TGTDIR | awk '/^d/ {A++;X[++j]=$NF} END{for(i=1;i<=A-5;i++){print "rm -rf " X[i]}}' | sh

Thanks,
R. Singh

Last edited by RavinderSingh13; 01-29-2015 at 02:35 AM.. Reason: Added comment about solution now
This User Gave Thanks to RavinderSingh13 For This Post:
# 6  
Old 01-29-2015
Quote:
Originally Posted by Devendra Hupri
I Want to remove all the sub-directories except latest five in any given TGTDIR.
Is there a way to do so without making a cd to TGTDIR?
I have tried the following but not worked.
Thank you.

Code:
rm -rf `ls -t $TGTDIR | awk 'NR>5'`

What operating system and shell are you using?

In what way did it not work?

Are there any files other than directories in the directory named by $TGTDIR? Could non-directory files in $TGTDIR be related to why your command did not work?

Do any filenames in $TGTDIR contain any whitespace characters (i.e., <space>, <tab>, <newline>, <carriage-return>, or <form-feed>)?
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 01-29-2015
Hey Ravinder,

No need to check the dir contents, I just need to retain latest five removing the old ones.
The dirs are not empty. they contain some data.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Getting all the subdirectories inside directories in excel file

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)
Discussion started by: Little
4 Replies

2. Shell Programming and Scripting

Append string to all the files inside a directory excluding subdirectories and .zip files

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)
Discussion started by: Ravi Kishore
4 Replies

3. Shell Programming and Scripting

List files with *.i extension in a directory and all its subdirectories + 30days old then remove

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)
Discussion started by: lena keung
2 Replies

4. Shell Programming and Scripting

Compressing all directories inside a directory and remove the uncompressed version

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)
Discussion started by: dll_fpga
2 Replies

5. Shell Programming and Scripting

remove a whole directory tree WITH files inside?

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)
Discussion started by: pstein
3 Replies

6. Shell Programming and Scripting

I'm trying to remove all mp3's in subdirectories

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)
Discussion started by: binary-ninja
3 Replies

7. Shell Programming and Scripting

Copying subdirectories of a directory to some other directory and renaming them

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)
Discussion started by: sholay
8 Replies

8. Shell Programming and Scripting

Move the latest or older File from one directory to another Directory

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)
Discussion started by: pp_ayyanar
1 Replies

9. Shell Programming and Scripting

How to find files only inside the subdirectories only?

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)
Discussion started by: Tuxidow
3 Replies

10. UNIX for Dummies Questions & Answers

How to remove directory with subdirectories and files?

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)
Discussion started by: ppa108
9 Replies
Login or Register to Ask a Question