Best one-liner?

My vote for the best short insight in 2021 goes to @Mina
“The essence of programming is abstraction and algorithms. Languages are not important.”
Cant remember which post it was in.

Are there any other nominations… selfies excluded
Neville

1 Like

I feel honoured.

Thank you so much for the nomination! :smiling_face_with_three_hearts:

alice

The quote is from this thread:

I nominate:

1 Like

Yes the contribution nominated by @mina is a great one-liner with a delicate twist of meaning.

Can we get some more nominations please?

When I see “one liner” I think of a neat, elegant, single liner CLI that loops stuff and has a bunch of conditionals e.g. :
for S in $(cat list-of-servers.txt) ; do [[ -f flagfile ]] flag-file-action && 2nd-action || not-found-action ; done

I usually avoid the conditionals if I’m in a hurry - too hard to troubleshoot the logic when things go wrong :smiley: - I’m usually more like :
for S in $(cat list-of-servers.txt) ; do action 1; action2; action3; done

Basically because I’m too lazy to do the same thing manually across 35 Linux servers, and I’m too lazy to learn Ansible :smiley:

OK @daniel.m.tripp , lets see who can come up with other innovative CLI one-liners.
I tried to follow yours. I assume S is used somewhere in the action bits?
I seem to remember some convoluted awk one-liners for data manipulation. Will have to consult my archives.
Neville

Yikes - I submit to your superior wisdom mate :smiley: REGEX one liners? I’d probably give up on the 2nd or 3rd failure :smiley:

I really hate it when people stick illegal characters in filenames - so I have to do all sort of escape trickery just to rename them… I love the Debian / Ubunu “rename” command, it supports perl (or sed) style regex for rename files (entirely different rename command in Red Hat bast distros) - but sometimes the quoting or escaping gets the better of me and I have to suck it, and just “mv old-crappy-name new-lesscrappy-name” :smiley:

Dont know about the superior bit. These days I am gettting a bit rusty. I do still find grep useful. awk is in the past - we used to use it to reformat data files.
I dont suppose you have a neat way to get the spaces out of file names that come from Windows users? That bugs me more than the occasional strange character.
So is rename better than mv? I have always used mv. Showing my age again.
We are spoilt , you know. I once worked on an OS that had 6 character file names, capitals only, no extensions, no directory structure just the home space, and the only utilities were copy and dir. Yes it was a mainframe. Unix was a great advance on that.
Cheers
Neville

In Debian based distros :

sudo apt install rename

Will get you the “perl / cpan” rename utility - it’s actually a perl script really…

e.g. I unzip a zip file chock full of music files with ugly PITA chars - and - spaces :

╭─x@fenrir ~/MPZ/DrColossus/tingting  
╰─➤  ls -al                                                                                                                    2 ↵
total 100981
drwxrwxr-x 2 x 1001       10 Dec 29  2021  .
drwxrwxr-x 6 x 1001        8 Dec 29 22:23  ..
-rw-rw-r-- 1 x 1001  3024530 Dec 25 18:07  cover.jpg
-rw-rw-r-- 1 x 1001  8489160 Dec 25 18:07 "Dr. Colossus - I'm a Stupid Moron With an Ugly Face and A Big Butt and my Butt Smells and I Like to Kiss My Own Butt - 01 Sex Cauldron.mp3"
-rw-rw-r-- 1 x 1001 10120823 Dec 25 18:07 "Dr. Colossus - I'm a Stupid Moron With an Ugly Face and A Big Butt and my Butt Smells and I Like to Kiss My Own Butt - 02 Pickabar.mp3"
-rw-rw-r-- 1 x 1001 13454243 Dec 25 18:07 "Dr. Colossus - I'm a Stupid Moron With an Ugly Face and A Big Butt and my Butt Smells and I Like to Kiss My Own Butt - 03 Lard Lad.mp3"
-rw-rw-r-- 1 x 1001 13162442 Dec 25 18:07 "Dr. Colossus - I'm a Stupid Moron With an Ugly Face and A Big Butt and my Butt Smells and I Like to Kiss My Own Butt - 04 So Long Stinktown.mp3"
-rw-rw-r-- 1 x 1001 19365918 Dec 25 18:07 "Dr. Colossus - I'm a Stupid Moron With an Ugly Face and A Big Butt and my Butt Smells and I Like to Kiss My Own Butt - 05 Get Mendoza.mp3"
-rw-rw-r-- 1 x 1001 10616626 Dec 25 18:07 "Dr. Colossus - I'm a Stupid Moron With an Ugly Face and A Big Butt and my Butt Smells and I Like to Kiss My Own Butt - 06 Hummingbird of Bengal.mp3"
-rw-rw-r-- 1 x 1001 24575761 Dec 25 18:07 "Dr. Colossus - I'm a Stupid Moron With an Ugly Face and A Big Butt and my Butt Smells and I Like to Kiss My Own Butt - 07 Space Coyote.mp3"

