Bash Function to find files with a certain extension

ls_ext () { find . -type f -iname '*.'${1}'' -exec ls -l {} \; ; }


ls_ext ()
 find . -type f -iname '*.'${1}'' -exec ls -l {} \; ;

then type “ls_ext pdf” or “ls_ext txt”

use unset -f function-name in this case “unset -f ls_ext” to clear the function.

Function to test multiple commands.

function mytest {
    local status=$?
    if [ $status -ne 0 ]; then
        echo "error with $1" >&2
    return $status

mytest $command1
mytest $command2

awk and printf to format a csv file

Although all the information is there it looks like a bit of a jumble.

To make it more legible use awk and printf to clean it up and make at easier to read.

Here’s what we start with.

Date,In,Out,h:m,Time,Rate(per hour),CA,Status,Billable,Customer,Tracking Number

This will format it into something a lot cleaner.

echo ' ';awk -F',' '{ printf "%-8s %-8s %-8s %-8s %-8s $%-16s %5s\n",$1,$2,$3,$4,$5,$6,$7; SUM += $7 } END {printf "%68s\n", "Total $"SUM}' timecard.csv; echo ' '

Now you have something like this

Date     In       Out      h:m      Time     $Rate(per hour)      CA
13.01.   10:02    17:32    7:30     7.5      $30                 225
12.01.   09:49    17:13    7:24     7.4      $30                 222
11.01.   09:53    17:36    7:43     7.72     $30               231.5
10.01.   09:50    17:15    7:25     7.42     $30               222.5
09.01.   10:05    17:16    7:11     7.18     $30               215.5
06.01.   09:30    17:16    7:46     7.77     $30                 233
05.01.   09:49    17:37    7:48     7.8      $30                 234
04.01.   09:40    17:18    7:38     7.63     $30                 229
03.01.   09:40    17:30    7:50     7.83     $30                 235
02.01.   10:13    10:14    0:01     0.02     $30                0.72
                                                      Total $2048.22

The echo’s are there just to break it away from the prompt.

Then you have the -F’,’ delimiter

Next the printf statement with the column width’s, one for each column.

Don’t forget the comma between the printf statement and the awk column selections.

I’ve also summarized the last column to give a total amount.

The last few column’s were left out intentionally, but you could add them easily by including them in both the awk and printf command. I also added the $ sign to the rate column by placing it in front of the % sign in the 6th printf column.

Hmmm! Looking at this I also think she needs a raise!

Here is a great intro to printf


Renaming Lists of Files

list of current files “test-file1.txt test-file2.txt  test-file3.txt  test-file4.txt”

change .txt to .html with rename

rename .txt .html *.txt

new list “test-file1.html  test-file2.html  test-file3.html  test-file4.html”

or use a for loop

for i in *.txt; do mv "$i" "$(basename $i .txt).html"; done OR for i in *.txt; do mv "$i" "`basename $i .txt`.html"; done

Add a prefix to every filename

for f in *.txt ; do mv "$f" "PREFIX_$f" ; done

new list “PREFIX_test-file1.txt  PREFIX_test-file2.txt  PREFIX_test-file3.txt  PREFIX_test-file4.txt”

Add a suffix

for f in *.txt ; do mv "$f" "$f_SUFFIX" ; done

Remove the PREFIX_ from every filename, adjust the cut command accordingly

for name in PREFIX_* ; do newname="$(echo "$name" | cut -b8-)"; mv "$name" "$newname" ; done

new list “test-file1.txt  test-file2.txt  test-file3.txt  test-file4.txt”

Remove the extension from file names

for i in $(ls); do mv $i ${i%.*}; done

Remove a prefix from file names

for i in $(ls *.txt); do mv $i ${i#*-};done

using a for loop and mv

for file in $(ls); do mv -i "${file}" "${file/Disc_1_-_/}"; done





Get the light to flash on an external drive

If you have multiple external drives connected and you need to know which one’s which you can use either of these commands to get it to flash the activity light.

The reason I needed to do this was because I have 3 external usb drives configured as a raid 5. No hot spare so if I see drive error’s they need to be addressed immediately.

X = the drive letter in question.

for i in {1..5}; do hdparm -tT /dev/sdX; done


for i in {1..10}; do dd if=/dev/sdX of=/dev/null bs=10M count=20; done

sed add to beginning or end of selected lines of a file

Create a new file with

for i in {1..15}-{a..c} ; do echo this is line $i >> test-file.txt ; done

Add a newline to the beginning and end of the file

sed -e '1i\ ' -e '$a\ ' test-file.txt

Add a newline from the 3rd through to the 10th line

sed -e '3,10i\ ' -e '$a\ ' test-file.txt

Add a newline at the 3rd and 10th line

sed -e '3~10i\ ' -e '$a\ ' test-file.txt

Add to the beginning of lines 1 through 5 with

sed '1,5 s/^/Alice the Goon say'"'"'s\t/' test-file.txt  <<-- Note the quotes for escaping the apostrophe.

Add to the end of lines 6 through 12 with

sed '6,12 s/$/\t  I Love Popeye/' test-file.txt

Select individual lines

sed -e '13 s/^/Lucky\t /' test-file.txt -e '14 s/$/\t\tPopeye/'

Add to the beginning of every third line

sed '0~3 s/^/hello\t/g' < test-file.txt

Add to the end of every third line

sed '0~3 s/$/\tdolly/g' < test-file.txt

change the file’s line numbering now with

sed -i '=' test-file.txt  "=" is a command in sed to print the current line number to the standard output.

append to the lines matching (PATTERN) “10” with

sed '/10/ a\ hello' test-file.txt

append after the line number (ADDRESS) “10” with

sed '10 a\ hello' test-file.txt

in each case

\a  for append (add after match) can be changed for

\i  for insert (add before match)

or  \c  to change the whole matching line.

example: sed '/10/ c\ hello dolly' test-file.txt

Unlike using

sed 's/is/hello dolly/g' test-file.txt  OR  sed 's/10/hello dolly/' test-file.txt

Finally you can re-number your file with

sed = test-file.txt | sed 'N;s/\n/\t/'  OR  nl testfile.txt  OR  cat -n testfile.txt

as always write it to a new file with > newfile-name

or use -i to edit it inline

To delete trailing whitespace from end of each line

cat input.txt | sed 's/[ \t]*$//' > output.txt

Remove all leading and trailing whitespace from each line

cat input.txt | sed 's/^[ \t]*//;s/[ \t]*$//' > output.txt

To change the nth occurrence on each line add a number at the end of the substitute command

sed 's/old/new/2' file



selinux cheat sheet

To list all sebooleans currently on (allowed)

getsebool -a | grep 'on$'

To list all sebooleans currently off (disallowed)

getsebool -a | grep 'off$'

To list all current booleans with their discriptions

semanage boolean -l

List your default port info with

semanage port -l

If you want to change a port on a SELinux system, you have to tell selinux about this change.
e.g. semanage port -a -t ssh_port_t -p tcp #PORTNUMBER

temporarily switch between permissive or enforcing (It will revert back to default on a reboot.)

setenforce 0 (permissive)

setenforce 1 (enforcing)

sestatus will give you your current status

To set the level permanently edit your /etc/selinux/config file.

Copy a security context from 1 file or directory to another with

chcon [OPTION]… –reference=RFILE FILE…

e.g. chcon -R –reference=/default/web/dir /other/web/dir (will recursively copy the permissions from default web dir to new web dir.)


change the label of /other/web/dir, recursively, to the httpd_sys_content_t type in order to grant Apache read-only access to that directory and its contents:
semanage fcontext -a -t httpd_sys_content_t “/other/web/dir(/.*)?”
Apply the selinux policy created with
restorecon -R -v /other/web/dir

List current security on files and dirs with ls -Z

Turn booleans on or off with

e.g. setsebool -P allow_ftpd_anon_write=1 or 0

Get a report on all selinux denials with

aureport -a

Get a report on current selinux denials with recommendations.

sealert -a /var/log/audit/audit.log  (for this I think you need setroubleshoot installed.)

From the manpage

setsebool – set SELinux boolean value
setsebool [ -PNV ] boolean value | bool1=val1 bool2=val2 …
setsebool  sets the current state of a particular SELinux boolean or a list of booleans to a given value. The value may be 1 or true or on to enable the boolean, or 0
or false or off to disable it.
Without the -P option, only the current boolean value is affected; the boot-time default settings are  not changed.
If the -P option is given, all pending values are written to the policy file on disk. So they will be persistent across reboots.
If the -N option is given, the policy on disk is not reloaded into the kernel.
If the -V option is given, verbose error messages will be printed from semanage libraries.

Installing Vim Pathogen


Manage your 'runtimepath' with ease. In practical terms, pathogen.vim makes it super easy to install plugins and runtime files in their own private directories.


Install to ~/.vim/autoload/pathogen.vim. Or copy and paste:

mkdir -p ~/.vim/autoload ~/.vim/bundle && \
curl -LSso ~/.vim/autoload/pathogen.vim

If you’re using Windows, change all occurrences of ~/.vim to ~\vimfiles.

Runtime Path Manipulation

Add this to your vimrc:

execute pathogen#infect()

If you’re brand new to Vim and lacking a vimrc, vim ~/.vimrc and paste in the following super-minimal example:

execute pathogen#infect()
syntax on
filetype plugin indent on

Now any plugins you wish to install can be extracted to a subdirectory under ~/.vim/bundle, and they will be added to the 'runtimepath'. Observe:

cd ~/.vim/bundle && \
git clone git://

Now sensible.vim is installed. If you really want to get crazy, you could set it up as a submodule in whatever repository you keep your dot files in. I don’t like to get crazy.

If you don’t like the directory name bundle, you can pass a runtime relative glob as an argument:

execute pathogen#infect('stuff/{}')

The {} indicates where the expansion should occur.

You can also pass an absolute path instead. I keep the plugins I maintain under ~/src, and this is how I add them:

execute pathogen#infect('bundle/{}', '~/src/vim/bundle/{}')

Normally to generate documentation, Vim expects you to run :helptags on each directory with documentation (e.g., :helptags ~/.vim/doc). Provided with pathogen.vim is a :Helptags command that does this on every directory in your 'runtimepath'. If you really want to get crazy, you could even invoke Helptags in your vimrc. I don’t like to get crazy.

Finally, pathogen.vim has a rich API that can manipulate 'runtimepath' and other comma-delimited path options in ways most people will never need to do. If you’re one of those edge cases, look at the source. It’s well documented.

change from booting graphical mode to multiuser with systemd

With systemd you can switch from graphical mode to multiuser by typing init 3 as root

or systemctl isolate

If you want to permanently change and save resources

systemctl set-default <name of target>.target

The full list of targets can be accessed with systemctl list-units –type=target

or do

rm /etc/systemd/system/

then do

ln -s /lib/systemd/system/ /etc/systemd/system/

when you reboot your machine it should start up with a nice black screen and login: