• Home
  • Shell
    • Emacs
    • Perl
    • screen
    • sed
  • Ubuntu
    • VNC
  • Web Development
    • Javascript
    • Joomla
    • MySQL
    • osTicket
  • Windows
    • Gimp

Posts in category BASH

BASH> Fix vi arrow keys in insert mode

Mar28
2013
Written by Scott Rowley

Full credit to Michael S. Kirkpatrick

The following fix saved my sanity

I prefer using vi as my text editor, because it is clean and simple. I don’t have to go searching for commands from drop-down boxes and other GUI crap. But arrow keys and backspace are sometimes problematic, depending on the OS you’re using. In my case, I frequently ssh from Linux to SunOS, and the SunOS vi doesn’t like my keyboard mappings.

Here is my (partial) solution. Open ~/.exrc and add the following lines. In case it is not obvious, do not type the letters inside brackets, but press those keys instead (i.e., [ctrl-v] means you hold the Ctrl key while pressing v).

— Michael S. Kirkpatrick

:map! [ctrl-v][backspace] [ctrl-v][esc]xa
:map! [ctrl-v][up-arrow] [ctrl-v][esc]ka
:map! [ctrl-v][down-arrow] [ctrl-v][esc]ja
:map! [ctrl-v][right-arrow] [ctrl-v][esc]la
:map! [ctrl-v][left-arrow] [ctrl-v][esc]ha

