Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Ubuntu Linux Toolbox 1000+ Commands for Ubuntu and Debian Power Users phần 4 pdf
Nội dung xem thử
Mô tả chi tiết
Related commands for changing group assignments and passwords include newgrp
and gpasswd, as well as the /etc/gshadow file.
Traversing the File System
Basic commands for changing directories (cd), checking the current directory (pwd)
and listing directory contents (ls) are well known to even casual shell users. So this
section focuses on some less-common options to those commands, as well as other
lesser-known features for moving around the file system. Here are some quick examples of cd for moving around the file system:
$ cd Change to your home directory
$ cd $HOME Change to your home directory
$ cd ~ Change to your home directory
$ cd ~francois Change to francois’ home directory
$ cd - Change to previous working directory
$ cd $OLDPWD Change to previous working directory
$ cd ~/public_html Change to public_html in your home directory
$ cd .. Change to parent of current directory
$ cd /usr/bin Change to usr/bin from root directory
$ cd usr/bin Change to usr/bin beneath current directory
If you want to find out what your current directory is, use pwd (print working directory):
$ pwd
/home/francois
Creating symbolic links is a way to access a file from other parts of the file system (see
the section “Using Symbolic and Hard Links” earlier in the chapter for more information on symbolic and hard links). However, symbolic links can cause some confusion
about how parent directories are viewed. The following commands create a symbolic link
to the /tmp directory from your home directory and show how to tell where you are
related to a linked directory:
$ cd $HOME
$ ln -s /tmp tmp-link
$ ls -l tmp-link
lrwxrwxrwx 1 francois francois 13 Mar 24 12:41 tmp-link -> /tmp
$ cd tmp-link/
$ pwd
/home/francois/tmp-link
$ pwd -P
/tmp
$ pwd -L
/home/francois/tmp-link
$ cd -L ..
$ pwd
/home/francois
$ cd tmp-link
77
Chapter 4: Working with Files
82935c04.qxd:Toolbox 10/29/07 12:59 PM Page 77
$ cd -P ..
$ pwd
/
Using the -P and -L options to pwd and cd, you can work with symbolically linked directories
in their permanent or link locations, respectively. For example, cd -L .. takes you up one
level to your home directory, whereas cd -P .. takes you up one level above the
permanent directory (/). Likewise, -P and -L options to pwd show permanent and
link locations.
Bash can remember a list of working directories. Such a list can be useful if you want
to return to previously visited directories. That list is organized in the form of a stack.
Use pushd and popd to add and remove directories:
$ pwd
/home/francois
$ pushd /usr/share/man/
/usr/share/man ~
$ pushd /var/log/
/var/log /usr/share/man ~
$ dirs
/var/log /usr/share/man ~
$ dirs -v
0 /var/log
1 /usr/share/man
2 ~
$ popd
/usr/share/man ~
$ pwd
/usr/share/man
$ popd
~
$ pwd
/home/francois
The dirs, pushd, and popd commands can also be used to manipulate the order of
directories on the stack. For example, pushd -0 pushes the last directory on the stack
to the top of the stack (making it the current directory). The pushd -2 command pushes
the third directory from the bottom of the stack to the top.
Copying Files
Provided you have write permission to the target directory, copying files and directories can be done with some fairly simple commands. The standard cp command will
copy a file to a new name or the same name in a new directory, with a new time stamp associated
with the new file. Other options to cp let you retain date/time stamps, copy recursively,
and prompt before overwriting. Here are some examples:
$ cd ; touch index.html
$ mkdir /tmp/html
78
Chapter 4: Working with Files
82935c04.qxd:Toolbox 10/29/07 12:59 PM Page 78
$ cp -i index.html /tmp/html/
$ cp -il index.html /tmp //html
$ mkdir /tmp/back
$ cp -a /tmp /html /mp/back/
$ cp -R /tmp /html /tmp/back/
The above examples show ways of copying files related. In the first cp example above,
if an index.html file exists in /tmp/html, you are prompted before overwriting it
with the new file. In the next example, the index.html file is hard-linked to a file of
the same name in the /tmp/html directory. In that case, because both hard links point
to the same file, editing the file from either location will change the contents of the file
in both locations. (The link can only be done if /tmp/html and your home directory
are in the same file system.)
The cp -a command copies all files below the /tmp/html directory, retaining all
ownership and permission settings. If, for example, /tmp/back represented a USB
flash drive, that command would be a way to copy the contents of your web server
to that drive. The -R option also recursively copies a directory structure, but assigns
ownership to the current user and adds current date/time stamps.
The dd command is another way to copy data. This command is very powerful because
on Linux systems, everything is a file, including hardware peripherals. Here is an
example:
$ dd if=/dev/zero of=/tmp/mynullfile count=1
1+0 records in
1+0 records out
512 bytes (512 B) copied, 0.000308544 s, 1.7 MB/s
/dev/zero is a special file that generates null characters. In the example just shown,
the dd command takes /dev/zero as input file and outputs to /tmp/mynullfile.
The count is the number of blocks. By default, a block is 512 bytes. The result is a
512-bytes-long file full of null characters. You could use less or vi to view the contents of the file. However, a better tool to view the file would be the od (Octal Dump)
command:
$ od -vt x1 /tmp/mynullfile View an octal dump of a file
Here’s another example of the dd command:
$ dd if=/dev/zero of=/tmp/mynullfile count=10 bs=2
10+0 records in
10+0 records out
20 bytes (20 B) copied, 0.000595714 s, 33.6 kB/s
This time, we set the block size to 2 bytes and copied 10 blocks (20 bytes). The following command line clones the first partition of the primary master IDE drive to the second partition
of the primary slave IDE drive (back up all data before trying anything like this):
$ sudo dd if=/dev/hda1 of=/dev/hdb2
79
Chapter 4: Working with Files
82935c04.qxd:Toolbox 10/29/07 12:59 PM Page 79