A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://ryanstutorials.net/linuxtutorial/bonus.php below:

Linux Tutorial - Bonus Material

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:

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:

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:

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:

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:

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:

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:

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.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4