Would like to delete all files in a directory except the 7 latest ones
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
is there a way to give that negated list to the rm command?
or should i be using the find command with an exec {}?
Thank YOu
Here's a way:
Code:
#
# Leave 7 session files
#
# Specify the target directory and file names to operate on.
target_files=/home/barrie/.nautilus/saved-session-*
# Calculate the total number of files matching the target criteria.
total_files=$(ls -t1 $target_files | wc --lines)
# Specify the number of files to retain.
retained_files=7
# If there are surplus files, delete them.
if [ $total_files -gt $retained_files ]
then
rm $(ls -t1 $target_files | tail --lines=$((total_files-retained_files)))
fi
I don't think your rm line will work right. You should probably use xargs with the -d '\n' option. That will help when dealing with spaces in file names.
Additionally, directories included in $target_files cause things to go screwy. We don't know for sure that he's dealing with session information or log files yet. He might have errant subdirectories that need avoiding.
I suggest something closer to this:
Code:
#
# Leave 7 newest files
#
# Specify the target directory and file names to operate on.
target_files=~/target_dir/*
#remove directories(etc) from target
target_files=$(ls -t1d --file-type $target_files | grep -v '[/=>@|]$') #there's probably a better way
# Calculate the total number of files matching the target criteria.
total_files=$(ls -t1 $target_files | wc --lines)
# Specify the number of files to retain.
retained_files=7
# If there are surplus files, delete them.
if [ $total_files -gt $retained_files ]
then
ls -t1d $target_files | tail --lines=$((total_files-retained_files)) | xargs -d '\n' rm
fi
In regards to the 7 latest files are they generated any way such that find can be used? Only asking because find may have fewer CLI contortions than using ls.
I haven't read every word of every post in this thread, but wanted to point the OP to this other thread: http://www.linuxquestions.org/questi...-files-839315/
where it appears that the OP there wanted to do the same (or a very similar) thing. Maybe you can learn something there that will help, and apply it here.
Last edited by GrapefruiTgirl; 12-04-2010 at 10:08 AM.
If you're referring to my little script - well, I suppose you should like it a little bit at least- it exists thanks to some code that you posted priorly somewhere, using similar `find -printf` functionality. So, thank you!
Thank you all very much for this....Just a follow up question.
Code:
tail +10 Output every line from line 10 onwards.
This is true in UNIX (on Solaris, korn shell), how would i achieve the same functionality in Linux bash?
If might be alot simpler to sort the list of files by time (ls -1t) and then use the tail +7 command to show all lines that are not the first 7 and do manipulation (rm) on those lines. Please correct me if i wrong.
Thank You
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.