Linux Administration Lesson 12: Basic Unix Tools
Linux Administration Lesson 12: Basic Unix Tools
This lesson introduces commands to find or locate files and to compress files,
together with other common
tools that were not discussed before. While the tools discussed here are technically not considered filters,
they
can be used in pipes.
1. find
The find command can be very useful at the start of a pipe to search for files. Here are some examples. You
might want to add 2>/dev/null to the command lines to avoid cluttering your screen with error messages.
2. locate
The locate tool is very different from find in that it uses an index to locate files. This is a lot faster than
traversing all the directories,
but it also means that it is always outdated. If the index does not exist yet,
then you have to create it (as root on Red Hat Enterprise Linux) with the updatedb command.
[paul@RHEL4b ~]$ locate Samba
warning: locate: could not open database: /var/lib/slocate/slocate.db:...
warning: You need to run the 'updatedb' command (as root) to create th...
Please have a look at /etc/updatedb.conf to enable the daily cron job.
[paul@RHEL4b ~]$ updatedb
fatal error: updatedb: You are not authorized to create a default sloc...
[paul@RHEL4b ~]$ su -
Password:
[root@RHEL4b ~]# updatedb
[root@RHEL4b ~]#
Most Linux distributions will schedule the updatedb to run once every day.
3. date
The date command can display the date,
time,
time zone and more.
paul@rhel55 ~$ date
Sat Apr 17 12:44:30 CEST 2010
A date string can be customised to display the format of your choice. Check the man page for more options.
paul@rhel55 ~$ date +'%A %d-%m-%Y'
Saturday 17-04-2010
Time on any Unix is calculated in number of seconds since 1969 (the first second being the first second of the
first of January 1970). Use date +%s to display Unix time in seconds.
paul@rhel55 ~$ date +%s
1271501080
4. cal
The cal command displays the current month,
with the current day highlighted.
5. sleep
The sleep command is sometimes used in scripts to wait a number of seconds. This example shows a five
second sleep.
paul@rhel55 ~$ sleep 5
paul@rhel55 ~$
6. time
The time command can display how long it takes to execute a command. The date command takes only a little
time.
paul@rhel55 ~$ time date
Sat Apr 17 13:08:27 CEST 2010
real 0m0.014s
user 0m0.008s
sys 0m0.006s
The sleep 5 command takes five real seconds to execute,
but consumes little cpu time.
paul@rhel55 ~$ time sleep 5
real 0m5.018s
user 0m0.005s
sys 0m0.011s
This bzip2 command compresses a file and uses a lot of cpu time.
paul@rhel55 ~$ time bzip2 text.txt
real 0m2.368s
user 0m0.847s
sys 0m0.539s
7. gzip – gunzip
Users never have enough disk space,
so compression comes in handy. The gzip command can make files
take up less space.
paul@rhel55 ~$ ls -lh text.txt
-rw-rw-r-- 1 paul paul 6.4M Apr 17 13:11 text.txt
paul@rhel55 ~$ gzip text.txt
paul@rhel55 ~$ ls -lh text.txt.gz
-rw-rw-r-- 1 paul paul 760K Apr 17 13:11 text.txt.gz
8. zcat – zmore
Text files that are compressed with gzip can be viewed with zcat and zmore.
paul@rhel55 ~$ head -4 text.txt
/
/opt
/opt/VBoxGuestAdditions-3.1.6
/opt/VBoxGuestAdditions-3.1.6/routines.sh
paul@rhel55 ~$ gzip text.txt
paul@rhel55 ~$ zcat text.txt.gz | head -4
/
/opt
/opt/VBoxGuestAdditions-3.1.6
/opt/VBoxGuestAdditions-3.1.6/routines.sh
9. bzip2 – bunzip2
Files can also be compressed with bzip2 which takes a little more time than gzip,
but compresses better.
paul@rhel55 ~$ bzip2 text.txt
paul@rhel55 ~$ ls -lh text.txt.bz2
-rw-rw-r-- 1 paul paul 569K Apr 17 13:11 text.txt.bz2
1. Explain the difference between these two commands. This question is very important.
If you don't know the answer,
then look back at the shell lesson.
find /data -name "*.txt"
find /data -name *.txt
2. Explain the difference between these two statements.
Will they both work when there are 200 .odf files in /data ? How about when there are 2 million .odf files ?
find /data -name "*.odf" > data_odf.txt
find /data/*.odf > data_odf.txt
3. Write a find command that finds all files created after January 30th 2010.
4. Write a find command that finds all *.odf files created in September 2009.
5. Count the number of *.conf files in /etc and all its subdirs.
6. Two commands that do the same thing: copy *.odf files to /backup/ . What would be a reason to replace
the first command with the second ? Again,
this is an important question.
cp -r /data/*.odf /backup/
find /data -name "*.odf" -exec cp {} /backup/ \;
7. Create a file called loctest.txt. Can you find this file with locate ? Why not? How do you make locate find
this file ?
8. Use find and -exec to rename all .htm files to .html.
9. Issue the date command. Now display the date in YYYY/MM/DD format.
10. Issue the cal command. Display a calendar of 1582 and 1752. Notice anything special ?