The first thing to do, before I process anything else - is get rid of the single apostophe on the word “I’m” …

╭─x@fenrir ~/MPZ/DrColossus/tingting  
╰─➤  rename "s/\'//" 
╭─x@fenrir ~/MPZ/DrColossus/tingting  
╰─➤  ls -al
total 100981
drwxrwxr-x 2 x 1001       10 Dec 29  2021  .
drwxrwxr-x 6 x 1001        8 Dec 29 22:23  ..
-rw-rw-r-- 1 x 1001  3024530 Dec 25 18:07  cover.jpg
-rw-rw-r-- 1 x 1001  8489160 Dec 25 18:07 'Dr. Colossus - Im a Stupid Moron With an Ugly Face and A Big Butt and my Butt Smells and I Like to Kiss My Own Butt - 01 Sex Cauldron.mp3'
-rw-rw-r-- 1 x 1001 10120823 Dec 25 18:07 'Dr. Colossus - Im a Stupid Moron With an Ugly Face and A Big Butt and my Butt Smells and I Like to Kiss My Own Butt - 02 Pickabar.mp3'
-rw-rw-r-- 1 x 1001 13454243 Dec 25 18:07 'Dr. Colossus - Im a Stupid Moron With an Ugly Face and A Big Butt and my Butt Smells and I Like to Kiss My Own Butt - 03 Lard Lad.mp3'
-rw-rw-r-- 1 x 1001 13162442 Dec 25 18:07 'Dr. Colossus - Im a Stupid Moron With an Ugly Face and A Big Butt and my Butt Smells and I Like to Kiss My Own Butt - 04 So Long Stinktown.mp3'
-rw-rw-r-- 1 x 1001 19365918 Dec 25 18:07 'Dr. Colossus - Im a Stupid Moron With an Ugly Face and A Big Butt and my Butt Smells and I Like to Kiss My Own Butt - 05 Get Mendoza.mp3'
-rw-rw-r-- 1 x 1001 10616626 Dec 25 18:07 'Dr. Colossus - Im a Stupid Moron With an Ugly Face and A Big Butt and my Butt Smells and I Like to Kiss My Own Butt - 06 Hummingbird of Bengal.mp3'
-rw-rw-r-- 1 x 1001 24575761 Dec 25 18:07 'Dr. Colossus - Im a Stupid Moron With an Ugly Face and A Big Butt and my Butt Smells and I Like to Kiss My Own Butt - 07 Space Coyote.mp3'

So - now there’s a whole bunch of redundant data here - the id3 V1 and V2 tags are filled with the artist name, the files live in an “artist” sub-folder called “DrColossus” already… And I already know the album name (e.g. it’s also in the id3 V1 and V2 tags) :
Note per above - I use zsh and oh-my-zsh and CLI completion is smart enough so I can just type :

rename Dr<TAB>

And then I can edit the zsh completion and put single quotes and sed style slashes in … too easy…

