The exercises in this lab cover the usage of some basic system utilities that users and administrators alike need to be familiar with. Most of the commands are used in navigating and manipulating the file system. The file system is made up of files and directories.
The exercises will cover the usage of –pwd, cd, ls, rm, mv, ftp, cp, touch, mkdir, file, cat, find, and locate utilities.
The cat command is short for concatenate, meaning it strings files together. The command cat will also display the contents of an entire file on the screen. You will use cat along with the redirection symbol “>” to create a file.
ftp is a client program for using and connecting to FTP services via the File Transfer Protocol. The program allows users to transfer files to and from a remote network site. It is a utility you might need to use often.
In this exercise you will learn how to log on anonymously to an FTP server and download a file from the server using an ftp client program.
Note
You will need to have completed the exercises in a previous lab to be able to follow along in this particular exercise that needs an available FTP server running somewhere reachable.
Most of the utilities and commands you use in Linux send their output to the screen. The screen is called the standard output (stdout). Redirection allows you to send the output somewhere else – maybe a file.
Every program started on a Linux system has three open file descriptors, stdin (0), stdout (1) and stderr (2). You may redirect or pipe to "pipe" them individually. The redirection symbols are “>, < “
Now you will redirect the output of the file command into that same file. You want to find out the file type for the temp_file11 in the folder1 directory and send the output to your myredirects file:
If you want to prevent what happened above from happening you will use the double redirection symbol “>>”. This will append (add) the new output to the file instead of replacing it. Try it:
[root@localhostfolder1]# ls >> myredirects
Now examine the contents of the file myredirects again using cat.
Question
Write down its contents here:
Using redirection to suppress the output of a command¶
You will be using the concepts covered here a lot in Linux, so please pay particular attention to it. It can be a bit tricky.
There will be times when you don’t want the user to see the output of a command- perhaps an error message. This will usually be because strange error messages often scare regular users. In this exercises you will send the output of your commands to the null device ( /dev/null/ ). The null device is like a “bit bucket”. Anything you place inside disappears forever. You can also send (or redirect) regular command output to the null device "null device".
Ensure you are still in the folder1 directory. Use the long listing option of the ls command on temp_file11:
[root@localhostfolder1]# ls –l temp_file11
-rw-r--r--1rootroot0Jul2618:26temp_file11
You will redirect the output of the same command above (ls –l temp_file11) to the null device.
[root@localhostfolder1]# ls –l temp_file11 > /dev/null
You should have no output.
Now if you accidentally mis-spell the name of the file whose information you want to see; You will get:
[root@localhostfolder1]# ls –l te_file1
ls:te_file1:Nosuchfileordirectory
The above is the result of the type of error the ls command was programmed to give.
Run the same command as the above with an incorrect spelling of the file name and redirect it to /dev/null
[root@localhostfolder1]# ls -l te_file1 > /dev/null
ls:te_file1:Nosuchfileordirectory
Question
What happened here? How come the output still showed up on the screen (stdout)?
For various reasons you may want to suppress error message such as the one above. To do this type:
[root@localhostfolder1]# ls –l te_file1 > /dev/null 2>&1
You will not get any output.
This time the standard output as well as the standard error is suppressed.
The order of redirection is IMPORTANT!!
Redirection is read from left to right on the command line.
The left-most part of the redirection symbol - “>”: will send the standard output (stdout) to /dev/null. Then the right-most part of the redirection - “2>&1 “: will duplicate the standard error (2) to the standard output (1).
Hence the above command can be read as: redirect stdout(1) to “/dev/null” and then copy stderr (2) to stdout
To further demonstrate the importance of the order of redirection; Try:
[root@localhostfolder1]# ls –l tem_file 2>&1 > order.txt
Use the cat command to examine the contents of the file “order.txt”
The left-most part – “2>&1” will copy the standard error to the standard output. Then, the right-most part of the above – “ > order.txt” redirects stdout to the file order.txt.
Try this variation of the above step:
[root@localhostfolder1]# ls –l hgh_ghz 2> order2.txt > order2.txt
Question
Examine the file “order2.txt” and explain what happened?
To send the standard output and standard error to separate files; Type:
[root@localhostfolder1]# ls –l tep_f > standard_out 2> standard_err
Question
Two new files were created. What are the names of the files and what are their contents?
You can similarly redirect both stdout and stderr to the same file by using:
[root@localhostfolder1]# ls –l te_fil &> standard_both
You were again prompted to confirm the removal of each file in the directory and the directory itself. What option will you use with the rm –r command to prevent this?
This exercise will discuss two of the most popular utilities used for searching for files and directories on the file system. They are the find command and the locate commands.
The find utility has been around for along time. It recursively scans directories to find files that match a given criterion.
The general syntax for find is:
find[path][options][criterion][action]
If you do not specify any directory or path, find will search the current directory. If you do not specify a criterion, this is equivalent to "true", thus all files will be found. The find utility has many options for doing just about any type of search for a file. Only a few of the options, criteria and actions are listed below.
Again from the above command, what is the “option”, what is the “path”, what is the “criterion” and finally what is the “action”? (HINT: The action = “- print”)
The search will only be performed 3 directories deep from the “/” directory.
The asterisk used in the command above is one of the “wild card” characters in Linux.
The use of wild-cards in Linux is called “globbing”.
Use the find command to find all files in your “pwd” that are “smaller” than 200 kilobytes in size. Type:
[root@localhostroot]# find . –size -200k
Use the find command to find all the find all the files in your pwd that are “larger” than 10 kilobytes and display their “file type” as well. Type:
The syntax for the find command can be rather difficult to use sometimes; and because of its extensive search, it can be slow. An alternative command is locate.
locate searches through a previously created database of all files on the file system.