Category Archives: Linux

Using sed to view and replace text on a specific file in Linux

Ever need to replace very specific text on a particular line in a very large file on Linux? Enter the sed command.

To view the line (let’s say line 864 in this case) in the specific file, issue this command…

$ sed -n '864'p myfile.txt

And let’s say you get back the string ” I hate cheese”. Since we all know that’s not true, you can use a different variation of the sed command to replace “hate” with “love” like so…

$ sed -i '864s/hate/love/' myfile.txt

And if you’re really curious on how this even came about, it was due to an error in the phpMyAdmin export.php script when you had to change “break 2” to “break” on line 846 as identified here: https://askubuntu.com/questions/928883/500-error-on-phpmyadmin-export-on-phpmyadmin-export-php/930975

Like this? Consider joining my mailing list, or at least using my affiliate link when buying from Amazon 🙂

You or someone you know looking to buy or sell?

Linux Startup Email

Want to receive an email any time your linux server reboots? Just create this script…

sudo nano /usr/local/sbin/startupemail.sh

Here’s the code for the script…

#!/bin/sh

# Lets things startup
sleep 60

# Get info
IP="$(ifconfig | grep -A 1 'eth0' | tail -1 | cut -d ':' -f 2 | cut -d ' ' -f 1)"
HOSTNAME=`hostname`

#Send email
echo "IP: $IP, Host: $HOSTNAME" | mail -s "Server Started" me@there.com

Make sure to make it executable…

sudo chmod u+x /usr/local/sbin/startupemail.sh

Then add it to your /etc/rc.local file

Like this? Consider joining my mailing list, or at least using my affiliate link when buying from Amazon 🙂

You or someone you know looking to buy or sell?

OwnCloud webapp not reflecting true list of files

If you’re using OwnCloud and have a process whereby your add or remove files from directories via some sort of server process, you may find that you need to force OwnCloud to update/rescan the file listing. To do so you can manually run the following command…

sudo -u username php /path/to/owncloud/console.php files:scan --all

You may also want to consider adding it to the crontab for “username” on a daily basis.

Thanks to this site for pointing me in the right direction.

Like this? Consider joining my mailing list, or at least using my affiliate link when buying from Amazon 🙂

You or someone you know looking to buy or sell?

Sendmail isn’t sending to domain.com : Linux

Say you’re using sendmail to relay email on a web server that you own that’s named “domain.com”, and email is being delivered fine to any domain except domain.com. What could be the problem?

  1. Rename your server. No server should be named with an actual domain name.
  2. Change /etc/hostname to the new name for your server
  3. Update /etc/hosts to remove the domain name and replace with the new hostname
  4. Remove the domain name from /etc/mail/local-host-names
  5. Restart sendmail “sudo service sendmail restart”
  6. Restart your server “sudo reboot”

Once you’ve done that test sendmail out like so…

echo "command line test" | mail -s "Sendmail test" me@domain.com
Like this? Consider joining my mailing list, or at least using my affiliate link when buying from Amazon 🙂

You or someone you know looking to buy or sell?

Improved Linux command history

With four lines setup one time you can type the beginning of a command (ex: “ls”) then press the up and down arrows to see the most recent commands containing the text you typed (ex: “ls ~/scripts”) making it much easier to find what you’re looking for. To make this happen, create a text file called .inputrc in your home folder and put the following four lines inside:

"\e[A": history-search-backward
"\e[B": history-search-forward
set show-all-if-ambiguous on
set completion-ignore-case on
Like this? Consider joining my mailing list, or at least using my affiliate link when buying from Amazon 🙂

You or someone you know looking to buy or sell?

Linux ls output colors

If you’ve used linux much and your terminal is set to display files and directories in color, you know how frustrating it can be to have dark blue text on a black background like so (I’ve seen it much worse too)…

dirColorBefore

If you would like to modify the output so the colors are a bit more readable, you can add the following to your .bashrc file…

alias ls='ls --color'
LS_COLORS='di=33:fi=36:ex=31'
export LS_COLORS

which will produce an easier to read version like so (with directories yellow, files a crayon/teal, and executable files red)…

dirColorAfter

As pointed out in this article

The first line makes ls use the –color parameter by default, which tells ls to display files in different colours based on the setting of the LS_COLORS variable.

The second line is the tricky one, and what I have worked out so far has been by trial and error. The parameters (di, fi, etc.) refer to different Linux file types. I have worked them out as shown

di = directory
fi = file
ln = symbolic link
pi = fifo file
so = socket file
bd = block (buffered) special file
cd = character (unbuffered) special file
or = symbolic link pointing to a non-existent file (orphan)
mi = non-existent file pointed to by a symbolic link (visible when you type ls -l)
ex = file which is executable (ie. has ‘x’ set in permissions).

The *.rpm=90 parameter at the end tells ls to display any files ending in .rpm in the specified colour, in this case colour 90 (dark grey). This can be applied to any types of files (eg. you could use ‘*.png=35’ to make jpeg files appear purple.) As many or as few parameters as you like can go into the LS_COLORS variable, as long as the parameters are separated by colons.

Using trial and error (and a little bash script I wrote… my first one ever! 🙂 I worked out all the colour codes, at least my interpretation of them –

0 = default colour
1 = bold
4 = underlined
5 = flashing text
7 = reverse field
31 = red
32 = green
33 = orange
34 = blue
35 = purple
36 = cyan
37 = grey
40 = black background
41 = red background
42 = green background
43 = orange background
44 = blue background
45 = purple background
46 = cyan background
47 = grey background
90 = dark grey
91 = light red
92 = light green
93 = yellow
94 = light blue
95 = light purple
96 = turquoise
100 = dark grey background
101 = light red background
102 = light green background
103 = yellow background
104 = light blue background
105 = light purple background
106 = turquoise background

Like this? Consider joining my mailing list, or at least using my affiliate link when buying from Amazon 🙂

You or someone you know looking to buy or sell?

Pass a Command Line Argument to an Alias

Ever want to pass in a command line argument to an alias? You would think you could just do it with $1, but actually you have to create a function and then call that function. So, for instance if you want to pass a portion of a log file name, you could set your alias like this…

alias catlog='function _catIt() { cat /var/logs/$1.log; };_catIt'

then, when you want to see the XX00D log, you just call your alias like so…

catlog XX00D
Like this? Consider joining my mailing list, or at least using my affiliate link when buying from Amazon 🙂

You or someone you know looking to buy or sell?

Checking and Maintaining Linux Disk Space

Ever need to find out what’s using the most space on your Linux box? There are a couple commands that will help make things easier…

To find out how much space you have use:

df -h .

Which will give you output like so…

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda         20G   15G  4.2G  79% /

To list out the directories using the most space, use this handy command…

sudo du /usr/local | sort -n

… where /usr/local is the directory you want info for (you can also just start from root: /)

Like this? Consider joining my mailing list, or at least using my affiliate link when buying from Amazon 🙂

You or someone you know looking to buy or sell?

Learning VIM

So you would think that as a programmer for many moons now that I would have considerable experience in VIM and VI, but I’m hear today to admit that I am not. For years I’ve gotten by with my favorite Windows text editor notepad++, but the geek in me wanted to know why everyone loves Vim so much. To that end the 2 things I started with are…

I can’t say I’m a convert yet, but it’s always nice to have another tool in the toolbelt. Here for your and my reference is my quick VIM cheat sheet.

Like this? Consider joining my mailing list, or at least using my affiliate link when buying from Amazon 🙂

You or someone you know looking to buy or sell?

UNIX: Don’t Show Permission Denied Errors when using find Command

If you’re using the unix find command to search for files with a particular name

find -name theName
find: `./dir/thing': Permission denied
find: `./dir/thing1': Permission denied
find: `./dir/thing2': Permission denied

You might get distracted by all those “Permission denied” errors. The easy way to solve this is to redirect stderr to /dev/null like so…

find -name theName 2>/dev/null
Like this? Consider joining my mailing list, or at least using my affiliate link when buying from Amazon 🙂

You or someone you know looking to buy or sell?