Avoid Confirmation while Copying Files

While copying files, using cp command you would have noticed that it always asks for the confirmation overwriting files like below.

[root@server ~]# cp -v test.txt example.txt
cp: overwrite ‘example.txt’?

Will see how you can avoid that – but before that let’s see why it happens.

This happens because of alias set in the .bashrc file.

[root@server ~]# cat .bashrc
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

you can remove alias in .bashrc file – and you won’t be asked for confirmation ever again.

but if you want to skip confirmation for time being then you can call the cp command directly from bin – Example.

[root@server ~]# /bin/cp -v test.txt example.txt
‘test.txt’ -> ‘example.txt’
[root@server ~]#

Rsync command examples

So what is Rsync?
Rsync is a Linux tool which helps is syncing a file from a server A to server B and vice versa. Rsync is powerful tool which numerous options and we will see some of the examples below.

Rsync Command to copy files in the destination.

So the basic command would look like this.

[root@server ~]#  rsync [option] user@host:[file/directory origin] [destination]

[root@server ~]# rsync -avzh /home/user/backups/ user@backup01:/home/backup/

So above command will copy contents in the backups folder to the remote destination – Important note if you don’t put trailing / in the /home/user/backups it will copy whole folder instead of just contents in it.

Further to explain above command:-
-a stands for copy files recursively and preserve ownership of files when files are copied
-v stands for runs the verbose version of the command; all of the processes that are run will be written out for the user to read
-z stands for compress the data synced
– h stands for produce easily readable output, not just numbers

Show Progress:-

If you are doing a large transfers and want to see progress, you can use –progress option.

[root@server ~]# rsync -avzh --progress /home/user/backups/ user@backup01:/home/backup/

Exclude Files/folders from syncing.

Create a file call exclude.txt and add all the files and folders you want to exclude e.g:-

rsync -avzh --exclude-from 'exclude.txt' sync/

Basic AWK command examples.

Along with SED command Linux – AWK is powerful command line tool which would help you to perform a lot of actions on your data. It can help you to sort, print and process your data in the way you like. You can use AWK to read and edit the file.

AWK is mostly used in the Shell Scripts. Lets check out Awk without wasting more time 🙂

Let’s create a file called data.txt with the following contents – so we can learn AWK command better with examples.

Bob America Married 50
Jay India Unmarried 25
Abdul UAE married 33
Mona London Married 28
John America Unmarried 18
Neha India Unmarried 16

Run the command below which will print the contents in the file as it is.

[root@server ~]# awk '{print}' data.txt

Bob America Married 50
Jay India Unmarried 25
Abdul UAE married 33
Mona London Married 28

Print all the lines Matching with America.

[root@server ~]# awk '/America/ {print}' data.txt
Bob America Married 50
John America Unmarried 18

Only Print age from the file. As you can see that Age is written in the 4 column so we will use the command below.

[root@server ~]# awk '{print $4}' data.txt
[root@server ~]#

So now if you want to print Name and Age.

[root@server ~]# awk '{print $1, $4}' data.txt
Bob 50
Jay 25
Abdul 33
Mona 28
John 18
Neha 16

You can print age first by changing the order.

[root@server ~]# awk '{print $4, $1}' data.txt
50 Bob
25 Jay
33 Abdul
28 Mona
18 John
16 Neha
[root@server ~]#

Conditional Printing

I would like to print the names of the people who live in America. So here you will use If Condition in awk. In the example below it will check if the America is present in the column 2 it will print the column 1

[root@server ~]# awk '{ if ($2 == "America") print $1;}' data.txt

In the above example, if you just put awk ‘{ if ($2 == “America”) print;}’ data.txt it will print the complete line.

AWK can do much more that what I have specified above. I will come up with Advanced AWK article soon.

Echo Command in Linux

Echo command mainly prints on the screen whatever you ask it. It a simple function but most the scripts would be incomplete with echo command. You won’t be able get visible output from the shell script, without echo command.

How to check what version of echo command you are using?

[root@server ~]# /bin/echo --version
echo (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Brian Fox and Chet Ramey.

How to print using echo command ?

it’s actually quite simple. Just use echo command following by the text.

[root@server ~]# echo "Hello JustGeek"
Hello JustGeek
[root@server ~]#

Printing Variables.

it’s not just for the print text there are many things you can do using echo command. Example you can print Variables.

[root@server ~]# IamVariable="This is JustGeek"
[root@server ~]# echo $IamVariable
This is JustGeek
[root@server ~]#

You can also run a command through echo command and print it’s output to the screen.

[root@server ~]# echo "This server is online from $(uptime | awk '{print $3,$4}')"
This server is online from 77 days,

Formatting using Echo Command.

To use formatting functions with echo -e. It enables interpretation of backslash escapes.
eg:- “\n” parameter will print on the new line. As shown below.

[root@server ~]# echo -e "First Line\nSecond Line"
First Line
Second Line
[root@server ~]#

There are multiple characters which can be used which suits you.

\a: Alert (historically known as BEL). This generates the default alert sound.
\b: Writes a backspace character.
\c: Abandons any further output.
\e: Writes an escape character.
\f: Writes a form feed character.
\n: Writes a new line.
\r: Writes a carriage return.
\t: Writes a horizontal tab.
\v: Writes a vertical tab.
\\: Writes a backslash character.