╭─x@fenrir ~/MPZ/DrColossus/tingting  
╰─➤  rename 's/Dr.\ Colossus\ -\ Im\ a\ Stupid\ Moron\ With\ an\ Ugly\ Face\ and\ A\ Big\ Butt\ and\ my\ Butt\ Smells\ and\ I\ Like\ to\ Kiss\ My\ Own\ Butt\ -\ //' *
╭─x@fenrir ~/MPZ/DrColossus/tingting  
╰─➤  ls -al
total 100981
drwxrwxr-x 2 x 1001       10 Dec 29  2021  .
drwxrwxr-x 6 x 1001        8 Dec 29 22:23  ..
-rw-rw-r-- 1 x 1001  8489160 Dec 25 18:07 '01 Sex Cauldron.mp3'
-rw-rw-r-- 1 x 1001 10120823 Dec 25 18:07 '02 Pickabar.mp3'
-rw-rw-r-- 1 x 1001 13454243 Dec 25 18:07 '03 Lard Lad.mp3'
-rw-rw-r-- 1 x 1001 13162442 Dec 25 18:07 '04 So Long Stinktown.mp3'
-rw-rw-r-- 1 x 1001 19365918 Dec 25 18:07 '05 Get Mendoza.mp3'
-rw-rw-r-- 1 x 1001 10616626 Dec 25 18:07 '06 Hummingbird of Bengal.mp3'
-rw-rw-r-- 1 x 1001 24575761 Dec 25 18:07 '07 Space Coyote.mp3'
-rw-rw-r-- 1 x 1001  3024530 Dec 25 18:07  cover.jpg

Replace the first space in each string with a hyphen (I could even leave them this way) :

╭─x@fenrir ~/MPZ/DrColossus/tingting  
╰─➤  rename 's/\ /-/' *                                                                                                                                              130 ↵
╭─x@fenrir ~/MPZ/DrColossus/tingting  
╰─➤  ls -al
total 100981
drwxrwxr-x 2 x 1001       10 Dec 29  2021  .
drwxrwxr-x 6 x 1001        8 Dec 29 22:23  ..
-rw-rw-r-- 1 x 1001  8489160 Dec 25 18:07 '01-Sex Cauldron.mp3'
-rw-rw-r-- 1 x 1001 10120823 Dec 25 18:07  02-Pickabar.mp3
-rw-rw-r-- 1 x 1001 13454243 Dec 25 18:07 '03-Lard Lad.mp3'
-rw-rw-r-- 1 x 1001 13162442 Dec 25 18:07 '04-So Long Stinktown.mp3'
-rw-rw-r-- 1 x 1001 19365918 Dec 25 18:07 '05-Get Mendoza.mp3'
-rw-rw-r-- 1 x 1001 10616626 Dec 25 18:07 '06-Hummingbird of Bengal.mp3'
-rw-rw-r-- 1 x 1001 24575761 Dec 25 18:07 '07-Space Coyote.mp3'
-rw-rw-r-- 1 x 1001  3024530 Dec 25 18:07  cover.jpg

Thankfully these files are ready to go to “CamelCase” format - if I wanted to remove the spaces (normally I would wouldn’t bother removing those spaces - but sometimes I get the hump with spaces in filenames) :

Because it’s a regex, I can use “g” at the end to GLOBALLY replace (remove) ALL spaces

