Category Archives: Bash Scripting

Bash Scripting in Linux, custom bash scripts for automating, logging, monitoring, etc your Linux server

How to force copy in Linux… force cp linux

Most users would expect to run a command like the following:

cp -rf <filename> <filename>

But the “f” option doesn’t always work. The reason is doesn’t always work is because it is aliased. To check what commands are aliased type “alias” at you command line. Please be aware that depending on which user you are logged in as your alias will change based on the user. In order to run a command that is aliased you will need to run it as follows:

/bin/cp -rf <filename> <filename>

By escaping the alias you will be able to use the -f –force option with cp.


crontab syntax: Using the crontab in Linux

Here is my overview for: crontab sytanx, crontab and cronjobs…

Alright so you have a script that you want to automate, you will need to know a little about crontab syntax. To do so you will also need to understand a few things about the Linux crontab and cronjob. I will give a very high level explanation regarding crontab syntax, crontab and cronjob.  I have only tested this method on REDHAT based systems (REDHAT, CENTOS and FEDORA CORE). You will also require experience with the text editor program “vi.” You will use the following commands:

i = insert, this allows you to edit the crontab
esc = removes you from insert mode,
:w! = save changes
:q! = quit without saving changes (if any were made)
:wq! = “w” tells the editor that you want to “write” and “quit” (w = write, q = quit), “!” execute the command forcefully

Each user has a crontab, in the crontab you can schedule cronjob’s. The cronjob’s can run at multiple times through out the day, week, month and year. To list your current crontab type the following:

[yourusername@server ~]$ crontab -l
no crontab for yourusername

The above message indicates that I have no scheduled cronjob’s in my crontab. If I want to automate a script I would have to add a cronjob to the crontab. To add a cronjob type the following at the command line: (you will need to use commands from the text editor “vi”)

[yourusername@server ~]$ crontab -e

The above code will open the crontab for editing, you can now create a cronjob. Below is a break down of how the crontab works – an overview of how you can schedule automation:

*     *   *  *   *  command to be executed
–     –    –   –  –
|     |     |   |    |
|     |     |   |    +—– day of week (0 – 6) (Sunday=0)
|     |     |   +——- month (1 – 12)
|     |     +——— day of month (1 – 31)
|     +———– hour (0 – 23)
+————- min (0 – 59)

Now what the above means is that you can schedule your cronjob by minute/hour/day-of-month/month/day-of-week. So if I wanted to schedule my “backup.bsh” script to run every day at 7PM I would enter the following:

0 19 * * * /home/yourusername/bin/backup.bsh

Once I have entered the above cronjob I can save my changes and exit. The cronjob is now setup to run via the crontab every night at 7PM. If I saved the crontab properly and I do a listing of my crontabs cronjobs I should see:

[yourusername@server ~]$ crontab -l
19 0 * * * /home/yourusername/bin/backup.bsh

If you want you can add comments to the crontab, this will help you understand what each cronjob is doing. To add a comment run the “crontab -e” command again and place the comment above the cronjob, like so:

[[email protected]server ~]$ crontab -l
# This script will run at 7PM every day, it runs backup.bsh
19 0 * * * /home/yourusername/bin/backup.bsh

There now we are done! As always if you have questions or feel as though I missed something please leave a comment, I would be happy to learn of different and even better ways to use the crontab and cronjob.

tar: Removing leading `/’ from member names


Here is how to remove the message:  tar: Removing leading `/’ from member names when tarring files.

This is your normal command which will produce a tar file but give you the message: tar: Removing leading `/’ from member names

# create a tar file of the directory /etc called foo.tar
tar -tvf etc.tar /etc # TAR FILE
# create a tar.gz file of the directory /etc called foo.tar.gz
tar -vczf etc.tar.gz /etc # TAR GZIP FILE
# create a tar.bz2 file of the directory /etc called foo.tar.bz2
tar -cjf etc.tar.bz2 /etc # TAR BZIP FILE

This is the option in tar you use to prevent the message
# remove the message like with the -C option
tar -tvf etc.tar -C / etc # TAR FILE WITHOUT MESSAGE
tar -vczf etc.tar.gz -C / etc # TAR GZIP FILE WITHOUT MESSAGE
tar -cjf etc.tar.bz2 -C / etc # TAR BZIP FILE WITHOUT MESSAGE

  • C: When this option is specified, `tar’ will change its current directory to DIR before performing any operations. When this option used during archive creation,  it is order sensitive.

If you would like to list the contents of the newly created tar.bz2 file run this command:
tar -tvf etc.tar # LIST THE CONTENTS OF A TAR FILE
tar -ztvf etc.tar.gz # LIST THE CONTENTS OF A GZIP FILE
tar -jtvf etc.tar.bz2 # LIST THE CONTENTS OF A BZIP FILE

  • t: List the contents of an archive
  • v: Verbosely list files processed (display detailed information)
  • z: Filter the archive through gzip so that we can open compressed (decompress) .gz tar file
  • j: Filter archive through bzip2, use to decompress .bz2 files.
  • f filename: Use archive file called filename

For additional information on tar see the man pages.

scp without password… passwordless scp

How to transfer files using scp without a password (secure copy):

1) On the Source Host run the following command as the user which will be scping the files to the remote host:
*** leave the paraphrase blank

ssh-keygen -t rsa

2) Once the key has been generated copy the to the remote-host machine you are scping to:

scp <user-name>@<remote-host>:/<remote-directory>

scp [email protected]_host:/home/joe/

3) Now on the remote-host you need to add the to the authorized_keys file (located /home/<user>/.ssh/). if the
directory does not exist create it (this assumes you are in the users home directory, an “ls -la” will show all files and directories in the current directory – including the hidden ones, which the .ssh is)

mkdir .ssh (IF NEEDED – must be in the /home/<user-name>)

cat >> .ssh/authorized_keys
chmod –recursive 700 .ssh/

4) Try copying a file to the remote-host, you should not need to enter the password. MAKE SURE NO ONE GETS A HOLD OF THE KEY you made.

scp <some-file.txt> <user>@<remote-host>:/<some_directory>

tar and gzip

tar and gzip… So you want to tar and then compress with gzip?  tar and gzip in one step… Here is how you do it, please check out the tar and gzip man pages: (this is how I tar and gzip my files, I am not responsible for anything that happens on your machine)

# EXAMPLE CODE TEMPLATE (the dash is a double dash, not single)
tar –verbose –create –gzip –file <filename>.tar.gz <from-filename>

# EXAMPLE CODE (the dash is a double dash, not single)
tar –verbose –create –gzip –file textfile.tar.gz textfile.txt

# EXAMPLE CODE (run and “ls -l” and you should see the following files)
ls -l