Searched for tag Bash and found 7 results in 0.7 ms

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.zst; 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.

Tags:
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
Tags:
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

Tags:
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.

Tags:
Leave A Reply

Bash command arguments

If you use Bash at the command-line you have probably found a need for some shortcut keys. If you have a long command like:

ls -la --size /some/long/path/to/file.txt

Often in a follow up command you will want to reference the previous command's arguments. In this example if you want to reference the file from the previous command you either have to type it all out again, copy and paste the previous argument, or use a Bash shortcut.

Shortcut Description
!! entire previous command
!$ last argument of previous command
!^ first argument of previous command
!:2 second argument
!:1-3 arguments 1 through 3
!* all arguments

You could use the !$ shortcut to reference the last parameter from the previous command

vim !$

This is a lot less typing, and quicker. The ^ and $ also line up nicely with the logic from regular expressions: first and last. Another real world example that I run into is creating a directory and then changing into it:

mkdir /var/tmp/user/path
cd !$
Tags:
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.

Tags:
Leave A Reply

Linux: SFTP and SCP friendly login banners

To display a message when a user logs into your server, add the following to ~/.bashrc:

# If it's an interactive terminal show the banner
if [[ $- == *i* ]]; then
    echo "Welcome to the monitoring server"
    echo "Configuration is stored in /etc/myapp"
fi

This runs only for interactive sessions. Non-interactive connections such as SFTP and SCP remain unaffected, which prevents automated processes from breaking.

See also: The text_color project.

Tags:
Leave A Reply