Professional Documents
Culture Documents
Chown:
The chown command (abbreviation for change owner) is used on Unix-like systems to change
the owner of a file. In most implementations, it can only be executed by the superuser.
Unprivileged (regular) users who wish to change the group of a file that they own may use
chgrp.
These examples illustrate typical syntax and use. Because chown modifies permissions, it usually
requires root privilege to run.
Change the owner of strace.log to 'rob' and the group identifier to 'developers'.
Change the owner of /tmp and /var/tmp to ‘nobody’ (not a good idea)
Change the group of /tmp and /var/tmp to ‘nogroup’
Change the group identifier of /home to 512 (regardless of whether a group name is
associated with the identifier 512 or not).
# chown -R us base
Change the ownership of base to the user us and make it recursive (-R)
Chgrp:
The chgrp (from change group) command is used by unprivileged users on Unix-like systems to change
the group associated with a computer file. Unlike the chown command, chgrp allows regular users to
change groups, but only to one of which they are a member
The group parameter indicates the new group with which the targets should be
associated. It may either be a symbolic name or an identifier.
The target1 parameter indicates the files or directories for which the change should be
made.
The target2 parameter indicates optional additional files or directories for which the
change should be made.
cksum is a command in Unix-like operating systems that generates a checksum value for a file
or stream of data. The cksum command reads the file or files specified as arguments, or standard
input if no arguments are provided, and calculates a checksum value, cyclic redundancy check
(CRC) and the byte count. The checksum, number of bytes, and file name are written to standard
output.
The cksum command can be used to verify the equality of two files on different computers, such
as a copy made over noisy transmission lines.[1] (If the files are on the same computer, equality
can be verified with the cmp command.) The comparison made by the cksum command is not
cryptographically secure. However, it is unlikely that an accidentally damaged file will produce
the same checksum as the original file.
Syntax
cksum [ File ... ]
"4038471504" represents the checksum value. "75" represents the file size of test.txt.
Cmp:
cmp is a command line utility for computer systems that use Unix or a Unix-like operating
system. It compares two files of any type and writes the results to the standard output. By
default, cmp is silent if the files are the same; if they differ, the byte and line number at which
the first difference occurred is reported.
cmp may be qualified by the use of command-line switches. The switches supported by the GNU
version of cmp are:
-b, --print-bytes
Print differing bytes.
-i SKIP, --ignore-initial=SKIP
Skip the first SKIP bytes of input.
-i SKIP1:SKIP2, --ignore-initial=SKIP1:SKIP2
Skip the first SKIP1 bytes of FILE1 and the first SKIP2 bytes of FILE2.
-l, --verbose
Output byte numbers and values of all differing bytes.
-n LIMIT, --bytes=LIMIT
Compare at most LIMIT bytes.
-s, --quiet, --silent
Output nothing; yield exit status only.
-v, --version
Output version info.
--help
Outputs a help file.
Usage
To copy a file to another file
cp [-f] [-H] [-i] [-p] [-r | -R] [--] SourceFile ... TargetDirectory
[edit] Flags
-f (force) – specifies removal of the target file if it cannot be opened for write operations. The
removal precedes any copying performed by the cp command.
-H – makes the cp command follow symbolic links, that is, to copy files to which symbolic links
point. For example:
cp -H s_4_1.fastq ../../run_110419a/lane4
where ls -l
-i (interactive) – prompts you with the name of a file to be overwritten. This occurs if the
TargetDirectory or TargetFile parameter contains a file with the same name as a file specified in
the SourceFile or SourceDirectory parameter. If you enter y or the locale's equivalent of y, the cp
command continues. Any other answer prevents the cp command from overwriting the file.
The time of the last data modification and the time of the last access.
The user ID and group ID (only if it has permissions to do this)
The file permission bits and the SUID and SGID bits.
[edit] Examples
To make a copy of a file in the current directory, enter:
cp prog.c prog.bak
This copies prog.c to prog.bak. If the prog.bak file does not already exist, the cp command
creates it. If it does exist, the cp command replaces its contents with the contents of the prog.c
file.
cp jones /home/nick/clients
To copy a file to a new file and preserve the modification date, time, and access control list
associated with the source file, enter:
cp -p smith smith.jr
This copies the smith file to the smith.jr file. Instead of creating the file with the current date and
time stamp, the system gives the smith.jr file the same date and time as the smith file. The
smith.jr file also inherits the smith file's access control protection.
cp /home/janet/clients/* /home/nick/customers
This copies only the files in the clients directory to the customers directory.
To copy a directory, including all its files and subdirectories, to another directory, enter:
cp -R /home/nick/clients /home/nick/customers
This copies the clients directory, including all its files, subdirectories, and the files in those
subdirectories, to the customers/clients directory. Be careful about including a trailing slash in
the source directory, however. If you run cp -R /home/nick/clients/
/home/nick/customers on a GNU-based system, it does the same thing as without the slash;
however, if you run the same thing on a BSD-based system, it will copy all the contents of the
"clients" directory over, instead of the "clients" directory itself.
This copies the jones, lewis, and smith files in your current working directory to the
/home/nick/clients directory.
cp programs/*.c .
This copies the files in the programs directory that end with .c to the current directory, signified
by the single . (dot). You must type a space between the c and the final dot.
Copying a file to an existing file is done by opening the existing file in update mode which
requires write access and results in the target file retaining the permissions it had originally.
Dd:
Note that an attempt to copy the entire disk image using cp may omit the final block if it is an
unexpected length[citation needed]; dd will always complete the copy if possible.
Using dd to wipe an entire disk with random data:
dd if=/dev/urandom of=/dev/hda
where ad0 is the source hard disk and ad1 is the destination hard disk in FreeBSD, where the
hard disk devices are named ad<no>. The target and destination disks should have the same size.
The noerror conversion option means to keep going if there is an error (though a better tool for
this would be ddrescue).
Duplicate a disk partition as a disk image file on a remote machine over a secure ssh connection:
The notrunc conversion option means do not truncate the output file — that is, if the output file
already exists, just replace the specified bytes and leave the rest of the output file alone. Without
this option, dd would create an output file 512 bytes long.
dd if=/dev/zero of=/dev/sda
The output of this command will resemble the following if the drive is blank:
00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
|................|
*
201f78000
16841664+0 records in
16841664+0 records out
8622931968 bytes (8.6 GB) copied, 1247.05 s, 6.9 MB/s
If the drive is blank, one line of blank bytes will be printed, followed by a '*' signifying repeated
blank lines, followed by a line indicating the address of the line which ends the repetition,
followed by the statistics which are printed after the output. The numbers in the statistics above
are illustrative. If the drive is not entirely blank, there will be more than one line of data output.
To create an image of the entire master boot record (including the partition table):
To create an image of only the boot code of the master boot record (without the partition table):
To make drive benchmark test and analyze read and write performance:
On source machine:
dd if=/dev/hda bs=16065b | netcat < targethost-IP > 1234
On target machine:
netcat -l -p 1234 | dd of=/dev/hdc bs=16065b
Sending a SIGINFO signal (or a USR1 signal on Linux) to a running `dd' process makes it print
I/O statistics to standard error and then resume copying:
Create a 1 GiB sparse file or resize an existing file to 1 GiB without overwriting:
Some implementations understand x as a multiplication operator in the block size and count
parameters:
where the "b" suffix indicates that the units are 512-byte blocks. Unix block devices use this as
their allocation unit by default.
w means 2
b means 512
k means 1024
M specifies multiplication by 1024*1024
G specifies multiplication by 1024*1024*1024
Hence bs=2*80*18b means, 2*80*18*512=1474560 which is the exact size of 1440 KiB floppy
disk
Each of the "Records in" and "Records out" lines shows the number of complete blocks
transferred + the number of partial blocks, e.g. because the physical medium ended before a
complete block was read.
Antonio Diaz Diaz (the developer of GNU ddrescue) compares[9] the variants of dd for the task of
rescuing:
The standard utility dd does a linear read of the drive, so it can take a long time or even fry the
drive without rescuing anything if the errors are at the beginning of the drive. Kurt Garloff's
dd_rescue does basically the same thing as dd, only more efficiently. LAB Valentin's dd_rhelp
is a complex shell script that runs Garloff's dd_rescue many times, trying to be strategic about
copying the drive, but it is very inefficient.
dd_rhelp first extracts all the readable data, and saves it to a file, inserting zeros where
bytes cannot be read. Then it tries to re-read the invalid data and update this file.
GNU ddrescue can be used to copy data directly to a new disk if needed, just like Linux
dd.
dd_rhelp or GNU ddrescue will yield a complete disk image, faster but possibly with some
errors. GNU ddrescue is generally much faster, as it is written entirely in C++, whereas dd_rhelp
is a shell script acting as a frontend to dd_rescue. Both dd_rhelp and GNU ddrescue aim to copy
data fast where there are no errors, then copy in smaller blocks and with retries where there are
errors. GNU ddrescue is easy to use with default options, and can easily be downloaded and
compiled on Linux-based Live CDs such as Knoppix, and can be used with SystemRescueCD.
Savehd7 saves the harddisk partition in several phases [11]. Therefore it is not necessary to call
savehd7 with different parameters. Savehd7 works interactively in text mode and shows the
progress while it works. E.g.:
Processing - Press any key to pause (may take some time to react)
progress: okay: bad blks: fixed: bad bytes:
copy 2.6568% 2.6568% 0.0000% 0.0000% 0
There are large differences in how disk errors are processed by various kernels. Operating
systems such as FreeBSD, NetBSD, OpenBSD, Solaris, as well as different Linux kernels (i.e.
hda vs. sda (<2.6.20)) behave differently. Also, Linux lacks "raw" disk devices like *BSD has,
which makes it less desirable for low-level data recovery. Non-raw devices read larger blocks
than requested, obscuring the actual location where the error occurred. You may wish to use
"dmesg |tail -n8" to see the error messages on the console.
Du:
du (abbreviated from disk usage) is a standard Unix program used to estimate the file space
usage—space used under a particular directory or files on a file system.
Usage
du takes a single argument, specifying a pathname for du to work; if it is not specified, the
current directory is used. The SUS mandates for du the following options:
-a,display an entry for each file (and not directory) contained in the current directory
-H,calculate disk usage for link references specified on the command line
-k,show sizes as multiples of 1024 bytes, not 512-byte
-L,calculate disk usage for link references anywhere
-s,report only the sum of the usage in the current directory, not for each file
-x,only traverse files and directories on the device on which the pathname argument is
specified.
Other Unix and Unix-like operating systems may add extra options. For example, BSD and GNU
du specify a -h option, displaying disk usage in a format easier to read by the user, adding units
with the appropriate SI prefix (e.g. 10 MB).
[edit] Examples
Sum of directories in kilobytes:
$ du -sk *
152304 directoryOne
1856548 directoryTwo
Sum of directories in human-readable format (Byte, Kilobyte, Megabyte, Gigabyte, Terabyte and
Petabyte):
$ du -sh *
149M directoryOne
1.8G directoryTwo
disk usage of all subdirectories and files including hidden files within the current directory
(sorted by filesize) :
$ du -sk .[!.]* *| sort -n
disk usage of all subdirectories and files including hidden files within the current directory
(sorted by reverse filesize) :
$ du -d 1 -c -h
Df:
df (abbreviation for disk free) is a standard Unix computer program used to display the amount
of available disk space for filesystems on which the invoking user has appropriate read access.
df is usually implemented by reading the mtab file or using statfs.
Usage
The Single UNIX Specification specifications for df are:
Example
$ df -k
Filesystem 1024-blocks Free %Used Iused %Iused Mounted on
/dev/hd4 32768 16016 52% 2271 14% /
/dev/hd2 4587520 1889420 59% 37791 4% /usr
/dev/hd9var 65536 12032 82% 518 4% /var
/dev/hd3 819200 637832 23% 1829 1% /tmp
/dev/hd1 524288 395848 25% 421 1% /home
/proc - - - - - /proc
/dev/hd10opt 65536 26004 61% 654 4% /opt
File: file is a standard Unix program for recognizing the type of data contained in a computer
file.
Usage
The SUS mandates the following options:
Other Unix and Unix-like operating systems may add extra options than these.
[edit] Examples
# file file.c
file.c: C program text
# file program
program: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically
linked
(uses shared libs), stripped
# file /dev/wd0a
/dev/wd0a: block special (0/0)
# file -s /dev/hda1
/dev/hda1: Linux/i386 ext2 filesystem
# file -s /dev/hda5
/dev/hda5: Linux/i386 swap file
# file compressed.gz
compressed.gz: gzip compressed data, deflated, original filename,
`compressed', last
modified: Thu Jan 26 14:08:23 2006, os: Unix
# file data.ppm
data.ppm: Netpbm PPM "rawbits" image data
fsck: The system utility fsck (for "file system check") is a tool for checking the consistency of a
file system in Unix and Unix-like operating systems such as GNU/Linux.
Example
The following example checks the file system on the first partition of the second hard disk on a
Linux system:
fsck /dev/sdb1
fuser: fuser is a UNIX command used to show which processes are using a specified file, file
system, or socket. For example:
# fuser -m -u /mnt/usb1
/mnt/usb1: 1347c(root) 1348c(guido) 1349c(guido)
fuser displays the PIDs of processes using the specified files or file systems. In the default
display mode, each file name is followed by a letter denoting the type of access:
c
current directory.
e
executable being run.
f
open file.
F
open file for writing.
r
root directory.
m
mmap'ed file or shared library
fuser returns a non-zero code if none of the files are accessed or in case of a fatal error. If at least
one access has succeeded, fuser returns zero. The output of "fuser" may be useful in diagnosing
"resource busy" messages arising when attempting to unmount filesystems.
[edit] Options
-k
kills all process accessing a file. For example fuser -k /path/to/your/filename kills
all processes accessing this directory without confirmation. Use -i for confirmation
-i
interactive mode. Prompt before killing process
-v
verbose.
-u
append username
-a
display all files
-m
name specifies a file on a mounted file system or a block device that is mounted. All
processes accessing files on that file system are listed. If a directory file is specified, it is
automatically changed to name/. to use any file system that might be mounted on that
directory.
Also note that -k sends a SIGKILL to all process. Use the -signal to send a different signal. For a
list of signals supported by the fuser run 'fuser -l'
Usage
The SUS mandates for ln two options: -f will force removal of existing files to allow the link to
be created; and -s will create symbolic links. Therefore, ln with no options creates a hard link,
ln -f forces a hard link, ln -s creates a symbolic link, and ln -fs forces a symbolic link. In
order to link to a folder (vs. a file), use the -n option so that the symbolic link is not
dereferenced: ln -sfn source/folder linked/folder/name.
Other Unix and Unix-like operating systems may add extra options. GNU ln adds options such
as -b to back up files before creating links, -v to print out the name of each file before links are
created, and others. BSD ln adds -h, preventing ln from descending into targets whose symlinks
point to directories
ln file_name link_name
would have the effect of creating a hard link called link_name that points to the same data as the
existing file file_name.
$ ln -s data.txt slink.txt
$ ls -li
The symbolic (soft) link is stored in a different inode than the text file (969817). The information
stored in data.txt is accessible through the slink.txt:
$ file slink.txt
$ cat slink.txt
some data
If we delete the text file data.txt, slink.txt becomes a broken link and our data is lost.
$ rm data.txt
$ ls -li
$ file slink.txt
$ cat slink.txt
If hlink.txt was a hard link, our data would still be accessible through hlink.txt. Also, if you
delete the original file, the hard-linked copy would still be there:
$ ln data.txt hlink.txt
$ ls -li
$ rm data.txt
$ ls -li
$ cat hlink.txt
some data
Sample usage
The following example demonstrates the output of the ls command given two different
arguments:
$ pwd
/home/fred
$ ls -l
rwrx--rx-x 1 fred editors 3011 design
drwxr--r-- 1 fred editors 4096 drafts
-rw-r--r-- 1 fred editors 30405 edition-32
-r-xr-xr-x 1 fred fred 8460 edit
$ ls -F
drafts/
edition-32
edit*
In this example, the user fred has a directory named drafts, a regular file called edition-32,
and an executable named edit in his home directory. ls uses unix file permission notation to
indicate which users or groups are allowed to access each file or directory.
This means, in short, that the first column lists permissions for: user (u), owning group (g), and
others (o)
http://en.wikipedia.org/wiki/
Cp: cp is a UNIX command used to copy a file. Files can be copied either to the same directory or to a
completely different directory, possibly on a different file system or hard disk drive. If the file is copied
to the same directory, the new file must have a different name to the original; in all other cases, the new
file may have the same or a different name. The original file remains unchanged