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’.
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
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!