≡ Menu


Bash History: Correct / Repeat The Last Command With a Substitution

I often type the wrong command as follows under Linux bash shell:
rsync -av dir1 user@server1.cyberciti.biz:/path/to/dir1
cp -av file1*.txt file2*.txt /path/to/dest

I need to correct those command as follows:

rsync -av dir5 user@server1.cyberciti.biz:/path/to/dir1
cp -av delta1*.txt delta2*.txt /path/to/dest

How do I replace dir1 with dir5 or file with delta and repeat the last command under bash shell?
[click to continue…]

Bash: Get The Last Argument Passed to a Shell Script

I'm writing a bash wrapper script that will pass arguments to the command. I need to find out the last argument if I call the wrapper as follows:

./wrapper -a -b --longarg=foo thisfilename.txt
./wrapper -a -b thisfilename.txt
./wrapper -a --next=true thisfilename.txt

=> $@ is all of them.
=> $0 is script name.
=> $1 is first arg.

I want thisfilename.txt stored in a shell variable called $last. How do I find the last argument passed to a shell script written in bash or ksh under Unix like operating systems?
[click to continue…]

Linux / Unix: Sed Substitute Multiple Patterns [ Find & Replace ]

I'm using the date +'%D_%T' to store Unix system date and time in a shell variable called $_now:

_now="$(date +'%D_%T')"
echo $_now


I'd like to replace / and : with _. I'm aware of the following sed command:

sed 's/\//_/g
> s/:/_/g' < <<"$_now"


How do I specify two pattern within the same sed command to replace | and : with _ so that I can get output as 01_20_12_16_10_42?
[click to continue…]

Bash C Style For Loop Example and Syntax

How do I use the bash C style for loop under UNIX or Linux operating systems?
[click to continue…]

Linux / UNIX: Run Command a Number of Times In a Row

How do I run "foo" command 10 times (or n times) under Linux or UNIX like operating systems?
[click to continue…]

Linux: TMOUT To Automatically Log Users Out

How do I auto Logout my shell user in Linux after certain minutes of inactivity?
[click to continue…]

Bash Remove Last Character From String / Line / Word

I have a file of that looks as follows:

foo bar
tom jerry
UNIX Linux

Each word and/or Linux is a different length. How do strip or remove the last character from each line using bash or ksh shell only on a Linux or Unix-like systems?
[click to continue…]

UNIX: Lynx Browser Set / Change the Default Home Page

How do I change the default home page with Lynx browser under UNIX or Linux operating system? How do I set the startup page to http://google.co.uk with Lynx?
[click to continue…]

Linux / UNIX: Run Commands When You Log Out

I've written a Perl script that connects to our central server for me and it allows me feed data so that I make a timesheet later. How do I run my script when I log out from Apple OS X or Linux / UNIX workstation using bash shell?
[click to continue…]

Bash String Concatenation

How do I join two strings under BASH? I have two strings stored in two different variables (_p=/delta and _u=aqua) and how do I join this and assign it to another variable in bash?
[click to continue…]