╭─x@fenrir ~/MPZ/DrColossus/tingting  
╰─➤  rename 's/\ //g' *
╭─x@fenrir ~/MPZ/DrColossus/tingting  
╰─➤  ls -al            
total 100981
drwxrwxr-x 2 x 1001       10 Dec 29  2021 .
drwxrwxr-x 6 x 1001        8 Dec 29 22:23 ..
-rw-rw-r-- 1 x 1001  8489160 Dec 25 18:07 01-SexCauldron.mp3
-rw-rw-r-- 1 x 1001 10120823 Dec 25 18:07 02-Pickabar.mp3
-rw-rw-r-- 1 x 1001 13454243 Dec 25 18:07 03-LardLad.mp3
-rw-rw-r-- 1 x 1001 13162442 Dec 25 18:07 04-SoLongStinktown.mp3
-rw-rw-r-- 1 x 1001 19365918 Dec 25 18:07 05-GetMendoza.mp3
-rw-rw-r-- 1 x 1001 10616626 Dec 25 18:07 06-HummingbirdofBengal.mp3
-rw-rw-r-- 1 x 1001 24575761 Dec 25 18:07 07-SpaceCoyote.mp3
-rw-rw-r-- 1 x 1001  3024530 Dec 25 18:07 cover.jpg

This is a Melbourne based stoner / doom metal band who base ALL their lyrical themes around The Simpsons :

It’s nearly 4 years since I installed this on a Red Hat based distro, I think I did it via using cpan, and then moved /usr/bin/rename to something else, then linked /usr/bin/rename to the CPAN perl script…

I think I did it this way :

yum install perl-App-cpanminus -y
cpanm File::Rename

Thank you @daniel.m.tripp
I will install rename and have a play. Can do sed but never used perl.
Might be simple to write a C program to filter a filename?
Neville

Back to the original topic - one-liners
We have 3 worthy nominations

  1. a nice piece of computing wisdom from @Mina
  2. a clever humouous twist from @Akito
  3. a powerful script from @daniel.m.tripp
    They are 3 very different things. Can we put them together?

Here is a real challenge
Can someone cleverer than me write a linux shell script which does something useful, and displays some point of computing wisdom, and has a humerous twist?
Here is a simple example
2 * b || !(2*b)
Not mine it went around the R community a couple of years ago
It evaluates to TRUE as long as b has a value
It lacks the humour bit

Just to illustrate what I mean by a twist, here is a quote from G K Chesterton, who was a master at such things
“… nothing grows old so quickly as what is new”
Chesterton was talking about fashions, he knew nothing of fashions in computer software, but I think his quote applies.

Best wishes to all for New Year, and I hope someone enjoys the challenge
Neville

it’s the last day of 2021, she’s on her last legs, and good riddance to the old dame, she grew old real quick :smiley:
I just bought an album tonight (Digital, from bandcamp in FLAC), from 2008, a gifted blues / hard rock artist from Houma in LA (Louisiana) - Dax Riggs… And for the life of me - I couldn’t remember how The “F” I removed the superalative single quote from “I’m” in yet another bunch of filenames… I’m lazy - I came back here and had a gander - and then lightbulb moment “oh shit yeah - that’s right!” :smiley: :
On my Macbook :

╭─x@methone.local /Volumes/BARGEARSE/MPZ/RiggsDax/2008-If This is Hell Then Im Lucky-FLAC
╰─➤  rename "s/\'//" *                                                                                                                            130 ↵
╭─x@methone.local /Volumes/BARGEARSE/MPZ/RiggsDax/2008-If This is Hell Then Im Lucky-FLAC
╰─➤  ls -al
total 733481
drwxrwxr-x  2 x   family        13 31 Dec  2021 .
drwxrwxr-x  6 xx  family         7 31 Dec 19:43 ..
-rw-r--r--  1 x   family  42483847 31 Dec 19:37 Dax Riggs - If This is Hell Then Im Lucky - 01 Strange Television.flac
-rw-r--r--  1 x   family  31828660 31 Dec 19:37 Dax Riggs - If This is Hell Then Im Lucky - 02 Waking Up Insane.flac
-rw-r--r--  1 x   family  27459187 31 Dec 19:37 Dax Riggs - If This is Hell Then Im Lucky - 03 Song With No Name.flac
-rw-r--r--  1 x   family  22354385 31 Dec 19:37 Dax Riggs - If This is Hell Then Im Lucky - 04 Graves Beyond Windows.flac
-rw-r--r--  1 x   family  51531923 31 Dec 19:37 Dax Riggs - If This is Hell Then Im Lucky - 05 Otherworldly Dreamer.flac
-rw-r--r--  1 x   family  34618766 31 Dec 19:37 Dax Riggs - If This is Hell Then Im Lucky - 06 Like the Dead Would Laugh.flac
-rw-r--r--  1 x   family  30538373 31 Dec 19:37 Dax Riggs - If This is Hell Then Im Lucky - 07 High Monster.flac
-rw-r--r--  1 x   family  54564745 31 Dec 19:37 Dax Riggs - If This is Hell Then Im Lucky - 08 Heart of Green.flac
-rw-r--r--  1 x   family  26601712 31 Dec 19:37 Dax Riggs - If This is Hell Then Im Lucky - 09 Barefoot in the Dark.flac
-rw-r--r--  1 x   family  52475250 31 Dec 19:37 Dax Riggs - If This is Hell Then Im Lucky - 10 Theme.flac
-rw-r--r--  1 x   family   1081446 31 Dec 19:37 cover.jpg
╭─x@methone.local /Volumes/BARGEARSE/MPZ/RiggsDax/2008-If This is Hell Then Im Lucky-FLAC
╰─➤  rename 's/Dax\ Riggs\ -\ If\ This\ is\ Hell\ Then\ Im\ Lucky\ -\ //' *
╭─x@methone.local /Volumes/BARGEARSE/MPZ/RiggsDax/2008-If This is Hell Then Im Lucky-FLAC
╰─➤  ls -al
total 733481
drwxrwxr-x  2 x   family        13 31 Dec  2021 .
drwxrwxr-x  6 xx  family         7 31 Dec 19:43 ..
-rw-r--r--  1 x   family  42483847 31 Dec 19:37 01 Strange Television.flac
-rw-r--r--  1 x   family  31828660 31 Dec 19:37 02 Waking Up Insane.flac
-rw-r--r--  1 x   family  27459187 31 Dec 19:37 03 Song With No Name.flac
-rw-r--r--  1 x   family  22354385 31 Dec 19:37 04 Graves Beyond Windows.flac
-rw-r--r--  1 x   family  51531923 31 Dec 19:37 05 Otherworldly Dreamer.flac
-rw-r--r--  1 x   family  34618766 31 Dec 19:37 06 Like the Dead Would Laugh.flac
-rw-r--r--  1 x   family  30538373 31 Dec 19:37 07 High Monster.flac
-rw-r--r--  1 x   family  54564745 31 Dec 19:37 08 Heart of Green.flac
-rw-r--r--  1 x   family  26601712 31 Dec 19:37 09 Barefoot in the Dark.flac
-rw-r--r--  1 x   family  52475250 31 Dec 19:37 10 Theme.flac
-rw-r--r--  1 x   family   1081446 31 Dec 19:37 cover.jpg

In nearly ALL cases, it pays to scrape off shitty characters, before attempting to replace big strings…

Hi @daniel.m.tripp ,
I tried rename script. It works fine for me.
Just for interest here is how we would have gone about it 40 years ago. The environment then was a minicomputer running Berkeley Unix ( BSD), an ascii terminal, just a CLI, not windows, no copy/paste, no apps, just a few unix utilities. The utilities are not up to it so we have to make one. So lets start with a small C program to turn one filename into a clean filename removing blanks and specials.
So here is nbs.c
/* nbs.c - remove blanks and special chars from a string */

#include <stdio.h>
#include <string.h>
#include <ctype.h>

main (int argc, char *argv[])
{
int i,j,c;
char *s;
for(i = 1; i<argc; i++){
s = argv[i];
screen(s);
printf("%s",s);
}
}

