The simplest solution without any tools is to enable globstar
in ksh and let ls
sorts by itself
set -o globstar
ls -dlrt /home/sk/ostechnix/** | head -5
I don't know how POSIX compliant this is but I believe it works in all other common POSIX OSes nowadays, not only AIX. If you have files with new lines in the names then it won't work correctly and you'll need to remove | head -5
**
in this snippet will expand to all files and folders in the specified path. -d
will prevent folder contents from being expanded; and -rt
will sort all the entries in the argument, oldest first. But this includes directories in the list so if you want to get oldest files only use this
ls -dlrt /home/sk/ostechnix/** | egrep -v '^d' | head -5
Also it won't work for hidden files. It's much easier in zsh where you can control the globbing at call site, but in ksh you'll need to do this
dirpath=/home/sk/ostechnix
# Get all files and folders
ls -dlrt "$dirpath"/** "$dirpath"/**/.* | egrep -v '/\.?\.$' | head -5
# Get only files
ls -dlrt "$dirpath"/** "$dirpath"/**/.* | egrep -v '^d' | head -5
The former command has a tiny downside: the .
and ..
entry of $dirpath
will be shown if you run without a path before **
like ls -dlrt ** **/.* | egrep -v '/\.?\.$'
. It can be filtered out with grep
ls -dlrt ** **/.* | egrep -v '/\.?\.$| \.?\.$'
# Or
ls -dlrt ** **/.* | egrep -v -e '/\.?\.$' -e ' \.?\.$'
# Or
ls -dlrt ** **/.* | egrep -v -e '/\.$' -e '/\.\.$' -e ' \.\.$' -e ' \.$'
But as always, parsing ls -l
output is not reliable so there'll be some files with special names which will be matched unexpectedly
You can remove -l
if you don't want the long format but to filter out folders and get files only you'll need to use a different way
ls -dprt /home/sk/ostechnix/** | egrep -v '/$' | head -5
Or ls -dprt ** **/.* | egrep -v '/$' | head -5
to get hidden files
AIX also includes csh which seems to also support globstar
. If you have bash you can enable the same option with shopt -s globstar
and in zsh you can use GLOB_STAR_SHORT with setopt globstarshort
or set per-glob globbing options. Similarly in tcsh use set globstar
For more details about globstar in each shell please read The result of ls *
, ls **
and ls ***