OPERATING SYSTEMS LINUX - os.ucg.ac.me SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

  • View
    218

  • Download
    2

Embed Size (px)

Text of OPERATING SYSTEMS LINUX - os.ucg.ac.me SYSTEMS LINUX Božo Krstajić, PhD, ... installed on...

  • OPERATING SYSTEMSOPERATING SYSTEMSLINUXLINUX

    Boo Krstaji, PhD, University of Montenegro Podgorica

    bozok@cg.ac.yu

  • Finding FilesSometime you have needs to find some files. You could go looking through directories, but there are

    quicker ways. There are four main file search commands available in

    Slackware.

    whichThe which command is usually used to locate a

    program quickly. It just searches your PATH and returns the first instance it finds and the directory path to it.

    which name

  • Finding FilesFor example:which bash

    /bin/bash

    From that you see that bash is in the /bin directory. This is a very limited command for searching, since it only searches your PATH.

  • Finding FileswhereisThe whereis command works similar to which, but

    can also search for man pages and source files.

    For example:

    $ whereis bashbash: /bin/bash /usr/bin/bash

    /usr/man/man1/bash.1.gz

    This command not only told us where the actual program, but also where the online documentation is stored. Still, this command is limited.

  • Finding FilesWhat if you wanted to search for a specific

    configuration file? You can't use which or whereis for that.

    find

    The find command will search for anything. We want to search the entire system for the default

    sample file on the system.

    find / -name sample(find where what)

    If you run this command as a normal user, you will probably get permission denied error messages for directories that only root can see.

  • Finding Fileslocate

    The locate command searches the entire filesystemjust like the find command can do, but it searches a database instead of the actual filesystem.

    The database is set to automatically update at 4:40AM, so you have a somewhat fresh listing of files on your system. You can manually run updatedb(1) to update the locate database (before running updatedb by hand, you must first su to the nobody user). Here's an example of locate in action:

    $ locate xinitrc # we don't have to go to the root

  • Example 1: Locate file inetd.config In your HOME directory find files which name

    start with letter n List of all files from / directory (and

    subdirectories) which finished with .conf put into file conf_file in your HOME.

    Where is file passwd in our filesystem?

  • Example 2:Try to find how grep command works.

    In your filesystem find files which consist word set .

    In your HOME find files which consist phrase name

  • Searching

    grep

    The grep command searches the named input FILEs for lines containing a match to the given PATTERN.

    grep PATTERN FILEs

  • Example 3: Find number the o letters containing in your

    HOME directory files.

  • grep cr o ~/*

  • Compress and Archive FilesUnder Slackware Linux, there are several programs

    that can be used to compress and archive files.

    These programs are especially useful for making backups and sending copies of files between machines over a network connection.

    There are programs for dealing with Unix formatted archives, as well as Windows archives.

  • Compress Filesgzip

    The gzip is the GNU compression program. It takes a single file and compresses it. The basic

    usage is as follows:gzip infile

    The resulting file will be named infile.gz and will usually be smaller than the input file.

    Note that infile.gz will replace infile. This means that infile will no longer exist, even though a gzippedcopy will.

  • Compress FilesRegular text files will compress nicely, while jpeg

    images, mp3s, and other such files will not compress too well as they are already compressed.

    The maximum compression can be achieved like so:

    gzip -9 infile

    This will take a longer time to compress the file, but the result will be as small as gzip can make it.

  • Compress FilesDecompressing gzipped files can be done using two

    commands, which are really just the same program.

    The gzip will decompress any file with a recognized file extension.

    A recognized extension can be any of the following: .gz, -gz, .z, -z, .Z, or -Z.

    The first method is to call gunzip on a file, like so:

    gunzip infile.gz

    This will leave a decompressed version of infile in the current directory

  • Compress FilesThe other way to decompress a gzipped file is to call

    gzip on the file:

    gzip -d infile.gz

    This will cause exactly the same behavior as calling gunzip.

    The reason for this is simple: gunzip is simply a symbolic link to /bin/gzip.

    CAN YOU FIND THAT LINK ?

  • Example 1: In your HOME directory create file skadar. Compress the new file using gzip command. Check the file size before and after this process. What is the name of compressed file? Show content of the compressed file. Change name of the compressed file to name

    without suffix gz? Can you restore this file without changing the

    name? Restore the file (skadar).

  • Compress Filesbzip2

    bzip2 is an alternative compression program installed on Slackware Linux. It uses a different compression algorithm from gzip, which results in some advantages and some disadvantages.

    The main advantage for bzip2 is the compressed file size. bzip2 will almost always compress better than gzip. In some instances, this can result in dramatically smaller files.

    The disadvantage to bzip2 is that it is more CPU intensive than gzip. This means that bzipping a file will generally take more time than gzip.

  • Compress FilesThe usage of bzip2 is very similar to gzip:

    bzip2 infile

    The resulting output file will usually be smaller than the input file, and will be called infile.bz2.

    As with gzip, the input file will no longer exist, since bzip2 replaces the input file with a compressed copy.

  • Compress FilesThere are two commands to decompress files ending

    in a .bz2 extension, just as with gzip.You can use bzip2 or bunzip2 to decompress

    bzipped files:bzip2 -d infile.bz2

    orbunzip2 infile.bz2

    This will decompress the bzipped file and replace it with the decompressed copy.

  • Example 2:

    In your HOME directory create file skadar. Explore some switches of command bzip2 Compress the new file using bzip2 command

    such that origin file exists after compresson. Check the file size before and after this process. What is the name of compressed file? Show content of the compressed file. Remove compressed file.

  • Compress Fileszip

    Finally, there are two utilities that can be used on zip files. These are very common in the Windows

    world, so Linux has programs to deal with them. The compression program is called zip, and thedecompression program is called unzip .Compressing files is easy:

    zip zip_filename *This will create the file zip_filename.zip, which will

    contain all the files in the current directory.

  • Compress FilesYou can also recurse (-r) through the current

    directory, zipping up any directories that are also laying around:

    zip -r zip_filename ./*

    Decompressing files is easy, as well.unzip zip_filename

    This will extract all the files in the file zip_filename.zip, including any directories in the archive.

  • Example 3:

    In your HOME directory create subdirectory arh. In arh copy all files from /etc directory which

    name start with letter s Compress all copied files and arh directory into

    file archive.zip Can you compress file archive.zip with bzip2

    command? Restore compressed files and directory.

  • Archive Filestar

    tar is the GNU archiver. It takes several files or directories and creates one large file. This allows you to compress an entire directory tree, which is impossible by just using gzip or bzip2.

    The tar has many command line options, which are explained in its man page.

    The most common use for tar is to decompress and unarchive a package that you've downloaded from a web site or ftp site. Most files will come with a .tar.gz extension. This is commonly known as a tarball. It means that several files were archived using tar and then compressed using gzip.

  • Archive FilesTo create a compressed tar archive of all the files in

    the current directory (including any subdirectories and their files), you would use tar like so:

    tar -cvzf archive.tar.gz ./*

    In this command line, the -c option tells tar to create an archive, while the -z option runs the resulting archive file through gzip to compress it, -v means to be verbose (this will list all the files that are being archived) and finally, the -f option tells tar that the next string on the command line is the file to operate on.

  • Archive FilesThe most common way to decompress a tarball is

    like so:

    tar -xvzf archive.tar.gz

    The -x means to extract. This is important, as it tells tar exactly what to do with the input file.

  • Example 4:

    In your HOME directory create subdirectory arh. In arh copy all files from /dev directory which

    name star with letter h Archive and compress all copied files and arh

    directory into file archive.tar.gz Check the archived file size. Restore compressed files and directory.

  • vi editorvi is the standard Unix text editing program, and

    mastering it is essential for system administrators and users.

    There are several versions (or clones) of vi available (vi, elvis, vile, and vim). One of these is available on just about any version of Unix, as well as on Linux.

    On Slackware Linux, the defau