Showing posts with label linux command. Show all posts
Showing posts with label linux command. Show all posts

Thursday, February 11, 2016

Recursive Replace Text in Files

If you ever have a need to replace text in files (including subfolders) execute the following linux command.

In the below example we wanted to replace a copyright on a group of html and php pages.
2010 - 2015 became 2010 - 2016


user@machine> find . -type f -name "*" -exec sed -i'' -e 's/2010 - 2015/2010 - 2016/g' {} +

Tuesday, September 24, 2013

Create Archive of Website

If you have a website that you're retiring but would like to copy it from your current registrar without having to sFTP/FTP the following command will copy it in Linux.

wget --recursive --domain [domain] --no-parent --page-requisites [location]

Tuesday, September 10, 2013

Nginx - Count of IP addresses in /var/log/nginx/access.log

Inspired by this post which supposedly works for Apache and also works for my Nginx installation. It seems however, some of the comments didn't work quite right on our Debian Lenny equipment.

Did away with grep (instead using cat) and tweaked to fix sorting not working correctly.

cat access.log | cut -d' ' -f1 | sort | uniq -c | sort -rg

If your log is big enough to scroll multiple pages when logged in remotely.

cat access.log | cut -d' ' -f1 | sort | uniq -c | sort -rg | less

Tuesday, March 6, 2012

Bulk File Rename

As usual there are command I use just often enough to forget them when the time cose. In our case we had a group of files that has the file extension in caps which was causing a problem with some 3rd party javascript so instead of modifying the .js files we change the extension to lower case (.JPG to .jpg).

for file in *.JPG ; do mv $file `echo $file | sed 's/\(.*\.\)JPG/\1jpg/'` ; done

Monday, June 29, 2009

Free and Easy Way to Merge PDF Documents

Recently when downloading a free, multi-part pdf copy of The Foxfire Book on Scribd it reminded me that there was a way in cygwin to merge those multiple copies into a single pdf, enter pdftk. It is extremely simple to merge with a single command.

Tuesday, February 10, 2009

How Do I Find Out My Linux Version

lsb_release -a

If your distro returns a "command not found" try

