Linux shell

Adding extra Swap to Linux

On one of my servers I needed extra swap space because the datacenter screwed up the partitioning and I didn’t have time to reimage the machine. so an easy way to add swap after partitioning is just to add it via a new file on any partition.

lets say you want to add 8GB of swap in a file called swapfile based in /

# generate empty swap file
sudo dd if=/dev/zero of=/swapfile bs=1024 count=8192k
# make the file into swap ready file
sudo mkswap /swapfile
# enable swap on said file so the OS / kernel knows about it
sudo swapon /swapfile
#list enabled swap
swapon -s

This will last till a reboot, so you need to add it to /etc/fstab.  type :

sudo nano /etc/fstab

add at the bottom :

/swapfile       none           swap    sw      0       0

and you are done 🙂

Linux shell

Yum install dependencies for a local RPM

Got an RPM laying about you need to install but it has a bunch of dependencies? well let yum do the heavy lifting.

yum --nogpgcheck localinstall SomeApp.noarch.rpm


Linux shell tuts

Remove all old installed but unused kernels

I just noticed I had 8 kernels installed on a machine. dont need but one…. so I rebooted to make sure I was using the newest, and removed all the old ones via these simple commands :


Ubuntu / Debian / dpkg / Apt:

dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | xargs sudo apt-get -y purge

Thats a one liner, so just copy paste the whole thing.

Centos / RHEL / Yum / RPM:

uname -r

See which kernel is being used.

rpm -q kernel

get a list of installed kernels

yum install yum-utils

make sure yum-utils is installed

package-cleanup --oldkernels --count=1

Cleanup old packages, the count can be any number and it will keep that number of the latest packages. count 2 will keep the current newest and one more, 3 will keep the current newest and 2 more and so on.

nano yum.conf

edit /etc/yum.conf and look for installonly_limit and change it to any number, this was the kernels will be cleaned up automatically. I have mine set to 3.



Linux shell

Generate file of fixed size via CLI

We recently moved a hosting to a new data center and wanted to test speeds, so I needed fixed size files to see what kind of speeds people could achieve while downloading them. I decided on the arbitrary numbers of 100MB and 500MB. Heres how I generated fixes size files via bash.

for 500 MB :

dd if=/dev/zero of=file.txt count=1000 bs=524288

for 100MB :

dd if=/dev/zero of=file.txt count=10240 bs=10240

Now you can use other things such as /dev/random etc for this. but this is extremely quick. the 500MB file was finished in .47 seconds. That’s less than half a second….

Linux shell

secure copy ( SCP ) howto 102

Secure Copy (SCP) provides a secure and authenticated method for copying files between hosts. SCP relies on Secure Shell (SSH).

Copy the file “somefile.txt” from a remote host to the local host

scp [email protected]:somefile.txt /some/local/directory

Copy the file “somefile.txt” from the local host to a remote host

scp somefile.txt [email protected]:/some/remote/directory

Copy the directory “somedir” from the local host to a remote host’s directory “anotherdir”

scp -r somedir [email protected]:/some/remote/directory/anotherdir

Copy the file “somefile.txt” from remote host “” to remote host “”

scp [email protected]:/some/remote/directory/foobar.txt \

[email protected]:/some/remote/directory/

Copying the files “somefile.txt” and “anotherfile.txt” from the local host to your home directory on the remote host

scp somefile.txt anotherfile.txt [email protected]:~

Copy the file “somefile.txt” from the local host to a remote host using port 1234

scp -P 1234 somefile.txt [email protected]:/some/remote/directory

Copy multiple files from the remote host to your current directory on the local host

scp [email protected]:/some/remote/directory/\{1,2,3\} .

scp [email protected]:~/\{somefile1.txt,somefile2.txt\} .
Linux shell

Padding File names with zeros

need to pad some file names with zeros? couldnt be simpler!


for f in foo[0-9]*; do mv $f `printf foo%05d ${f#foo}`; done
rename 's/\d+/sprintf("%05d",$&)/e' foo*