In a previous post, I gave a command for doing a find and replace operation across multiple files in a directory.
I have used that command a number of times over the years but the problem with it is that it only works if all of the files you want to change are in the same directory.
I sometimes have need to perform this find and replace operation across files in different directories.
For this, you can use the find command and use xargs to run sed:
find . -name '*.txt' -print0 | xargs -0 sed -i 's|originaltext|replacementtext|g'
This command will find any files with the .txt extension, in the current directory or any directory below it and run it through sed to replace ‘originaltext’ with ‘replacementtext’.
Anyone who has done web development on a site that uses a https:// (SSL) secure connection will be familiar with the annoying “This Page Contains Both Secure and Non-Secure Items” error (or exclamation mark in other browsers) if you include an asset in http:// on the page.
This neat trick, which Paul Irish calls the “Protocol Relative URL”.
If you use // at the beginning of the URL, it will automatically use whichever protocol you are currently using.
eg:
<img src="//domain.com/images/logo.png">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>
.myclass { background: url(//domain.com/images/background.gif);
The only cavet is that you should avoid using it with <link> or @import to include stylesheets as IE7 and IE8 have a bug where it will load the stylesheet twice, which is in-efficient (especially since downloading a stylesheet is a blocking action so it will halt rendering the page while it downloads).
If you use VirtualBox and try to copy/clone a hard disk (VDI) file to use with another virtual machine, it will not let you add it because it complains that there is already an image with that UUID.
Fortunately the fix is simple – there is a hidden command available to generate a new UUID:
VBoxManage internalcommands setvdiuuid myfile.vdi
To recursively chmod all files in the current directory (and sub-directories), you can do this:
find . -type d -exec chmod 755 {} \;
To do the same with just files, you can use:
find . -type f -exec chmod 644 {} \;
It’s really useful to not only be able to see the user and directory in your prompt but to have different colours for different users/servers – like a different colour for root or production boxes. Here is the code I use – just put it in a .profile in the users’s home directory (or in /etc/profile for globally):
# Set xterm title:
SHOSTNAME=`hostname -s`
PROMPT_COMMAND='if [ "${TERM}" = "xterm" -o "${TERM}" = "xterm-color" ];
then
if [ -n "${XTITLE}" ];
then
echo -ne "\033]0;${XTITLE}\007";
else
echo -ne "\033]0;[${USER}@${SHOSTNAME}:${PWD}]\007" | sed -e "s@${HOME}@\~@";
fi;
fi'
# Set Bash Prompt:
if [ "$BASH" ]; then
PS1='\[\033[1;36m\][\u@\h:\w]\$\[\033[0m\] '
# alias ls='ls --color'
else
if [ "`id -u`" -eq 0 ]; then
PS1='# '
else
PS1='$ '
fi
fi
export PS1 PROMPT_COMMAND SHOSTNAME
You can change the colours using the following codes:
# Black 0;30 Dark Gray 1;30
# Blue 0;34 Light Blue 1;34
# Green 0;32 Light Green 1;32
# Cyan 0;36 Light Cyan 1;36
# Red 0;31 Light Red 1;31
# Purple 0;35 Light Purple 1;35
# Brown 0;33 Yellow 1;33
# Light Gray 0;37 White 1;37
If you have a high load on your web server, it’s useful to be able to see a list of the current connections on port 80 as sometimes high load can be caused by someone abusing the site – for example scraping it or some kind of denial of service attack.
This code will show a list of the ip’s currently connected to your server on port 80 with a count of the number of connections to the left of it. It’s ordered by the number of connections – highest first:
netstat -ntulpa | grep :80 | awk '{print $5}' | cut -d: -f1 | sort -n | uniq -c | sort -n -r
There are loads of snippets on the web about how to add your facebook status to your wordpress blog by using an RSS feed.
However, it looks like Facebook have made a load of changes to the interface and I can’t find the mini-feed page anywhere, which has the link on.
Luckily, this blog post gave the link for it.
So, to get an RSS feed of your Facebook status, you just log into Facebook, go to this link:
http://www.new.facebook.com/minifeed.php?filter=11
and click on the “My Status” link on the right hand side.
You can then add that RSS feed to the right hand side of your WordPress blog, if required!
Have you ever needed to change the same thing in a load of files and have had to sit there and go through each one in turn and make the same change?
This is a really useful and simple command:
perl -pi -w -e 's/from text/to text/g;' *
It simply goes through all files matching * and runs the regular expression, ‘s/from text/to text/g’ – which means replace all instances of “from text” with “to text”. nifty!