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

Essex Web Design

September 3, 2010
A lot of contract providers give you free internet usage now, but if you have Pay As You Go, then you are going to be paying heavy prices.
Calling time on mobile internet nonsense?

Krasochka

September 2, 2010
Hack again?!
Adding more terminals to your function keys

GenryFlorist

September 2, 2010
<b>Cheap flowers delivery around the world!</b> Celebrate summer with our gorgeous flowers. They?re the perfect gift for any summer occasion. From birthdays to anniversaries, we offer beautiful flowers, lush plants, ...
Burning an iso to CD on Windows

auto-financing.co.cc

September 2, 2010
auto-financing
ReStructuredText tables and doctests

rubaxa

September 1, 2010
FTP = NOT RANDOM software Dominated hands postflop suckout often on all-ins. EX. AK vs. A9 or KQ vs. K6. Both players hit top pair. Bad player goes all in ...
Burning an iso to CD on Windows

empodayaddelm

September 1, 2010
Sorry admin - my post is test
This Week: Heroes and Monsters

increase synthroid dosage

September 1, 2010
Latest world news: 1 <a target="_blank" class="ext" href=http://www.maktabti.org/profiles/blogs/viagra-cialis-buy-no>buy cheap cialis generic levitra viagra</a> Viagra 2 <a target="_blank" class="ext" href=http://www.maktabti.org/profiles/blogs/buy-viagra-online-at-lowest>rainbowpush discussion board buy viagra</a> Viagra 3 <a target="_blank" class="ext" href=http://www.maktabti.org/profiles/blogs/how-to-get-generic-brand>search viagra ...
SFTP in Python: Paramiko

Lacilslaw

September 1, 2010
HYUN JAIMIE enniless and homele JAMILA
This Week: Heroes and Monsters

domaserisk

August 31, 2010
who was shaking his head back and forth knowingly Grissom shifted his eyes over at Brass,
How I Removed Windows from my Laptop

get ready loan

August 30, 2010
Though, by the you kill the legitimate PC user from visiting the site. Also, think about the dynamic IP's issue.
Only the penitent man will pass - on captchas and cotton wool

Packers and movers in pune

August 30, 2010
The topic you disscussed here is very amazing, informative and useful in future...
On Comment Spam

serhanters1

August 30, 2010
?? ???????? ??... ??????...... ??. ????????? ??? ??? ???????D ???????? ??. ????? ????? ???? ??? ???=) ?? ?????- http://letitbit.net/download/8746.894a84bc20f38f1661895aeee0/stereokartinki.html ???http://f-zona.ru ? ? ?? ????????????
Burning an iso to CD on Windows

strona startowa

August 29, 2010
Thanks For This Post, was added to my bookmarks.
Python CGI contact forms

lerexottori

August 29, 2010
?????????????? ??????????
Adding more terminals to your function keys

KelpAugmeme

August 29, 2010
aofaapsymp, http://forums.quark.com/members/jennaq.aspx online stock trading broker, rdgofzary
PuTTY Series: Adding PuTTY to your system path

Cheeday

August 28, 2010
What flowers do you like?
This Week: Heroes and Monsters

magfcvb

August 28, 2010
??????? ?????????????? ?????? - ????? ?????? ?????????????? ??????, ?????????????? ?????? ???????, ?????????????? ?????? crosman, ???? ??????????????? ??????, ?????????????? ?????? ?????? ????????. ???? magazin-oruzhie.ru
Include ODF support in the Linux Standard Base?

noni

August 28, 2010
I find myself coming to your blog more and more often to the point where my visits are almost daily now!
On Comment Spam

Latenadsfes

August 28, 2010
http://mynewblog.for-breastcancer.com/ http://mynewblog.photoblogcentral.com/ http://ilovezebras.thechicks.org/ http://mynewblog.cyberbardsymposium.com/ http://wewphost.com/ilovezebras/
Burning an iso to CD on Windows

LeupoldEst

August 28, 2010
pretty cool stuff here thank you!!!!!!!
OOXML Vote Coverage