Showing entries with tag "Bash".

Found 5 entries

Bash: Quick backup

Often I will need to take a quick backup of a directory or file before I make some changes. Sometimes I will need multiple backups, so I include the date/time in the output filename. After doing it manually enough I wrote a quick bash function that will handle creating the backup file, and naming it appropriately. Put these in your ~/.bashrc or copy and paste to your terminal

# Quick tar/file commands
qtar() { C=$(basename $1); O=/var/tmp/quicktar-$C-$(date +%F_%H-%M).tar.gz; tar -cvpaf $O $*; echo; ls -sh $O; }
qfb()  { C=$(basename $1); O=/var/tmp/quickfile-$C-$(date +%F_%H-%M).gz; gzip $1 -c > $O; ls -sh $O; }

Then to run it simply invoke it like this:

# Multiple files or directories
qtar file1.txt file2.txt
qtar /path/to/dir
qtar files/

# One file
qfb /etc/dhcp/dhcpd.conf
qfb /etc/php.ini

This will make an appropriately named file in /var/tmp/ with the files/dirs you listed tarred and gzipped up. It's not perfect, there are some corner cases that will break it, but I needed something super simple to drop on remote servers for quicky backups.

Update: Less common, but I also whipped up a zip option.

qzip() { C=$(basename $1); O=/var/tmp/quickzip-$C-$(date +%F_%H-%M).zip; zip -9vr $O $*; ls -sh $O; }
Leave A Reply

Bash: Using previous command's parameters

Bash allows you to save some typing by referencing the previous command's parameters via a variable. If you want to reference the first parameter from the previous command you would use !^ and if you want the last parameter you would use !$. A real world example would be something like this:

mkdir /long/dir/structure/new_dir
cd !$

This would make a new directory and then change to that directory. Alternately you can reference numbered parameters using !:2 to reference the 2nd parameter.

Leave A Reply

Bash: Use previous expression parameters

I was running a bash command similar to this:

perl /var/www/scott/perl/test/scott.pl --size 10 --file /tmp/output.txt

and afterwards I wanted to make a small change to that script. Rather than typing out the full path you can use some Bash history expansion and do this instead:

vim !:1

If you use !: followed by a number, Bash will replace that with the Xth (starting at zero) parameter from the previous command.

Leave A Reply

Bash: History size and timestamp

Bash defaults to logging 1000 entries to the history. I increased that limit by adding the following line to my ~/.bashrc

export HISTSIZE=5000           # the bash history should save 5000 commands
export HISTTIMEFORMAT="%F %T " # Store the timestamps of each command also

Previously

Leave A Reply

Bash Prompt

This is my little mini script to put in /etc/bashrc to make your bash prompt colorful!

black="\033[0;30m"
red="\033[1;31m"
white="\033[1;37m"
blue="\033[0;34m"
green="\033[1;32m"
cyan="\033[1;36m"
yellow="\033[1;33m"
purple="\033[0;35m"
brown="\033[0;33m"
gray="\033[0;37m"
reset="\033[0m"

rootc=$red
userc=$cyan
hostc=$white
dirc=$yellow

if [ "$BASH" ]; then
        if [ "`id -u`" -eq 0 ]; then
                #Special prompt for root
                prompt="$rootc\u$gray@$hostc\h$dirc(\w)$reset\n:"
                PS1=$prompt
        else
                #Prompt for the rest of the users
                prompt="$userc\u$white@$hostc\h$dirc(\w)$reset\n:"
                PS1=$prompt
        fi
fi

export PATH PS1

On modern version of Fedora you can put this script in your /etc/profile.d directory

wget -O /etc/profile.d/bash_prompt.sh http://www.perturb.org/code/bash_prompt.sh
Leave A Reply