Ict-innovation/LPI/103.3

= 103.3 Perform basic file management = Candidates should be able to use basic Linux commands to manage files and directories.

Key Knowledge Areas


 * Copy, move and remove files and directories individually.
 * Copy multiple files and directories recursively.
 * Remove files and directories recursively.
 * Use simple and advanced wildcard specifications in commands.
 * Using find to locate and act on files based on type, size, or time.
 * Usage of tar, cpio and dd.

Moving Around the Filesystem
Absolute and relative paths

A directory or a file can be accessed by giving its full pathname, starting at the root (/) or its relative path, starting from the current directory.

Absolute path:independent of the user's current directory, starts with /

Relative path:depends on where the user is, doesn't start with /

As in any structured filesystem there are a number of utilities that can help you navigate through the system.

pwd:Gives your actual position as an absolute path.

cd:The 'change directory' command

ls: List the contents of a directory.

The command can take several parameters the most common of which are:

-l – use the long listing format,

-a – list all files and directories including hidden files and directories,

-h – show file sizes in human readable format, ie. Formatted for easy reading

-d – list directories only and does not list their contents.

Finding Files and Directories

We will describe the find, which, whereis and locate utilities.

find

Syntax:

find   [-exec  {} \;]

The DIRECTORY argument tells find where to start searching and CRITERIA can be a combination of serial selection criteria, including the name of a file or directory we are looking for.

Examples:

The names of matching files are listed to standard outpu. Alternatively, a specific operation can be performed on each file found. For example to delete the file, or change the permission. The find tool has the built-in option –exec which allows you to do that. For example, remove all files belonging to user 502:

Handling directories

Creating a directory

When making a directory you can set the permission mode with the -m option. Another useful option is -p which creates all subdirectories automatically as needed.

Example:

Removing directories:

To remove a directory use either rmdir or rm -r. rmdir will only remove empty directories. Specify -f to force the deletion of files on which you do not have write permission..

Notice:rm –rf /dir1/* removes all files and subdirectories leaving dir1 empty

rm –rf /dir1/ removes all files and subdirectories including dir1

Using cp and mv

cp

Syntax:

 cp [options] file1 file2

 cp [options] files directory

It is important to notice that cp file1 file2 makes a new copy of file1 and leaves file1 unchanged. You can also copy several files to a directory, using a list or wildcards. The following table lists the most used options.

Note: cp –r /mydir/* /dir2/ will copy all files and subdirectories omitting mydir

cp –r /mydir/ /dir2/ will copy all files and subdirectories including mydir

mv

Syntax:

 mv [options] oldname newname

 mv [options] source destination 

 mv [options] source directory 

The mv command can both move and rename files and directories. If oldname is a file and newname is a directory then the file oldname is moved to that directory.

If the source and destination are on the same filesystem, then the file isn't copied but the link is simply moved to the new location. Most common options are -f force overwrite and -i query interactively.

touch and dd

touch

Another way of creating or modifying a file is to use touch.

Syntax: touch {options} file(s)

If file doesn't exist it is created. You can also update the access time of a file to the current time using the -a option, -m changes the modification time and -r is used to apply the time attributes of another file.

Example:

touch file1.txt file2.txtcreates new files

touch myfile -r /etc/lilo.confmyfile gets the time attributes of lilo.conf

dd

This command copies a file with a changeable I/O block size. It can also be used to perform conversions (similar to tr). Main options are if =  (input file) of = (output file) conv =  (conversion)

The conversion switch can be: lcase ucase ascii

Example:

Notice that unlike cp the dd tool will copy portions of a device and preserve the underlying filesystem. On the other hand cp only deals with the data and will transfer it from one filesystem to another:

File Archiving and Compression
Linux has several utilities for compressing and archiving files. Some of these tools have their origins in tape archiving and backup solutions and the parameters and names reflect this.

tar

The tar (tape archive) command is used to archive directories and optionally compress the archive. Originally the tar command was used to archive to tape but can now also archive to disk, which is its most common use. An archive is created as follows:

This will create a bzip compressed archive of user1's home directory. The options provided to tar are:


 * c – create the archive,
 * v – show verbose output during archive creation,
 * j – compress the archive with bzip compression, alternatively you could stipulate z which would use gzip compression
 * f – the name of the file to created, in this case backup.tar,bz

To extract the backup.tar.bz archive you would use the following command

This would extract the archive to the current directory. The command line parameters are mostly the same as the above example except for the -x (exctract) parameter which replaces -c (create) To list the contents of an archive without extracting it you would use the -t parameter:

cpio

cpio is an older archive utility that does not support compression natively. cpio stands for copy in/out. Although cpio has been largely superseded by tar it is still used in Linux. In particular the initrd image file is a cpio archive. cpio expects a list of files to archive on standard input and so is usually used in combination with the find or ls command.

The above two commands create an archive using cpio. The v parameters tells cpio to provide verbose output during archive creation. To extract an archive you will use a command such as:

This will extract the cpio archive to the current directory. One of the tricky things to remember with cpio is its parameters. With archiving we usually talk of archiving and extracting which suggest the parameters -c for creating and -x or -e for extraction. The easiest way to remember that -o is for creating and -i is for extraction is to remember cpio stands for copy in/ copy out. You will create an archive by copying out from the filesystem and extract an archive by copying in from an archive.

gzip/gunzip

gzip is used to compress files using Lempel-Ziv coding. As with most Linux commands it can take a plethora of parameters but is most commonly used as follows:

By default gzip creates an output file with the same name as the input file but with the extension .gz added. The above command would create a compressed file with the name largefile.txt.gz. To uncompress he file you would run the command

bzip/bzip2

bzip compresses files using the Burrows-Wheeler block sorting text compression algorithm, and Huffman coding, which is considered more efficient than the Lempel-Ziv file. Its most commonly used format for compressing a file take a number from 1 – 9 as a parameter. This number is used to tell bzip2 to use the least efficient but fastest compression block size for 1 and use the most efficient but slowest compression block size for 9. If no number is specified the default value of 9 is used.

It will create the smallest file with the original file name and .bz appended. In our case the file would be largefile.txt.bz. To uncompress the file you would use

Used files, terms and utilities:* cp
 * find
 * mkdir
 * mv
 * ls
 * rm
 * rmdir
 * touch
 * tar
 * cpio
 * dd
 * file
 * gzip
 * gunzip
 * bzip2
 * file globbing

Previous Chapter | Next Chapter