Keeping ‘N’ Latest Files in a Folder
Last Updated on August 19, 2021 by Hammad Rauf
In BASH shell (Linux operating system or Windows 10 [See below] ) when you are running a server of some kind often you are faced with a situation where the log folder gets filled up with old copies of the log files. Some Server software provide tools for automatic removal of old log files, but at other times you may not be that lucky. I recently faced a similar situation but after some Web-searching and experimentation came up with a solution using a single line Bash Command. If your software does not have automatic roll over capability for log files, then probably you can put the following script in your cron-jobs or Task Scheduler.
The basic command is given below:
ls -t | tail -$( expr `ls -t | wc -l` - 4 ) | xargs rm -Rf
# The above command will keep 4 latest files, N = 4
Let us break it down in parts and study it for a while.
ls -t
This command lists out the file names in a folder by order of modified date, the newest files are shown first, then older files.
ls -t | wc -l
The ‘|’ character is the PIPE symbol, it passes output from the command on the left to the command on the right. The command on the right word-count with -l option, which counts the number of lines. So in all this command will count the number of files in the directory.
$( expr `ls -t | wc -l` - 4 )
The ‘$’ symbol has multiple uses. In this case it is being used to evaluate an expression (Expression as in arithmetic). Infact there are 2 expressions here. The inner Expression is marked by the BASH-keyword ‘expr’ and enclosed in back-tics characters (`). The inner expression is calculating the total number of files in the directory. The outer Expression is subtracting N (N=4 in our example). So the outer expression is calculating 4 less than the total number of files in the directory.
tail -$( expr `ls -t | wc -l` - 4 )
The tail command is often used as the right hand side in a PIPE command. Tail command displays the last part of a file or stream. The above tail command is actually “tail -X” where X can be any integer. It will show the last X lines of the File or input stream. So in our expression it will show the last part of the file or input stream, leaving aside the top 4 (N) latest files.
ls -t | tail -$( expr `ls -t | wc -l` - 4 )
The above command now makes more sense, as the Input Stream or file list is now included. “ls -t” on the left side of the | (PIPE) symbol is trying to list all files but then the right side of the Pipe “tail -$( expr ls -t | wc -l
– 4 )” is only tailing the last so many files, leaving the top 4 latest files. So this command is generating the list of files we want to delete, in the next step.
ls -t | tail -$( expr `ls -t | wc -l` - 4 ) | xargs rm -Rf
Now since we can list the names of the files we want to delete, let us PIPE in another command to delete them. “xargs rm -Rf” is doing exactly that. The actual delete files command is “rm -Rf”, which deletes all files forcing the delete with recursive options. “xargs” is a way in BASH shell to pass arguments to commands via a PIPE.
Changing Directories Before and After the Command
The command above can be modified to Change into a target directory, then delete all but 4 latest files, and then change back into home folder. The following command does exactly that.
cd /home/hammad/test && ls -t | tail -$( expr `ls -t | wc -l` - 4 ) | xargs rm -Rf && cd ~
How to Run it in Windows 10?
If you have enabled and installed “Windows Subsystem for Linux” then you can run the above and many other BASH commands in Windows 10 also. A good reference article link is given here and below in Further Reading section. Another link given in further reading section, Article 3 Link, shows how to access Windows folders from Linux Sub-system in Windows 10.
Further Reading
- How to Use Linux Bash Shell on Windows 10. Article 1 Link, Date accessed August 14, 2021.
- WC Command Examples to Count Number of Lines….. , Article 2 Link, Date accessed August 14, 2021.
- Access Windows files from Linux subsystem, Article 3 Link, Date accesses August 14, 2021.