void screen(char s[])
/* remove blanks and special chars from s */
{
int i,j;
for(i = j = 0; s[i] != ‘\0’; i++)
if(s[i] != ’ ’ && isalnum(s[i]))
s[j++] = s[i];
s[j] = ‘\0’;
}
It just leaves out all unwanted characters.
Compile it
gcc nbs.c -o nbs
and put it somewhere in your PATH eg
cp nbs ~/bin

So now we have a traditional unix utility
To use it over many files we need a script, the choice is sh or csh. With sh we do
#! /bin/sh
for i in * ; do
echo $i
echo nbs $i
mv “$i” “nbs $i
done
and lets call it nbs.sh, and make it executable.
I have a small test directory
nevj@mary:/common/Cwork/test$ ls
‘abc def’ asd#fgr ’ tes& < t blan^k’
so from in there we run
nevj@mary:/common/Cwork/test$ …/nbs.sh
tes& < t blan^k
testblank
abc def
abcdef
asd#fgr
asdfgr
nevj@mary:/common/Cwork/test$ ls
abcdef asdfgr testblank

So it works.
Not the best way today, but a brief view of where we came from.

Regards
Neville

1 Like

Just been trying to rip an audio CD to add to my digital music collection - what worked before, didn’t work today… DANG!

i.e. “abcde -o flac” (to rip output into flac files in $PWD) rewarded me with “insufficient or missing discinfo”…

So I google-fu’d the shit out of that error message and came here :

https://abcde.einval.com/bugzilla/show_bug.cgi?id=100

I found that line, replaced “5” with “4” and it worked… DAMN! that page is 3 years old and it’s still broken???

So - now I’m ripping my audio CD to FLAC files… (actually - it seems it’s one big file - it’s not quite “Opera” - i.e. true opera buffs would turn their snooty noses up - but - it’s a London West End recording of Lloyd-Webber’s “Phantom of the Opera” with Michael Crawford and Sarah Brightman)… My missus and I used to play this when our children were very small after seeing an Australian prodution of this (with Aussie versions of Crawford and Brightman, Warlow and maybe Marina Prior?)… I kinda hate Lloyd-Webber, but I love the early 1970’s recording of the Broadway production of JCSS, and I quite liked Julie Covington’s interpretation of “Don’t Cry for me Argentina” (but everytime I think of that I get sad, 'cause it makes me think of Tim Brooke-Taylor singing “Don’t Cry For Me Marge and Tina” and I remember Tim was one of the most famous early casualities of Covid 19 in 2020, and how angry it made me that Covid took TBT but let Bojo reccver!)…

CRAP! my external USB 2 DVD drive is making AWFUL noises and spitting out ugly SCSI messages…

scsi_read error: sector=208265 length=6 retry=2
                 Sense key: 3 ASC: 10 ASCQ: 0
                 Transport error: Medium reading data from medium
                 System error: Input/output error

I don’t think this is going to end in success… I do have another USB 2 DVD / CD-ROM burner / reader… I also have a SATA 5.25" blueray device - but I’d rather not go to that extreme (I prefer to have the least amount of moving parts in my computer as possible) - my favourite thing about my current desktop machine is that the ONLY moving parts are the case and CPU and GPU fans… I did look around for external USB 2 / 3 5.25" enclosures (for SATA) - but - it’s actually cheaper to just BUY a whole new “thing”…

1 Like

completely (and utterly) rebooted my PC, which I hate doing, and usually don’t do, for weeks at a time…

Same difference, different USB 2 DVD drive ripping an audio CD… taking forever - gets to around 500 MB then starts whining and sooking :

 (== PROGRESS == [                        ++>   | 212620 00 ] == :-0 o ==)   scsi_read error: sector=213043 length=27 retry=0
                 Sense key: 3 ASC: 11 ASCQ: 0
                 Transport error: Medium reading data from medium
                 System error: Input/output error

The day that the font of all human knowledge is stored on random access solid state media in hi-res pseudo-analog can’t come soon enough for me (I’m perfectly fine with quantum computing AI filling in the missing detail)… but I doubt I’ll live long enough to see that :smiley:

1 Like