LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-27-2020, 10:46 AM   #1
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,634

Rep: Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558
Listing recent files


A large amount of my ls use is for recent files/directories - either created same day, or within the past week. I figured I'd create a couple of aliases so I don't keep getting a screenful when I only want a handful.

The perfect solution would be if I could simply splice the -newer option from find into the ls command (retaining all of ls's functionality), but failing that I've been looking at other ways to achieve this.

To rule out the likely question: why not use -ls option to find? e.g. find -maxdepth 1 -newermt "7 days ago" -ls
The -ls option gives equivalent to "ls -dils" but I want "ls -Flptr --color" by default, plus the ability to use other functionality of ls as and when required, which isn't available with that method.

After a bunch of fiddling, I came up with this - it adds a tab-delimited timestamp before the actual time definition, which awk can filter/compare against a passed in timestamp, and then remove before printing the line:
Code:
alias lsweek="f(){ ls -Flptr --color --quoting-style=shell-escape --time-style='+%t%F%t%a %e %b %H:%M' \"\$@\" | awk -v lastweek=\$(date -d '7 days ago' +'%F') -F \$'\t' 'BEGIN{OFS=\"\"}{ if (lastweek<=\$2){\$2=\"\";print} }' ;};f "
I needed to add --quoting-style=shell-escape to ensure any filenames with newlines don't get misreported.

Here's a non-alias version of the same thing, with escaped line-breaks added for readability:
Code:
f(){
ls -Flptr --color --quoting-style=shell-escape --time-style='+%t%F%t%a %e %b %H:%M' "$@" \
| awk -v lastweek=$(date -d '7 days ago' +'%F') -F $'\t' \
  'BEGIN{OFS=""}{ if (lastweek<=$2){$2="";print} }'
};f
Two downsides:
* It requires output in long listing format to work.
* Colours needs to be manually turned off if sending to a file/etc.

Neither of those are major problems, but would still be nice to have fixes/workarounds.

Or point out if I've missed anything?

Maybe there's another less hacky way to achieve this?


Last edited by boughtonp; 02-27-2020 at 10:49 AM.
 
Old 02-27-2020, 10:56 AM   #2
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,697
Blog Entries: 19

Rep: Reputation: 4499Reputation: 4499Reputation: 4499Reputation: 4499Reputation: 4499Reputation: 4499Reputation: 4499Reputation: 4499Reputation: 4499Reputation: 4499Reputation: 4499
Couldn't you use find with a pipe to the ls command you want rather than using find's built-in ls options?
 
1 members found this post helpful.
Old 02-27-2020, 11:32 AM   #3
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,634

Original Poster
Rep: Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558
Quote:
Originally Posted by hazel View Post
Couldn't you use find with a pipe to the ls command you want rather than using find's built-in ls options?
You mean like this?
Code:
find -maxdepth 1 -newermt "7 days ago" | ls -Flptr
That just lists the full directory instead of the found files. (Not sure why - I'm sure I've done some variant of that before.)

This partially worked:
Code:
ls -Flptr $(find -maxdepth 1 -newermt "7 days ago")
But using it on a subdir includes the subdir name in the output, (and there were other problems with it).

I also tried passing ls into -exec which had similar issues to that.

 
Old 02-27-2020, 12:11 PM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,078

Rep: Reputation: 7364Reputation: 7364Reputation: 7364Reputation: 7364Reputation: 7364Reputation: 7364Reputation: 7364Reputation: 7364Reputation: 7364Reputation: 7364Reputation: 7364
find has a -ls switch too.
Also you can simply:
Code:
ls -lt <whatever> | awk ' filter on the (last X) day of the first entry '
 
Old 02-27-2020, 12:11 PM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,373
Blog Entries: 3

Rep: Reputation: 3771Reputation: 3771Reputation: 3771Reputation: 3771Reputation: 3771Reputation: 3771Reputation: 3771Reputation: 3771Reputation: 3771Reputation: 3771Reputation: 3771
It could be something more like this:

Code:
find . -type f -print0 | sort --zero-terminated | xargs --null ls -lth
Or if you have GNU find then

Code:
find . -maxdepth 1 -newermt "7 days ago" -printf '%CY%Cm%Cd\t%M\t%s\t%p\n'

Last edited by Turbocapitalist; 02-27-2020 at 12:16 PM.
 
2 members found this post helpful.
Old 02-27-2020, 12:26 PM   #6
ehartman
Senior Member
 
Registered: Jul 2007
Location: Delft, The Netherlands
Distribution: Slackware
Posts: 1,674

Rep: Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888
Quote:
Originally Posted by boughtonp View Post
You mean like this?
Code:
find -maxdepth 1 -newermt "7 days ago" | ls -Flptr
You have to use the "xargs" command in FRONT of the ls to let it pick up the filenames found by the find command. So something like
Code:
find ...|xargs ls -Flptr
and have to experiment how to strip the directory from the found filenames.
Should be something with "basename" but at the moment I don't see how to put xargs on both commands. Maybe use a script and run THAT through xargs.
 
Old 02-27-2020, 05:25 PM   #7
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,634

Original Poster
Rep: Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558
Quote:
Originally Posted by ehartman View Post
You have to use the "xargs" command in FRONT of the ls to let it pick up the filenames found by the find command.
Thanks, xargs is a command I always seem to forget about.

So...
Code:
find -mindepth 1 -maxdepth 1 -newermt "7 days ago" -print0 | xargs -0 ls -Flptr -color=auto
That makes colours automatic and allows any ls format, but has directory name included - if I strip it out before ls (e.g. using sed -z), it wont find the files - unless I do pushd/popd to temporarily switch to directory and then it's just removing ./ so ls still finds the file...

Code:
pushd subdir >/dev/null ; \
find -mindepth 1 -maxdepth 1 -newermt "7 days ago" -print0 \
  | sed -zE "s/(^'?)\.\//\1/" \
  | xargs -0 ls -Flptr --color=auto ; \
popd >/dev/null
Making that work for "lsweek subdir/*.txt" would require some logic to split it so the path goes to pushd and the filter is added to find - should be fairly simple, but I'll worry about that another time.


Last edited by boughtonp; 02-28-2020 at 06:38 AM. Reason: fix backreference
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Listing IP's Currently Banned By The Netfilter Recent Module Washington Ratso Linux - Networking 1 01-04-2017 01:20 PM
sftp ls command, short listing vs long listing slufoot80 Linux - General 3 02-05-2013 10:40 AM
Delete all .log files and .bat files except the most recent? alancampbell6 Linux - Newbie 3 12-15-2011 09:17 AM
puppy thoughts after having a recent look see -- given recent developments .. jonyo Puppy 0 11-29-2011 08:45 PM
listing files and burning files - just a question radiodee1 Debian 1 11-26-2006 07:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration