Post your Command line tricks

Forum rules
Share your brain ;)
User avatar
rhowaldt
Dog
Posts: 4565
Joined: Wed Oct 17, 2012 9:01 am
Contact:

Re: Post your Command line tricks

Unread post by rhowaldt » Tue Nov 19, 2013 12:22 pm

was looking for a simple way to save my MP3 files (titles) to a textfile. i tried

Code: Select all

ls *mp3 > mp3.txt
but since my ls is set in my .bashrc to default to colors, that would include all the color escapecodes and make a right mess. so, looking through the ls manpage, i found you can undo that like so:

Code: Select all

ls --color=never *mp3 > mp3.txt
and i ended up with a nice list to share with people or as a reminder for when you are at somebody's place and trying to remember that one song you loved but you are too stoned to do so :)
All statements are true in some sense, false in some sense, meaningless in some sense, true and false in some sense, true and meaningless in some sense, false and meaningless in some sense, and true and false and meaningless in some sense.

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: Post your Command line tricks

Unread post by machinebacon » Tue Nov 19, 2013 6:33 pm

env -i ls *mp3 > mp3.txt

:D just a guess
..gnutella..

User avatar
rhowaldt
Dog
Posts: 4565
Joined: Wed Oct 17, 2012 9:01 am
Contact:

Re: Post your Command line tricks

Unread post by rhowaldt » Tue Nov 19, 2013 8:19 pm

^ damn that's good. didn't know that one. although of course your solution is more bloated :P
All statements are true in some sense, false in some sense, meaningless in some sense, true and false in some sense, true and meaningless in some sense, false and meaningless in some sense, and true and false and meaningless in some sense.

User avatar
wuxmedia
Grasshopper
Posts: 6454
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: Post your Command line tricks

Unread post by wuxmedia » Tue Nov 19, 2013 9:35 pm

^ WTF? Are all your mp3's loose in one folder? OCD my ass.
I just get an error.
You want bloat?

Code: Select all

find | cut -c 3- | env -i egrep '\.(mpc|mp4|mp3|flac)$' > mp3.txt
Yeah I couldn't get regex to play nice in find -shock!

EDIT added quote, to make it work 8)
"Seek, and Ye shall find"
"Github | Chooons | Site"

User avatar
rhowaldt
Dog
Posts: 4565
Joined: Wed Oct 17, 2012 9:01 am
Contact:

Re: Post your Command line tricks

Unread post by rhowaldt » Tue Nov 19, 2013 9:50 pm

^ nope, they aren't. in fact, the *mp3 is inaccurate for my needs. my mp3s are in directories inside directories inside directories and so forth. ls * works recursively but only one directory down, so i might need something like your solution for finding *every* mp3. in my use-case however i just wanted one directory :)
All statements are true in some sense, false in some sense, meaningless in some sense, true and false in some sense, true and meaningless in some sense, false and meaningless in some sense, and true and false and meaningless in some sense.

User avatar
wuxmedia
Grasshopper
Posts: 6454
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: Post your Command line tricks

Unread post by wuxmedia » Tue Nov 19, 2013 9:56 pm

that's what I thought. good idea, to have a list.
I think the above goes as recursive as it can. plus it prints the path too.
"Seek, and Ye shall find"
"Github | Chooons | Site"

User avatar
GekkoP
Emacs Sancho Panza
Posts: 5878
Joined: Tue Sep 03, 2013 7:05 am

Re: Post your Command line tricks

Unread post by GekkoP » Tue Nov 19, 2013 10:05 pm

^ doesn't it lack a quote at the end of egrep? :)

Code: Select all

find | cut -c 3- | env -i egrep '\.(mpc|mp4|mp3|flac)$' > mp3.txt

pidsley
Hermit
Posts: 2539
Joined: Wed Oct 17, 2012 12:31 pm

Re: Post your Command line tricks

Unread post by pidsley » Tue Nov 19, 2013 10:36 pm

um, guys, I have ls defined like this in my .zshrc (and .bashrc)

Code: Select all

alias ls='ls -F --color=auto --group-directories-first'
and I get colored output in a terminal, but I don't see any escape characters in the file if I do "ls > file".

from the ls man page:

Code: Select all

With --color=auto, ls emits color codes only when standard output is connected to a terminal.
So no escape codes if you redirect stdout to a file.

Another handy trick for things like this is the "command" builtin. From "help command":

Code: Select all

 Runs COMMAND with ARGS suppressing  shell function lookup, or display
    information about the specified COMMANDs.  Can be used to invoke commands
    on disk when a function with the same name exists.
So, for example, if you have "cp" aliased to run "cp -v" and then you decide you in fact want to run cp in non-verbose mode, you can use "command cp" to run the native cp without invoking your alias.

Finally, if you get really stuck and need a plain command (like somehow you broke coreutils or the path and nothing works) don't forget that busybox provides simple versions of most coreutils. So this works

Code: Select all

busybox ls
See "man busybox" for all the applets provided in this useful package. It even includes some networking stuff like ifconfig, ip, and ping.

User avatar
wuxmedia
Grasshopper
Posts: 6454
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: Post your Command line tricks

Unread post by wuxmedia » Tue Nov 19, 2013 11:31 pm

@ gekko, yes bad copypasta 8(
thanks Pids, more eyeopeners, as usual 8)
so, theoretically, perhaps not very practically, the busybox package could be used instead of coreutils?
"Seek, and Ye shall find"
"Github | Chooons | Site"

pidsley
Hermit
Posts: 2539
Joined: Wed Oct 17, 2012 12:31 pm

Re: Post your Command line tricks

Unread post by pidsley » Wed Nov 20, 2013 12:04 am

wuxmedia wrote: so, theoretically, perhaps not very practically, the busybox package could be used instead of coreutils?
Yes. If you look at "man busybox" you can see the large list of what is provided, and some tips for how to use the package instead of coreutils. You can either alias everything or use symbolic links from the commands to busybox. In fact, if you look at /bin/ash on your Debian system, you will probably see that it is a symlink to busybox.

This could come in handy if, for example, you forgot to install the "iproute" package and find that you need the "ip" command to set up your network so you can install the iproute package. You can use "busybox ip" instead.

Maybe one of these days I will set up a system without coreutils. I could just remove them from the system I already have that uses busybox as init.

User avatar
wuxmedia
Grasshopper
Posts: 6454
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: Post your Command line tricks

Unread post by wuxmedia » Wed Nov 20, 2013 12:50 am

you're getting me excited Pids, because my addled brain has understood your clear description.
Good to see Oppenheimer back too. I bet you look like him too. 8)
"Seek, and Ye shall find"
"Github | Chooons | Site"

User avatar
GekkoP
Emacs Sancho Panza
Posts: 5878
Joined: Tue Sep 03, 2013 7:05 am

Re: Post your Command line tricks

Unread post by GekkoP » Wed Nov 20, 2013 9:59 am

Interesting stuff, pidsley, as usual. I had a look at busybox but never tried using it instead of coreutils.

User avatar
rhowaldt
Dog
Posts: 4565
Joined: Wed Oct 17, 2012 9:01 am
Contact:

Re: Post your Command line tricks

Unread post by rhowaldt » Wed Nov 20, 2013 12:12 pm

@pidsley: thanks for that. i just found out where the problem was. this was in my ~/.bashrc:

Code: Select all

alias ls="ls -F --color --group-directories-first"
changed it to

Code: Select all

alias ls="ls -F --color="auto" --group-directories-first"
and indeed that fixed it. nice 'auto' :)
All statements are true in some sense, false in some sense, meaningless in some sense, true and false in some sense, true and meaningless in some sense, false and meaningless in some sense, and true and false and meaningless in some sense.

User avatar
GekkoP
Emacs Sancho Panza
Posts: 5878
Joined: Tue Sep 03, 2013 7:05 am

Re: Post your Command line tricks

Unread post by GekkoP » Tue Nov 26, 2013 7:39 pm

Cleaning.

Code: Select all

find ~/ -name "*~" -exec rm -i {} \;
or

Code: Select all

find ~/ -name ".*~" -exec rm -i {} \;

pidsley
Hermit
Posts: 2539
Joined: Wed Oct 17, 2012 12:31 pm

Re: Post your Command line tricks

Unread post by pidsley » Wed Nov 27, 2013 2:54 am

"filename: syntax error line 27"

print just line 27:

Code: Select all

sed -n 27p filename
print lines 26 through 28:

Code: Select all

head -28 filename | tail -3

User avatar
DebianJoe
Frame Buffer
Posts: 1915
Joined: Mon Jul 01, 2013 5:41 am
Location: emacs.d

Re: Post your Command line tricks

Unread post by DebianJoe » Wed Nov 27, 2013 7:43 am

Keeping with the "line numbering" theme, let's say I want to find the line numbers of a regex in a file:

Code: Select all

grep -on "^static" file.c
spits out a list of every line number in file.c that starts with the word 'static'. Removing the 'o' switch, provides the full line text along with the numbers.
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

User avatar
johnraff
Sperminator
Posts: 199
Joined: Wed Oct 17, 2012 6:38 pm
Location: Japan
Contact:

Re: Post your Command line tricks

Unread post by johnraff » Thu Nov 28, 2013 6:30 pm

wuxmedia wrote:

Code: Select all

find | cut -c 3- | env -i egrep '\.(mpc|mp4|mp3|flac)$' > mp3.txt
Yeah I couldn't get regex to play nice in find -shock!
Hmm... felt obliged to come to find's defence... how about:

Code: Select all

find . -iregex '.*\.\(mpc\|mp4\|mp3\|flac\)$' -printf '%P\n' > mp3.txt
Have to escape the ( and | but saves some pipes.
All code is one.

User avatar
wuxmedia
Grasshopper
Posts: 6454
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: Post your Command line tricks

Unread post by wuxmedia » Thu Nov 28, 2013 6:39 pm

Thanks - you are an honourable man John. 8)
"Seek, and Ye shall find"
"Github | Chooons | Site"

User avatar
xaos52
The Good Doctor
Posts: 190
Joined: Thu Aug 15, 2013 11:59 am
Location: Eernegem, Belgium

Re: Post your Command line tricks

Unread post by xaos52 » Thu Dec 05, 2013 2:45 pm

You think grep is fast?
Try package 'silversearcher-ag' with command 'ag' and think again....
Connected. Take this REPL, brother, and may it serve you well.

User avatar
GekkoP
Emacs Sancho Panza
Posts: 5878
Joined: Tue Sep 03, 2013 7:05 am

Re: Post your Command line tricks

Unread post by GekkoP » Thu Dec 05, 2013 5:44 pm

^ never heard of it, thanks

Post Reply