cat /etc/*version and you should see a number returned

Wednesday, February 6, 2008

How To Fix A Bad DVD

If you've ever had a DVD that was having problems you might want to consider trying out ddrescue. I used it on a video that had one too many scratches on it and it created a reusable iso that worked well enought to watch.


user@machine-> aptitude show ddrescue
Package: ddrescue
State: installed
Automatically installed: yes
Version: 1.13-3
Priority: optional
Section: universe/utils
Maintainer: Ubuntu MOTU Developers
Uncompressed Size: 77.8k
Depends: libc6 (>= 2.5-0ubuntu1)
Description: copies data from one file or block device to another dd_rescue is a tool to help you to save data from crashed partition. It tries to read and if it fails, it will go on with the next sectors where tools like dd will fail. If the copying process is interrupted by the user it is possible to continue at any position later. It can copy backwards.

Tuesday, February 5, 2008

021 no connection named [OpenSWAN]

Since it had been over a year since I had last used OpenSWAN I couldn't remember why this error occurred. Searching Google for 021 no connection named "blah" was not producing a fix. Finally, and I admit this a little embarrassed I read the manual and found that I had forgot to do after configured the /etc/ipsec.conf file.

user@machine ~> sudo ipsec auto --add myconnection


So if you see the following in the future don't forget to add the connection.

user@machine ~> sudo ipsec whack --name myconnection --initiate
021 no connection named "myconnection"

Restarting Pluto [OpenSWAN]

If you've suffered the same fate as I have...the inability to locate solutions on restarting pluto aside from the infamous RTM this post if for you.

First we'll make sure pluto is running and get the PID

user@machine ~> sudo ipsec setup --status
IPsec running - pluto pid: 8794
pluto pid 8794
No tunnels up


Now we restart everything related to ipsec

user@machine ~> sudo /etc/init.d/ipsec restart
ipsec_setup: Stopping Openswan IPsec...
ipsec_setup: Starting Openswan IPsec 2.4.6...
ipsec_setup: insmod /lib/modules/2.6.22-14-generic/kernel/net/key/af_key.ko
ipsec_setup: insmod /lib/modules/2.6.22-14-generic/kernel/net/ipv4/xfrm4_tunnel.ko
ipsec_setup: insmod /lib/modules/2.6.22-14-generic/kernel/net/xfrm/xfrm_user.ko


Last but not lease lets make sure pluto restarted and has a new PID

user@machine ~> ipsec setup --status
IPsec running - pluto pid: 9193
pluto pid 9193
No tunnels up

There are probably other ways of accomplishing this as well however so if you know please add a comment letting everyone know.

Tuesday, December 11, 2007

Play DVD ISO In VLC

After ripping some DVDs I purchased that appear to have been made using a home computer DVD burner or something simple I couldn't get the ISO files to play in VLC using the usual method. After a few minutes of looking around VLC I found the solution, these DVDs did not have menus so it was cause VLC to not load the ISO, switching to the command that exists for DVDs without menus we were off to the races.

user@machine~> vlc dvdsimple:///videos/Buckshot/10_Homemade_Traps.iso

Monday, December 10, 2007

Get List of Installed Packages on Debian, Ubuntu, Etc.

Need to get a list of packages that exist on your Debian based system? If you're running Ubuntu, Debian, etc. try the following.

user@machine~> dpkg --list

Tuesday, December 4, 2007

Create an ISO from a DVD

If you need to create backups of DVDs there are a multitude of ways people recommend doing them on the web. The easiest I found can be accomplished in a single command and creates an ISO file that can be mounted later if necessary. Even tried it with an old, uncopyrighted DVD movie and it worked well.

user@machine ~> dd if=/dev/dvd0 of=~/dvdcopy.iso bs=2048


Some might ask: Why the bs=2048 parameter?

The reason is that often burnt cds/dvds have a number of null sectors
appended to the actual iso on the disc. If you simply use dd without a block
count the resulting iso will not be identical to the iso from which the dvd
was produced. The same remarks apply to the use of cat /dev/dvd > your.iso.
Not to say that the result won't work, it might. But if you do `md5sum
your.iso` the sum will not match that of the original iso if the there are
any blocks of padding copied up.
source

To remount:
user@machine ~> mkdir /media/dvdcopy
user@machine ~> mount -t iso9660 -o loop ~/dvdcopy.iso /media/dvdcopy


There are some posts on the web stating that if mounting a DVD that the type needs to be set to udp instead of iso9660 however the iso9660 value worked fine.

Friday, November 30, 2007

Ubuntu Version Where Are You?

Being a more recent Ubuntu user I can never remember which version I installed, it was much easier in the Debain days but adapt, improvise and overcome is the motto I try to live by. There are two options which I've included below, if someoone knows of more please pass them along.

Option 1:

user@machine~> cat /etc/issue
Ubuntu 7.04 \n \l

Option 2:
user@machine~> lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 7.04
Release: 7.04
Codename: feisty



Monday, November 26, 2007

Retrieve System Information

As usual with Linux (and Windows in some cases) there are multiple ways to accomplish the same task but here is one for getting information on disks, usb devices, etc. attached to your computer.

user@machine:~$ sudo lshw

The listing can be quite long so don't forget to run it with the less, more or grep if you just want to see if a particular device is installed. For example if I wanted to check if one of my PQI mini-flash memory sticks was installed.

user@machine:~$ sudo lshw | grep pqi
vendor: pqi


If you're allergic to the command line use the GUI by installing lshw-gtk.

Monday, October 29, 2007

Scan for Wireless Networks on Ubuntu

Now that I'm beginning to travel more I run into situations where I need to search for wireless networks. Since I'm running Xubuntu some of the utilities that would exist in the more full featured desktops do not exist in Xubuntu but I found a few alternatives.

One solution is to install the wifi-radar package. Another that I found was running the following commands:

user@machine ~> sudo iwlist scan
user@machine ~> sudo iwconfig wlan0 mode Ad-Hoc {In this example wlan0 is the name of the wireless inteface}

The interface can also be edited with:
user@machine ~> sudo iwconfig

Test nginx Configuration File

If there is a time when it becomes necessary to test out a nginx.conf file prior to attempting to use it (IOW on a live/production system) here is a handy little command.

user@machine ~> nginx -t -c /etc/nginx/nginx.conf

Tuesday, July 24, 2007

Keeping SSH Session Open

If you're a person that uses ssh often to connect to remote machines one thing you'll discover pretty quick is that you want a way to keep your ssh session 'open and running' when you're disconnected. The best solution I found for that is screen. To quote directly from the man for screen:

Screen is a full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells). Each virtual terminal provides the functions of a DEC VT100 terminal and, in addition, several control functions from the ISO 6429 (ECMA 48, ANSI X3.64) and ISO 2022 standards (e.g. insert/delete line and support for multiple character sets). There is a scrollback history buffer for each virtual terminal and a copy-and-paste mechanism that allows moving text regions between windows.
Here's a good .screenrc, just drop it in your home directory and you should be good to go.

The usual process is to log into a server, run screen and then work within the screen shells to do your work. When you're ready to log off the server just type CTL+A and then D and you should be back at the login bash/csh/whatever prompt and see [detached] above it. When you log back into the server (after logging off or being kicked off due to a connection problem) just type screen -r and you should be back into the screen session you had working before. Now that I've been using screen for almost 3 years I don't know how I ever got along without it.

Creating an ISO from a CD on Linux

As many can relate too I've built up a huge pile of CDs from customer PC builds, PC installs, software purchases, etc. Now that I've migrated my main PC over to Linux I wanted to move all of them out to ISOs so I'd have a backup (plus I'm working on automated offsite backups). Doing it is easy!

dd if=/dev/cdrom of=cd.iso # for cdrom
If you want to know more then I'd recommend checking out this post by Scott Granneman