When you type these in (assuming you’re using vi or vim, these lines will look like this.

:map! ^? ^[xa
:map! ^[OA ^[ka
:map! ^[OB ^[ja
:map! ^[OC ^[ha
:map! ^[OD ^[la
Tagged arrow, backspace, control, ctrl, down, editor, esc, escape, exrc, fix, key, keys, left, map, Michael S. Kirkpatrick, right, up, vi, vim

BASH> Functions

Mar22
2013
Written by Scott Rowley

The beginning of a list of any functions I have created or found and deem useful:

This function will allow you to type “up ANY_NUMBER” and move that many directories up in the current directory tree you are in. If you you do not specify a number and instead just type ‘up’ by itself you will automatically go up just one directory.

function up ()
{
if (( $#> 0 )); then
COUNTER=$1;
else
COUNTER=1;
fi;
while (( ${COUNTER}> 0 )); do
UP="${UP}../";
(( COUNTER=${COUNTER}-1 ));
done;
echo "cd $UP";
cd $UP;
UP=''
}

Some servers don’t have ‘watch’ installed so I’ve made my own function to replicate it’s basic functionality (updated to work with pipes!)

function mywatch () {
while true
do
clear
date +'%D %r'
echo ''
cmd=`echo $@`
#echo "Command is '$cmd'"
last=`echo $@ | awk {'print $NF'}`
num='^[0-9]+$'
if [[ $last =~ $num ]]; then
cmd=`echo $cmd | sed "s^$last^^g"`
eval "$cmd"
sleep $last
else
eval "$cmd"
sleep 2
fi
clear
done
}

Useage:

mywatch "ls -al" 5

If a second parameter ($2) is not supplied (in this case 5) then 2 will be used and your watch will refresh every 2 seconds.

Easily locate problem directories when space is low on a server. Add the following function to your profile (.profile / .bashrc / etc)

function spaceHog ()
{
du -k | sort -n | awk '
BEGIN {
split("KB,MB,GB,TB", Units, ",");
}
{
u = 1;
while ($1>= 1024) {
$1 = $1 / 1024;
u += 1;
}
$1 = sprintf("%.1f %s", $1, Units[u]);
print $0;
}'
}

Useage:

[17:25:51][user@server][ ~ ]
[$]> spaceHog | tail -10 # To show the 10 largest, change this number as desired.
247.5 MB ./bin
247.8 MB ./.cache/google-chrome/Default
247.8 MB ./.cache/google-chrome
283.4 MB ./.cache
293.2 MB ./.wine/drive_c/Program Files (x86)
423.5 MB ./Downloads
653.6 MB ./IOS_APPS
655.1 MB ./.wine/drive_c
658.2 MB ./.wine
2.7 GB .

Delete (rm) a file by it’s inode number

Occassionally I will (or others will) run a command that ends up accidentally creating some goofy named files such as ‘?[01’. This is actually a character so you can’t just type it in to delete it. For this you can lookup the file name by it’s inode and then delete it.

You can do an ls with an -i option to get the inodes, for example:

[$]> ls -ali
total 8
295188 drwxr-xr-x 2 sasowner sas 4096 Dec 30 11:26 .
295064 drwx------ 23 sasowner sas 4096 Dec 30 11:26 ..
295406 -rw-r--r-- 1 sasowner sas 0 Dec 30 11:26 file1
295407 -rw-r--r-- 1 sasowner sas 0 Dec 30 11:26 file2
295408 -rw-r--r-- 1 sasowner sas 0 Dec 30 11:26 file3
295409 -rw-r--r-- 1 sasowner sas 0 Dec 30 11:26 file4

You could then follow this up with a command such as:

[$]> find . -inum 295409 -exec rm {} \;

However, you probably won’t use this command often enough to remember it unless you are regularly familiar with the find command. Instead, I’ve create the following simple function:

function rmi ()
{
find . -inum $1 -exec rm -i {} \;
}

Then simply type the following after getting the inode of the file

[$]> rmi ######
Posted in Ubuntu - Tagged alias, bash_profile, bashrc, cd, clear, COUNTER, date, directory, function, monitor, move, navigate, profile, sleep, up, watch

Usenet> Newznab backfilling

Jan31
2013
Written by Scott Rowley

I’ve recently been checking out newznab plus. I’ve found that while it’s nice as it slowly builds, it’s a bit annoying not having any older posts already loaded. Fortunately nn+ (nnp, newznab plus) comes with an option for backfilling. The instructions are useful but I’d like to expand on them to make it less of a hassle to update more than one particular group.

Please note that I’ve only been using nn+ for a few days now so there may indeed be easier ways to do things but this is the best way I’ve found so far. For example, I’m not exactly sure what the update_binaries_threaded.php file does but I don’t believe it does what I’m discussing here. I’m guessing from the “threaded” that it simply makes multiple threads of the same process to make the entire task faster.

The following is taken from http://newznab.readthedocs.org/en/latest/readme/#backfilling-groups

Backfilling Groups
Since most usenet providers have 800+ days of retention indexing all that information in one shot is not practical. Newznab provides a backfill feature that allow you to index past articles once your initial index has been built. To use the feature first set the back fill days setting in the group(s) to be backfilled to the number of day you wish to go back, making sure to set it higher than the number of days listed in the first post column. Once set run the backfill.php script in misc/update_scripts. Groups can be backfilled to a particular date using the script misc/update_scripts/backfill_date.php using the syntax:

php backfill_date.php 2011-05-15 alt.binaries.groupname.here

You can use the _threaded version of this script if on linux.

For more information on backfilling, see Update Scripts.

First off, what we need to do is to activate any groups we want. More than likely you’ve already done this. What you’ll want to do in order to backfill those groups is to change the ‘Backfill Days’ entry. This can be done through the webpage but if you are wanting to backfill all of them then this following command will make it much faster than manually clicking through a dozen or more groups.

You’ll need to be on the command line and then log into mysql

mysql -u root -p
Enter password:
mysql> use newznab; (or whatever your database name is)
mysql> update groups set backfill_target=365 where active=1;

You have now just changed all active groups to have a backfill of 365 days (1 year). Now we just need to run our backfill_date.php on each group. If we’ve got dozens of groups or more though this can be tedious to repeat over and over with each group. So instead we are going to get a list of all the active groups by running the following command:

mysql -u root -p newznab -e 'select name from groups where active=1;'> groups.txt

You can now go into the groups.txt file and remove and additional groups you want or just leave it intact if you want everything.

Finally we want to run the php backfill.php command to backfill each of our selected groups back 365 days (or whatever number you’ve selected, alternatively you can also skip updating the database with the amount of days to backfill and simply substitute the backfill_date.php as noted from the link.)

Normally this command would be run like so for a single group:

php backfill.php alt.binaries.groupname.here

But this is much too slow for multiple groups so we are going to use a ‘for’ function with our data from the groups.txt file we created.

for group in `cat groups.txt`; do php backfill.php $group; done

If you are like me and like to see your code before it runs then simply add an echo beforehand to view the results and then remove it when you are ready:

for group in `cat groups.txt`; do echo "php backfill.php $group"; done

The use of the backticks (`) around ‘cat groups.txt’ tells it to run the command in a subshell and then use the information from that back in the shell command.

Posted in PHP, Ubuntu - Tagged backfill, backfill days, for, groups, MySQL, newznab, newznab plus, nn+, nnp, php, update, usenet

MAN PAGE> ‘date’ (8.4)

Jan30
2013
Written by Scott Rowley

Note that this man page is regarding date version ‘date (GNU coreutils) 8.4’, your mileage may vary when using other versions.

I’m going to expand this section to include examples of every entry from the man page. Note that some examples include additional commands to demonstrate the information we are working with.

NAME
date — print or set the system date and time

SYNOPSIS
date [OPTION]… [+FORMAT]
date [-u|–utc|–universal] [MMDDhhmm[[CC]YY][.ss]]

DESCRIPTION
Display the current time in the given FORMAT, or set the system date.

-d, –date=STRING
display time described by STRING, not ‘now’

date
Wed Jan 30 10:08:18 CST 2013
date -d "1 year ago"
Mon Jan 30 10:08:33 CST 2012

-f, –file=DATEFILE
like –date once for each line of DATEFILE

cat years.txt
1 year ago
2 years ago
3 years ago
date -f years.txt
Mon Jan 30 10:19:17 CST 2012
Sun Jan 30 10:19:17 CST 2011
Sat Jan 30 10:19:17 CST 2010

READ MORE »

Tagged cat, date, example, Format, man, man page, page, shell, time

BASH> Modify file/directory timestamp

Jan28
2013
Written by Scott Rowley

In order to modify the timestamp of any file or directory you can run the following commands

Modify timestamp to “right now”

touch filename

Modify timestamp to a past or future time

touch -t 201301281030 filename

BASH> Recursively set file/directory permissions

Jan19
2012
Written by Scott Rowley

Move to the directory you want to start chmodding files in and run the following:

Recursively set FILE permissions.

find . -type f -exec chmod 644 {} \+

Recursively set DIRECTORY permissions.

find . -type d -exec chmod 755 {} \+

*Note, obviously you can change the chmod number to whatever you want, 777, 600, etc.

Tagged chmod, chmodding, directory, file, files, find, permission, recursive, recursively, set, type

Recursively set group permissions to match owner permissions

Oct10
2011
Written by Scott Rowley

I have had need of this multiple times in the past, just making a note of it here:

chmod -R g=u directory_name/
Tagged chmod, directory, group, owner, permissions, recursive

Linux> Aliases

Jun08
2011
Written by Scott Rowley

Aliases can be a great way to more easily remember an oddball command or to shorten a long command. Aliases are stored in your .profile (or .bash_profile) or within a include file referenced from .profile.

A couple of quick examples before showing how to set them up:
READ MORE »

Posted in Ubuntu - Tagged alias, basic, conditional, epoch, ggrep, grep, if, include, linux, Perl, print, profile, reload, solaris, time, ubuntu, unix, whois

Linux> Command line shortcuts

Jun06
2011
Written by Scott Rowley

Another starter article to grow with my needs, we’ll start off with the shebang, or bang…or simply “!”. The first two commands I believe are the most useful and could be commonly used. Note that some of these can be a bit difficult to remember, we’ll also go over making aliases so we can use something a little easier to remember in a later article.
READ MORE »

Posted in Ubuntu - Tagged !, bang, command, date, exclamation, execute, home, last, previous, print, prior, shebang, shortcut, tilde

AWK> Useful examples

May26
2011
Written by Scott Rowley

Just a starter page that will grow as I find useful awk commands to share.

Convert Linux .txt to Windows .txt
Replace the end of a line with a carriage. Useful for converting .txt files from Linux to Windows.

awk 'sub("$", "\r")' linuxfile.txt > windowsfile.txt

Convert Windows .txt to Linux .txt
And the reverse. Useful for converting .txt files from Windows to Linux.

awk '{ sub("\r$", ""); print }' windowsfile.txt> linuxfile.txt
Tagged awk, convert, example, linux, list, sub, txt, Windows
« Older Entries

Corrections? Questions? Comments?

Find an error?
Everything work out great for you?
Have some feedback?
Like to see something added to the article?

PLEASE leave us a comment after the article and let us know how we are doing, or if something needs corrected, improved or clarified.

Thank you!
- The Management

Advertisement

Sudo Bash
By Geeks - For Geeks

Back to Top