Ryans Tutorials
More great tutorials at RyansTutorials

Bonus Material!

For those that want to go further.

Introduction

In this section I'll introduce a few bonus Linux commands and concepts. These are not essential for using the command line but are useful for those that want to take things a bit further. They will give you a feel for some of the more powerful things that Linux can do. I don't plan to cover each of these in detail. There will just be a brief description and some sample usage. Enough to give you the gist of what it does and how to start tinkering. If you're playing with this stuff then I figure you probably have enough understanding and ability to use this as a starter and figure the rest out for yourself by reading the man pages, experimenting and doing further research on Google. Explore and have fun!

Cron - Task Scheduling

Cron stands for Command Run ON. It is a mechanism that allows you to tell the system to run certain commands at certain times. So for instance, I could craft a script to check how much space is left on the disk that holds the home directories and send an email to certain people if the free disk space gets below a certain amount. Then I would use cron to get this script to run automatically every hour.

Cron allows a fair bit of control in terms of when commands are scheduled to run. Like most things in Linux, it is managed by way of a text file. The layout of the text file can be a little daunting but once you get the hang of it it's not that bad. There are many websites out there with good references to make it easy as well.

The file that manages cron is called a crontab. Each user may have their own crontab and within it each line represents a schedule for a particular command as follows:

* * * * * command to execute

Where the *'s represent (in order from left to right:

  • Minutes (0 - 59)
  • Hours (0 - 23)
  • Day of Month (1 - 31)
  • Months (1 - 12)
  • Day of week (0 - 7) (0 and 7 are Sunday)

A * in any of those places means every one of those increments.

Some examples:

* * * * * /bin/myscript.sh

Execute myscript.sh every minute.

30 3 * * 4 /bin/myscript.sh

Execute myscript.sh every Thursday at 3:30am.

To view a list of what tasks you currently have scheduled you may run the following command:

crontab -l

To edit your scheduled tasks, run the following command. It will open up in your default text editor which is normally Vim.

crontab -e

Xargs

Xargs is a useful tool to run a particular command for every item in a list. It is kinda hard to explain so a demonstration is probably the easiest way to illustrate it's usage.

Let's say we have a series of images in a directory and they have been named with an uppercase extension. We would like them all to be lowercase instead for consistency.

  1. ls
  2. image1.JPG image2.JPG image3.JPG image4.jpg
  3. basename -s .JPG -a *.JPG | xargs -n1 -i mv {}.JPG {}.jpg
  4. ls
  5. image1.jpg image2.jpg image3.jpg image4.jpg

Let's break it down:

  • Line 1 - Run ls to see what the current situation is.
  • Line 4 - Now the magic. it's a little complex so let's break it down again:
    • First we use the command basename. This command allows us to get the name with the suffix removed.
    • The -s option allows us to specify what the suffix is that should be removed.
    • The -a option allows us to specify multiple files. Basename is one of the few commands that doesn't support multiple files as default.
    • This will give us a listing of the files to be renamed but with the .JPG suffix removed.
    • Now we pipe that list into the command xargs.
    • The first option -n1 spcifies that the next command should be executed once for each item passed in through the pipe.
    • -i indicates we are going to use a replacement string in the next command (it defaults to {}). This means, execute the next command, substituting every occurrence of {} with what was just read through the pipe.
    • Finally we specify that the command to execute is mv and fill in the gaps accordingly.
  • Line 6 - We ran ls again just to verify that it did do what we thought it would.

Xargs has plenty more options available to it and plenty of opportunity. You often need a touch of creativity and enough persistence to tweak the command until it works.

Find

Find is a great tool for fine grained control over searching for files. Like with xargs, the man page is the best place to discover them all. I will illustrate a basic example below. Here we have a problem with disk space running low so we want to do a search for files over 200mb in the users home directories.

  1. find /home -size +200M -exec ls -sh {} \;
  2. 452M /home/barry/backups/everything.tar.gz
  3. 941M /home/lisa/projects/loony/servermigration.tar.gz
  4. 768M /home/mark/Documents/gregs_birthday.mpg
  5. ...

Let's break it down:

  • find is the program we are running.
  • /home is the directory to find in. By default it is a recursive search.
  • -size is a search option. Here we are saying find any files which are greater in size ( + ) than 200Mb. There are many search options and you can list as many as you like here. Find will list all files which match all of the given search options.
  • -exec tells find to execute the following command for each file it finds. Substitute {} for the actual file found. We end the exec option with ; but we have to put a \ in front of it to escape it's normal meaning on the command line. Find will list the files found in it's normal behaviour but we were also interested in seeing their sizes so we added this last bit to get nicer output.

Or maybe we are part of a team managing a server. It is behaving odd today yet we know it was working fine up until yesterday. We know that all the config files for the server are in the directory /etc so we suspect someone may have changed one of those. Let's do a search to find all files in that directory modified in the last 24 hours.

  1. find /etc -mtime -1
  2. /etc/hosts
  3. /etc/resolv.conf
  4. ...

Tar

