Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Wednesday, 30 January 2013

Copy and rename multiple files

What do you do on Linux when you want to rename, copy or move many files together in the same way, e.g. replacing all files named "donkey01.jpg" as "horse01.jpg" ? There is plenty of GUI utilities, but sometimes a quick and cheap shell hack is needed. Here you go: the magics of sed and shell scripting in a one liner.

for i in `ls *`; do cp $i `echo $i | sed "s/search/replace/g"`; done

From here.

Thursday, 11 August 2011

Recursive find

Sometimes you don't have locate , or you want just to find recursively below your current directory. Here is a handy alias:

alias fn='find . -name':
Example usage: $ fn "page*.htm"

Tip from Joe Grossberg.

Sunday, 25 May 2008

Where is my GRUB?

On the Gentoo mailing list, a nice thread evolved a way to find where did you install your bootloader last time. The question is less trivial than it seems - is it on your disk MBR or on your partition? If you have a non-obvious setup, you may forget... and if you try to upgrade/reinstall GRUB without remembering where it is, bad things can happen.

The solution is:
for i in /dev/[hs]d*
dd if=$i bs=446 count=1 2>/dev/null | strings | grep -q GRUB \
&& echo "GRUB found in $i"

Saturday, 10 May 2008

Create a .tar.gz archive

Ok, this is admittedly really noobish. But it's something I always forget. While the tar -xzvf spell for unpacking archives is well known, only seldomly I need to create archives.

However, here's the magic:

tar -pczf name_of_archive.tar.gz /path/to/directory

That's more a reminder for me...

Thursday, 1 May 2008

Find and delete broken symlinks

The Gentoo guide to switch from tetex to texlive contains a little command line gem. It allows you to look for broken/dangling symbolic links and delete them interactively:


find /usr/bin -type l ! -xtype f ! -xtype d -ok rm -f {} \;

Thursday, 15 March 2007

Matlab-like interactive search in your Bash command history

You probably already know that to navigate forward and backward in time your command line history, you use arrows. Arrow up: previous command. Arrow down: next command. What if you have to navigate more?



For example, suppose you yesterday looked at a text file. You don't remember its name, but you know you used "cat" to see it. And now you want to see it again. Normally you should have to (a)use the up arrow a lot until you meet the command you wanted or (b)use some trick like cat ~/.bash_history | grep cat and then copy-and-paste the right command.


Good, but it could be better. The science package Matlab for example has a nifty feature that allows you to search in your Matlab command line intelligently, autocompleting the first characters you have typed with matching lines of your command line history. This means that you type "cat" on your command line, by using the up and down arrows you navigate only the history of commands beginning with "cat". A kind of interactive grep on the go. Nice, isn't it?



For some reason this really useful feature is not enabled by default on most Unix distributions I am aware of, but it is really easy to activate it on Bash. Look for a file called .inputrc in your home directory. If you haven't it, look for /etc/inputrc instead (you of course have to be root to edit this one). Open it with your favourite text editor and look for these lines:

"\e[5~": beginning-of-history
"\e[6~": end-of-history

Comment them with a "#" character in front of them:

#"\e[5~": beginning-of-history
#"\e[6~": end-of-history

Insert these lines below the ones you just commented:

"\e[5~": history-search-backward
"\e[6~": history-search-forward

Save and exit. To make the changes have effect you probably have to login again. Now pressing Page-Up and Page-Down should autocomplete the first characters you type with the corresponding commands in the history. Once you get accustomed to this feature, you'll never want to come back.