Tutorial 7
In this tutorial, you will learn some advanced file operations in UNIX. In the beginning of this tutorial, the file compression and de-compression commands in UNIX are presented. Then you will learn some operations on file alias and searching. The commands for using ftp in UNIX are illustrated in the end of this tutorial.
There are examples in every section of this tutorial. The examples have been done using the TA’s directories that you may not be able to access. But if you like you can redo the examples by changing the directories to those you own.
The Archival Program
For sending a group of files to someone either by FTP or email, it helps to combine them into a single filed called an archive. The tar (tape archiver) command which does the thing. Only one option can be used at a time:
-c Creates an archive
-x Extracts files from archive
-t Displays files in archive
In addition, we will frequently use two options: -f for specifying the name of the archive and -v to display the progress. Here, we create a file archive, archive.tar, from two uncompressed files:
{cs1:~} tar -cvf archive.tar foo.sh foo.bak
a foo.sh 1K
a foo.bak 1K
To extract the archive:
{cs1:~} tar -xvf archive.tar
tar: blocksize = 6
x foo.sh, 5 bytes, 1 tape blocks
x foo.bak, 5 bytes, 1 tape blocks
To view the archive:
{cs1:~} tar -tvf archive.tar
tar: blocksize = 6
-rwx--x--x 92977/215 5 Aug 30 18:22 2006 foo.sh
-r-x--x--x 92977/215 5 Sep 10 12:32 2006 foo.bak
The Compression Program
gzip compresses a file and gunzip decompresses it. For example:
{cs1:~} gzip infile
This creates a gzipped file named "infile.gz". To view the amount of compression we can use -l option:
{cs1:~} gzip -l infile.gz
compressed uncompr. ratio uncompressed_name
109 103 18.4% infile
To uncompress a gzipped file we can use either of the two 'gzip -d' or gunzip commands. For example,
{cs1:~} gzip -d infile.gz
Or
{cs1:~} gunzip infile.gz
An additional layer of compression helps bring down the file size, the reason why gzip is ofter used with tar for creating a compressed archive. Here we gzip the file archive.tar that was created before with tar:
{cs1:~} gzip archive.tar
This creates a "tar-gzipped" file, archive.tar.gz. To extract the file we can use:
{cs1:~} gunzip archive.tar.gz
The Compression and Archival Program
The 'zip; command combines the compressing function of gzip with the archival function of tar. zip requires the first argument to be the compressed filename, the remaining arguments are interpreted as files and directories to be compressed. For example,
{cs1:~} zip archive.zip foo.sh infile
adding: foo.sh (stored 0%)
adding: infile (deflated 18%)
To view the content of the archive, we can use the command 'unzip' with the option -v:
{cs1:~} unzip -v archive.zip
Archive: archive.zip
Length Method Size Ratio Date Time CRC-32 Name
------ ------ ---- ----- ---- ---- ------ ----
5 Stored 5 0% 08-30-06 18:22 82ad0086 foo.sh
103 Defl:N 84 18% 09-10-06 14:45 f5e139a3 infile
------ ------ --- -------
108 89 18% 2
To decompress the compressed archive, we have to use:
{cs1:~} unzip archive.zip
Archive: archive.zip
replace foo.sh? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
extracting: foo.sh
replace infile? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
inflating: infile
unzip does the decompression interactively in case a file with same name exists.
Creating Links
The 'ln' command links a file and thus provides it with an alias. The link count is increased by one and maintained in the inode. The command has a syntax similar to 'cp'. Suppose we have a file 'date.sh' and we want to link this file with the name 'who.sh':
{cs1:~/fperm} ls -li date.sh
216061441 -r--r--r-- 1 mfh062000 sn 5 Sep 11 17:15
date.sh
Link count is 1
{cs1:~/fperm} ln date.sh
who.sh
The link is being created here
{cs1:~/fperm} ls -li date.sh who.sh
216061441 -r--r--r-- 2 mfh062000 sn 5 Sep 11 17:15
date.sh
Both of them has link count of 2
216061441 -r--r--r-- 2 mfh062000 sn 5 Sep 11 17:15 who.sh
To remove a link, we have to use 'rm':
{cs1:~/fperm} rm who.sh
rm: remove who.sh (yes/no)? y
{cs1:~/fperm} ls -li date.sh
216061441 -r--r--r-- 1 mfh062000 sn 5 Sep 11 17:15 date.sh
The links we created so far was hard links. To create symbolic links, we have to use 'ln -s':
{cs1:~/fperm} ln -s date.sh date.sym
{cs1:~/fperm} ls -li date.sh date.sym
216061441 -r--r--r-- 1 mfh062000 sn 5 Sep 11 17:15 date.sh
216061442 lrwxrwxrwx 1 mfh062000 sn 7 Sep 11 17:46 date.sym -> date.sh
Locating Files
'find' command is a powerful tool for locating files. Its syntax is:
find path_list selection_criteria action
Suppose, we want to find the file named 'date.sh' in our home directory tree. We have to use:
{cs1:~} find $HOME -name date.sh -print
/net/core/export/home/cs/001/m/mfh062000/fperm/date.sh
Here, we are selecting file by name. Other criteri, such as file type (-type), file permissions (-perm) can also be used:
{cs1:~} ls -li fperm/
total 4
216061441 -r--r--r-- 1 mfh062000 sn 5 Sep 11 17:15 date.sh
216061442 -r-------- 1 mfh062000 sn 5 Sep 11 17:50 date2.sh
{cs1:~} find ~ -inum 216061441
/net/core/export/home/cs/001/m/mfh062000/fperm/date.sh
{cs1:~/TA} find . -type d
.
./TA Works
./PrinciplesOfUnix
./PrinciplesOfUnix/Tutorials
./PrinciplesOfUnix/Tutorials/Tutorial1
./PrinciplesOfUnix/Tutorials/Tutorial1/tutorial1_files
./PrinciplesOfUnix/Tutorials/Tutorial2
./PrinciplesOfUnix/Tutorials/Tutorial2/tutorial2_files
{cs1:~/TA} find . -perm 640
./FAll 2006 - TA LAB SIGN UP SHEET.doc
./TA Office Location - Fall 06.xls
./TA Works/stdinfo.txt
./TA Works/GradeSheet.xls
Files tend to build up incessantly on disk. To clean them up, we need to know which files have not been accessed or modified for a specified time. The '-mtime' (modification time) and '-atime' (access time) selection criteria come to enormous help in this regard:
{cs1:~/fperm} find . -mtime -2 -print
.
./date.sh
./date2.sh
Here, -2 means less than 2 days. If we want to specify more than 2 days, we have to use +2. If we want to specify exactly two days, we have to use '2'. Similarly, we can find files based on access time:
{cs1:~/fperm} find . -atime -1 -print
.
./date.sh
./date2.sh
find has three operators. The '!' operator negates an options meaning:
{cs1:~/Installers} ls
FirefoxGoogleToolbarSetup.exe googletalk-setup.exe
MinGW-5.0.3.exe mingw-runtime-3.10.tar.gz
QuickTimeInstaller.exe mingw.ini
SFTPMSI.exe mingw32-make-3.80.0-3.tar.gz
SkypeSetup.exe mirrorlist
binutils-2.15.91-20040904-1.tar.gz msgr8us.exe
gcc-core-3.4.2-20040916-1.tar.gz w32api-3.7.tar.gz
gcc-g++-3.4.2-20040916-1.tar.gz winamp524_lite.exe
gcc-java-3.4.2-20040916-1.tar.gz
{cs1:~/Installers} find . ! -name "*.exe"
-print
Find all files except the .exe ones
.
./mirrorlist
./mingw.ini
./mingw-runtime-3.10.tar.gz
./w32api-3.7.tar.gz
./binutils-2.15.91-20040904-1.tar.gz
./gcc-core-3.4.2-20040916-1.tar.gz
./gcc-g++-3.4.2-20040916-1.tar.gz
./gcc-java-3.4.2-20040916-1.tar.gz
./mingw32-make-3.80.0-3.tar.gz
The '-o' operator ORs the selection options:
{cs1:~/Installers} ls
FirefoxGoogleToolbarSetup.exe googletalk-setup.exe
MinGW-5.0.3.exe mingw-runtime-3.10.tar.gz
QuickTimeInstaller.exe mingw.ini
SFTPMSI.exe mingw32-make-3.80.0-3.tar.gz
SkypeSetup.exe mirrorlist
binutils-2.15.91-20040904-1.tar.gz msgr8us.exe
gcc-core-3.4.2-20040916-1.tar.gz w32api-3.7.tar.gz
gcc-g++-3.4.2-20040916-1.tar.gz winamp524_lite.exe
gcc-java-3.4.2-20040916-1.tar.gz
{cs1:~/Installers} find . \( -name "*.exe" -o -name
"*1.tar.gz" \)
-print
Find executable files or files whose name end with 1.tar.gz
./FirefoxGoogleToolbarSetup.exe
./QuickTimeInstaller.exe
./msgr8us.exe
./MinGW-5.0.3.exe
./binutils-2.15.91-20040904-1.tar.gz
./gcc-core-3.4.2-20040916-1.tar.gz
./gcc-g++-3.4.2-20040916-1.tar.gz
./gcc-java-3.4.2-20040916-1.tar.gz
./googletalk-setup.exe
./SkypeSetup.exe
./winamp524_lite.exe
./SFTPMSI.exe
The '-a' operator ANDs the selection options:
{cs1:~/Installers} ls
FirefoxGoogleToolbarSetup.exe googletalk-setup.exe
MinGW-5.0.3.exe mingw-runtime-3.10.tar.gz
QuickTimeInstaller.exe mingw.ini
SFTPMSI.exe mingw32-make-3.80.0-3.tar.gz
SkypeSetup.exe mirrorlist
binutils-2.15.91-20040904-1.tar.gz msgr8us.exe
gcc-core-3.4.2-20040916-1.tar.gz w32api-3.7.tar.gz
gcc-g++-3.4.2-20040916-1.tar.gz winamp524_lite.exe
gcc-java-3.4.2-20040916-1.tar.gz
{cs1:~/Installers} find . \( -name "*.exe" -a -atime -4 \)
-print
Find executables which has been accessed within last 4 days
./FirefoxGoogleToolbarSetup.exe
./QuickTimeInstaller.exe
./msgr8us.exe
./MinGW-5.0.3.exe
./googletalk-setup.exe
./SkypeSetup.exe
./winamp524_lite.exe
./SFTPMSI.exe
Logging on FTP
At your UNIX console prompt type “ftp” to open a connection to an ftp server.
Note: Due to
the fact that ftp is an unsecured connection method, all ftp servers and services
have been disabled in the Computer Science Dept [and UTD]. Only sftp, Secure
FTP, is available on all the Unix servers: cs1, cs2, etc. So you can use “sftp”
instead of “ftp” to establish a connection.
//First log on UNIX (or suing consoles like putty, telnet, etc), then type the following
{cs1:~} sftp cs1.utdallas.edu
//And you will see something like
this
Connecting to
cs1.utdallas.edu...
University of Texas at Dallas
Information Resources
Pursuant to Texas Administrative Code 202:
(1) Unauthorized use is prohibited;
(2) Usage may be subject to security testing and monitoring;
(3) Misuse is subject to criminal prosecution; and
(4) No expectation of privacy except as otherwise provided by
applicable privacy laws.
Password:
// Then type in your password and
press “ENTER”, and you will see the sftp prompt “sftp”
sftp>
Changing Remote Directory (Server Side) using
FTP
You can using “cd” to change your
remote directory.
When you are in the ftp mode in UNIX,
type the following commands. (You may want to list all the directories using “dir”
first).
sftp> dir
.
..
Mail
permission
public_html
qs.sh
qunsorted
sorted
(etc.)
sftp>
sftp> cd .
sftp>
//And then your working directory on
the server side will be the main folder (for me it is “/net/core/export/home/cs/001/k/kxt056000”). Node that the ftp prompt remains the same during
the connection regardless of whatever directory your are working with.
Changing Local Directory (Client Side) using
FTP
To change the local directory, you need to use command “lcd”.
Note: If you
are using a remote console like putty or telnet to log on the UNIX server, the “lcd”
command will only change the local directory on the UNIX server (the FTP client
side), not any directory on your computer (the UNIX client side) on which you
logged into the UNIX server.
//For example, at the ftp prompt, type the following. (Note that this is the TA’s directory and you may not be able to access. So you shall “lcd” to another directory that you can access.)
sftp>lcd /net/core/export/home/cs/001/k/kxt056000/Mail
//Then my working directory on the UNIX server is changed to /net/core/export/home/cs/001/k/kxt056000/Mail.
Using the Binary Mode in FTP
You must always change to Binary Mode using “bin” if you are going to transfer files (other than text file format) between you machine and an ftp server if you are in the UNIX ftp environment.
//At the ftp prompt, type the following command to change to binary mode
sftp>bin
//Now you are done.
Downloading Files using FTP
You can download files from the ftp server using the “get” command.
//At the ftp prompt, type the following command to download a file.
sftp> dir
.
..
Mail
permission
public_html
qs.sh
qunsorted
sorted
(etc.)
sftp> get qs.sh
Fetching
/net/core/export/home/cs/001/k/kxt056000/qs.sh to qs.sh
//Then the file “qs.sh” are
downloaded into my local working directory “/net/core/export/home/cs/001/k/kxt056000/”
from my remote working directory “net/core/export/home/cs/001/k/kxt056000/Mail”.
Uploading Files using FTP
You can upload files to the ftp server using the “put” command. Node that you must upload a file to an ftp server directory that you are allowed to upload files to.
//At the ftp prompt, type the following command to upload a file.
sftp> put sorted
Uploading sorted to
/net/core/export/home/cs/001/k/kxt056000/sorted
/Then the file “sorted” is uploaded from
my local working directory “/net/core/export/home/cs/001/k/kxt056000/” into
my remote working directory “net/core/export/home/cs/001/k/kxt056000/Mail”.
Closing FTP Connection
Use “bye” or “quit” to close an ftp
connection.
//e.g.
sftp>quit
//or
sftp>bye
Get Help
//Type “help” to get help on commands.
For Advanced FTP Commands