Tar stands for Tape ARchive and is a popular means for combining and compressing several files into a single file. It was originally created for making backups on tape but is still commonly used today. Even though there are other options such as ZIP files for compression, tar remains popular as it is able to store filesystem information as well such as user and group permissions and dates.

Below I will present the basic forms I typically use for creating and extracting tar archive. Tar has many options however so it's worth a quick skim through the man page.

  1. ls
  2. image1.jpg image2.jpg image3.jpg
  3. tar -zcvf mytar.tar.gz *
  4. image1.jpg
  5. image2.jpg
  6. image3.jpg
  7. ls
  8. image1.jpg image2.jpg image3.jpg mytar.tar.gz
  9. rm *.jpg
  10. ls
  11. mytar.tar.gz
  12. tar -zxvf mytar.tar.gz
  13. image1.jpg
  14. image2.jpg
  15. image3.jpg
  16. ls
  17. image1.jpg image2.jpg image3.jpg mytar.tar.gz

Let's break it down:

  • Line 1 - Run ls to see what the current situation is.
  • Line 4 - Create the tar archive. z means use gzip to compress the resulting file. c means that we are creating a new archive. v means verbose and asks tar to list all the files it's adding to the archive. Finally f indicates that the result should be saved into a file (as opposed to being stored on a tape). Then we specify the file to create and after that the files to be added to it.
  • Line 16 - This time we are extracting files from the archive so replace c with x for extract and off we go.

Awk

Awk is a good program to use if you need to work with data that is organised into records with fields (such as the sample data below). It allows you to filter the data and control how it is displayed. If you're wondering about the name, it actually comes from the Initials of the 3 authors of the program.

Like find and xargs, it is very powerful but for most people it is very useful with just a brief understanding. If you need to do more complex data mangling, awk can do it but a popular alternative to look into is Perl.

  1. cat mysampledata.txt
  2. Fred apples 20
  3. Susy oranges 5
  4. Mark watermellons 12
  5. Robert pears 4
  6. Terry oranges 9
  7. Lisa peaches 7
  8. Susy oranges 12
  9. Mark grapes 39
  10. Anne mangoes 7
  11. Greg pineapples 3
  12. Oliver rockmellons 2
  13. Betty limes 14

So let's say we wanted to print out only the 2nd column.

  1. awk '{print $2}' mysampledata.txt
  2. apples
  3. oranges
  4. watermellons
  5. pears
  6. oranges
  7. peaches
  8. oranges
  9. grapes
  10. mangoes
  11. pineapples
  12. rockmellons
  13. limes

Let's break it down:

  • awk is the program we are running.
  • '{print $2}' is the action to take. We enclose it in single quotes so that none of the characters get interpreted with their special meaning by bash. The curly brackets ( {} ) state that this is an action. It is possible to set conditions outside of these and to have multiple actions depending on conditions. print tells awk to print something and $2 means the second column.
  • mysampledata.txt is the file to process.

Now let's say we want to print the order but only if the order is above 10. We also wish to reformat the output a little.

  1. awk '$3 > 10 {print $1 " - " $2 " : " $3}' mysampledata.txt
  2. Fred - apples : 20
  3. Mark - watermellons : 12
  4. Susy - oranges : 12
  5. Mark - grapes : 39
  6. Betty - limes : 14

Let's break it down:

  • $3 > 10 is a condition. We are saying perform the following action
  • For the action we can include multiple columns and other text we want printed enclosed by double quotes ( " ).

Secure Copy ( scp )

Scp is a quick, easy and secure way to copy files between different machines. It is part of the SSH (Secure SHell) suite of tools. With it you may copy files to and from your local machine and a remote machine.

To copy a local file to a remote machine:

  1. scp ./myfile.jpg username@192.183.243.10:/home/ryan/myfile.jpg
  2. The authenticity of host '192.183.243.10' can't be established.
  3. Are you sure you want to continue connecting (yes/no) yes
  4. Warning: Permanently added '192.183.243.10' to the list of known hosts.
  5. Password:
  6. myfile.jpg 100% 37KB 4.5KB/s 00:03

Let's break it down:

  • Line 1 - The first argument is the source. Here it is a local file so we provide a path (which may be absolute or relative). After that, the destination. Here the destination is a remote server. It is of the format username@server-address:path-to-location
  • Lines 2 - 3 - The first time you connect to a remote machine it will offer this message. You will need to type 'yes' (as opposed to just 'y').
  • Line 5 - It will ask you for your password. When you type your password in the cursor won't move. This is a security mechanism so people can't tell how many letters in your password.
  • Line 6 - This line will update during the file copy to let you know it's progress.

And the other way around:

  1. scp username@192.183.243.10:/home/ryan/myfilex.jpg ./myfiley.jpg
  2. Password:
  3. myfiley.jpg 100% 89KB 4.2KB/s 00:12

It is possible to copy several files in one go using wildcards. If wildcards are not practical then using Tar (above) to compress several files into a single file is also a good approach.

There are other tools available for copying files between machine such as FTP. These are often much more powerful than scp but as a result are more complex to use. If you just want to copy a small number of files then scp is probably the easiest way to go.