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; }

Update: I learned that Vim has built in diff support, even inside of .gz files. This means you can do vim -d /etc/orig.txt /var/tmp/quickfile-orig.txt-2022-08-03_08-13.gz and it will show you the diff.

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/ --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


Leave A Reply

Bash Prompt

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



if [ "$BASH" ]; then
        if [ "`id -u`" -eq 0 ]; then
                #Special prompt for root
                #Prompt for the rest of the users

export PATH PS1

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

wget -O /etc/profile.d/
Leave A Reply