LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 09-16-2020, 06:59 AM   #1
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 839

Rep: Reputation: 40
Finding the pattern to ls


Im trying to find a pattern that allows me to list current log file and its latest rotated one.
So, what I want as an input is eg.
Code:
/var/log/syslog
/var/log/syslog.1
...and skip all remaining 2,3,4,5 and so on.
I was hoping that following would work but it doesnt
Code:
ls -al /var/log/syslog*[,1]
-rw-r----- 1 syslog adm 268915 Sep 16 08:06 /var/log/syslog.1
Any quick ideas here?
 
Old 09-16-2020, 07:04 AM   #2
boughtonp
Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 567

Rep: Reputation: 366Reputation: 366Reputation: 366Reputation: 366

I think you're asking for brace expansion - i.e. "ls filename{,.1}" will expand to "ls filename filename.1"

If you don't know that the last rotation is .1 you could also achieve this with a wildcard, sorted by date and using head/tail with -n2 to truncate.

 
2 members found this post helpful.
Old 09-16-2020, 08:44 AM   #3
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 839

Original Poster
Rep: Reputation: 40
Yes, that does the trick

Code:
ls -al /var/log/syslog{,.1}
-rw-r----- 1 syslog adm 171272 Sep 16 15:38 /var/log/syslog
-rw-r----- 1 syslog adm 268915 Sep 16 08:06 /var/log/syslog.1
 
Old 09-16-2020, 10:46 AM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,454

Rep: Reputation: 664Reputation: 664Reputation: 664Reputation: 664Reputation: 664Reputation: 664
The standard wildcard glob is not good enough for this.
But bash has extglob, an extended glob:
Code:
shopt -s extglob
ls -l /var/log/syslog?(.1)
Opposed to the brace expansion, the glob matches against existing files; not present files are omitted.
See the following exercise:
Code:
ls -l /var/log/syslog{,{1..9}}
ls -l /var/log/syslog?(.[1-9])
If no file matches then the glob keeps the given glob pattern, unless nullglob is set in addition.
The nullglob works well with a for loop, not with ls (that defaults to all files).

Last edited by MadeInGermany; 09-16-2020 at 10:51 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Adding (not replacing) a pattern match with a similar pattern? b-bri Linux - Newbie 2 08-31-2009 12:36 AM
substituting pattern only if another pattern is present rattlesnakejoe Programming 4 07-22-2009 07:46 PM
substitute a pattern only if some other pattern is present raghu123 Programming 3 06-26-2009 06:53 AM
printing pattern match and not whole line that matches pattern Avatar33 Programming 13 05-06-2009 06:17 AM
replace a text pattern with the reverse of another text pattern lothario Linux - Software 5 07-25-2008 02:43 PM

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

All times are GMT -5. The time now is 11:11 PM.

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