Sei sulla pagina 1di 18

File Management commands.

Pwd - pwd command will print your home directory on screen, pwd means print
working directory.
/u0/ssb/sandeep - is output for the command when I use pwd in /u0/ssb/sandeep
directory.

Ls - ls command is most widely used command and it displays the contents of directory.
Options

ls will list all the files in your home directory, this command has many options.
ls -l will list all the file names, permissions, group, etc in long format.
ls -a will list all the files including hidden files that start with . .
ls -lt will list all files names based on the time of creation, newer files bring first.
ls Fx will list files and directory names will be followed by slash.
ls R will lists all the files and files in the all the directories, recursively.
ls -R | more will list all the files and files in all the directories, one page at a time.

Mkdir - mkdir sandeep will create new directory, i.e. here sandeep directory is
created.

Cd - cd sandeep will change directory from current directory to sandeep directory.


Use pwd to check your current directory and ls to see if sandeep directory is there or not.
You can then use cd sandeep to change the directory to this new directory.

Cat - cat cal.txt cat command displays the contents of a file here cal.txt on screen (or
standard out).

More - More command will display a page at a time and then wait for input which is
spacebar. For example if you have a file which is 500 lines and you want to read it all. So
you can use
more filename

Wc - wc command counts the characters, words or lines in a file depending upon the
option.

Options

wc -l filename will print total number of lines in a file.


wc -w filename will print total number of words in a file.
wc -c filename will print total number of characters in a file.

File -File command displays about the contents of a given file, whether it is a text
(Ascii) or binary file. To use it type
file filename. For example I have cal.txt which has ascii characters about calander of

current month and I have resume1.doc file which is a binary file in Microsoft Word. I
will get
file resume.doc
resume1.doc: data
file cal.txt
cal.txt:

ascii text

Cp - cp command copies a file. If I want to copy a file named oldfile in a current


directory to a file named newfile in a current directory.
cp oldfile newfile
If I want to copy oldfile to other directory for example /tmp then
cp oldfile /tmp/newfile. Useful options available with cp are -p and -r . -p options
preserves the modification time and permissions, -r recursively copy a directory and its
files, duplicating the tree structure.

Rcp - rcp command will copy files between two unix systems and works just like cp
command (-p and -i options too).
For example you are on a unix system that is called Cheetah and want to copy a file
which is in current directory to a system that is called lion in /usr/john/ directory then you
can use rcp command
rcp filename lion:/usr/john
You will also need permissions between the two machines. For more infor type man rcp
at command line.

Mv - mv command is used to move a file from one directory to another directory or to


rename a file.

Some examples:

mv oldfile newfile will rename oldfile to newfile.


mv -i oldfile newfile for confirmation prompt.
mv -f oldfile newfile will force the rename even if target file exists.
mv * /usr/bajwa/ will move all the files in current directory to /usr/bajwa
directory.

Ln - Instead of copying you can also make links to existing files using ln command.
If you want to create a link to a file called coolfile in /usr/local/bin directory then you can
enter this command.
ln mycoolfile /usr/local/bin/coolfile

Some examples:

ln -s fileone filetwo will create a symbolic link and can exist across machines.
ln -n option will not overwrite existing files.
ln -f will force the link to occur.

Rm - To delete files use rm command.


Options:

rm oldfile will delete file named oldfile.


rm -f option will remove write-protected files without prompting.
rm -r option will delete the entire directory as well as all the subdirectories, very
dangerous command.

Rmdir - rmdir command will remove directory or directories if a directory is empty.


Options:

rm -r directory_name will remove all files even if directory is not empty.


rmdir sandeep is how you use it to remove sandeep directory.
rmdir -p will remove directories and any parent directories that are empty.
rmdir -s will suppress standard error messages caused by -p.

FILE TRANSFER B\W UNIX SYSTEMS

Ftp - ftp command is used to execute ftp protocol using which files are transferred over
two systems.
Syntax is
ftp options hostname
options
-d enable debugging.
-g disable filename globbing.
-i turn off interactive prompts.
-v verbose on. show all responses from remote server.
ftp hostname by default will connect you to the system, you must have a login id to be
able to transfer the files. Two types of files can be transferred, ASCII or Binary.
bin at ftp> prompt will set the transfer to binary.
Steps:
1.
2.
3.
4.

ftp hostname
Connects to host
Login:
Password:

Changes to binary mode. Note: You must do this!


get [filename] copies file [filename] form host
mget [filespec] - Multiple files are copied from host.
put [filename] - copies file [filename] to host
mput [filenspec] - Multiple files are copied to host.
Bin -

Bye To disconnect from ftp


prompt - Turns off confirming uploads/downloads
hash - Turn on hash indicators for download/upload status

Advanced
Lcd - Changes your local directory (the directory from which you started 'ftp' from).
! - Allows to suspend ftp and use your command prompt, type 'exit' to return to 'ftp'.

Storage commands
Compress - compress command compresses a file and returns the original file
with .z extension, to uncompress this filename.Z file use uncompress filename command.
syntax for compress command is
compress options files
Options
-bn limit the number of bits in coding to n.
-c write to standard output (do not change files).
-f compress conditionally, do not prompt before overwriting files.
-v Print the resulting percentage of reduction for files.

Uncompress - uncompress file uncompresses a file and return it to its original


form.
syntax is
uncompress filename.Z this uncompresses the compressed file to its original name.
Options
-c write to standard output without changing files

Cpio - cpio command is useful to backup the file systems. It copy file archives in from
or out to tape or disk, or to another location on the local machine. Its syntax is
cpio flags [options]
It has three flags, -i, -o, -p
cpio -i [options] [patterns]
o cpio -i copy in files who names match selected patterns.
o If no pattern is used all files are copied in.
o It is used to write to a tape.
cpio -o
o Copy out a list of files whose name are given on standard output.
cpio -p
o copy files to another directory on the same system.
Options
o -a reset access times of input files.
o -A append files to an archive (must use with -o).
o -b swap bytes and half-words. Words are 4 bytes.
o -B block input or output using 5120 bytes per record.
o -c Read or write header information as Ascii character.

o
o
o
o
o
o
o
o
o

-d create directories as needed.


-l link files instead of copying.
-o file direct output to a file.
-r rename files interactively.
-R ID reassign file ownership and group information to the user's login ID.
-V print a dot for each file read or written.
-s swap bytes.
-S swap half bytes.
-v print a list of filenames.

find . -name "*.old" -print | cpio -ocvB > /dev/rst8 will backup all *.old
files to a tape in /dev/rst8
cpio -icdv "save"" < /dev/rst8 will restore all files whose name contain
"save"
find . -depth -print | cpio -padm /mydir will move a directory tree.

Examples

o
o

Dump command is useful to backup the file systems.


dump command copies all the files in filesystem that have been changed after a certain
date. It is good for incremental backups. This information about date is derived from
/var/adm/dumpdates and /etc/fstab .
syntax for HP-UX dump is
/usr/sbin/dump [option [argument ...] filesystem]
Options
0-9 This number is dump level. 0 option causes entire filesystem to be dumped.
b blocking factor taken into argument.
d density of tape default value is 1600.
f place the dump on next argument file instead of tape.
This example causes the entire file system (/mnt) to be dumped on
/dev/rmt/c0t0d0BEST and specifies that the density of the tape is 6250 BPI.
o /usr/sbin/dump 0df 6250 /dev/rmt/c0t0d0BEST /mnt
for more info type man dump at command line.

Pack - pack command compacts each file and combine them together into a filename.z
file. The original file is replaced. Pcat and unpack will restore packed files to their
original form.
Syntax is
Pack options files
Options
- Print number of times each byte is used, relative frequency and byte code.
-f Force the pack even when disk space isn't saved.
To display Packed files in a file use pcat command
pcat filename.z
To unpack a packed file use unpack command as unpack filename.z .

Tar - tar command creates an archive of files into a single file.


Tar copies and restore files to a tape or any storage media. Synopsis of tar is
tar [options] [file]

Examples:
tar cvf /dev/rmt/0 /bin /usr/bin creates an archive of /bin and /usr/bin, and store on the
tape in /dev/rmt0.
tar tvf /dev/rmt0 will list the tape's content in a /dev/rmt0 drive.
tar cvf - 'find . -print' > backup.tar will creates an archive of current directory and store it
in file backup.tar.

Functions:

c creates a new tape.


r append files to a tape.
t print the names of files if they are stored on the tape.
x extract files from tape.

Options:

b n use blocking factor of n.


l print error messages about links not found.
L follow symbolic links.
v print function letter (x for extraction or a for archive) and name of files.

Mt - mt command is used for tape and other device functions like rewinding, ejecting,
etc. It give commands to tape device rather than tape itself. Mt command is BSD
command and is seldom found in system V unix versions.
syntax is
mt [-t tapename] command [count]
mt for HP-UX accept following commands

eof write count EOF marks.


fsf Forward space count files.
fsr Forward space count records.
bsf Backward space count files.
bsr Backward space count records.
rew Rewind tape.
offl Rewind tape and go offline.
eod Seek to end of data (DDS and QIC drives only).
smk Write count setmarks (DDS drives only).
fss Forward space count setmarks (DDS drives only).
bss Backward space count setmarks (DDS drives only).
Examples
o mt -t /dev/rmt/0mnb rew will rewind the tape in this device.
o mt -t /dev/rmt/0mnb offl will eject the tape in this device.

Unix Command Summary


See the Unix tutorial for a leisurely, self-paced introduction on how to use the commands listed
below. For more documentation on a command, consult a good book, or use the man pages.
For example, for more information on grep, use the command man grep.
Contents

cat --- for creating and displaying short files


chmod --- change permissions
cd --- change directory
cp --- for copying files
date --- display date
echo --- echo argument
ftp --- connect to a remote machine to download or upload files
grep --- search file
head --- display first part of file
ls --- see what files you have
lpr --- standard print command (see also print )
more --- use to read files
mkdir --- create directory
mv --- for moving and renaming files
ncftp --- especially good for downloading files via anonymous ftp.
print --- custom print command (see also lpr )
pwd --- find out what directory you are in
rm --- remove a file
rmdir --- remove directory
rsh --- remote shell
setenv --- set an environment variable
sort --- sort file
tail --- display last part of file
tar --- create an archive, add or extract files
telnet --- log in to another machine
wc --- count characters, words, lines

cat
This is one of the most flexible Unix commands. We can use to create, view and concatenate
files. For our first example we create a three-item English-Spanish dictionary in a file called
"dict."
% cat >dict
red rojo
green verde
blue azul
<control-D>
%

TCS Internal

<control-D> stands for "hold the control key down, then tap 'd'". The symbol > tells the
computer that what is typed is to be put into the file dict. To view a file we use cat in a
different way:
% cat dict
red rojo
green verde
blue azul
%
If we wish to add text to an existing file we do this:
% cat >>dict
white blanco
black negro
<control-D>
%
Now suppose that we have another file tmp that looks like this:
% cat tmp
cat gato
dog perro
%
Then we can join dict and tmp like this:
% cat dict tmp >dict2
We could check the number of lines in the new file like this:

% wc -l dict2

The command wc counts things --- the number of characters, words, and line in a file.

chmod
This command is used to change the permissions of a file or directory. For example to make a
file essay.001 readable by everyone, we do this:
% chmod a+r essay.001
To make a file, e.g., a shell script mycommand executable, we do this
% chmod +x mycommand
Now we can run mycommand as a command.
To check the permissions of a file, use ls -l . For more information on chmod, use man
chmod.

cd
Use cd to change directory. Use pwd to see what directory you are in.
% cd english

TCS Internal

% pwd
% /u/ma/jeremy/english
% ls
novel poems
% cd novel
% pwd
% /u/ma/jeremy/english/novel
% ls
ch1 ch2 ch3 journal scrapbook
% cd ..
% pwd
% /u/ma/jeremy/english
% cd poems
% cd
% /u/ma/jeremy
cp
Use cp to copy files or directories.
% cp foo foo.2
This makes a copy of the file foo.
% cp ~/poems/jabber .
This copies the file jabber in the directory poems to the current directory. The symbol "."
stands for the current directory. The symbol "~" stands for the home directory.
date
Use this command to check the date and time.
% date
Fri Jan 6 08:52:42 MST 1995
echo
The echo command echoes its arguments. Here are some examples:
% echo this
this
% echo $EDITOR
/usr/local/bin/emacs
% echo $PRINTER
b129lab1
Things like PRINTER are so-called environment variables. This one stores the name of the
default printer --- the one that print jobs will go to unless you take some action to change
things. The dollar sign before an environment variable is needed to get the value in the
variable. Try the following to verify this:
% echo PRINTER
PRINTER
ftp
Use ftp to connect to a remote machine, then upload or download files. See also: ncftp

TCS Internal

Example 1: We'll connect to the machine fubar.net, then change director to mystuff, then
download the file homework11:
% ftp solitude
Connected to fubar.net.
220 fubar.net FTP server (Version wu-2.4(11) Mon Apr 18 17:26:33 MDT 1994) ready.
Name (solitude:carlson): jeremy
331 Password required for jeremy.
Password:
230 User jeremy logged in.
ftp> cd mystuff
250 CWD command successful.
ftp> get homework11
ftp> quit
Example 2: We'll connect to the machine fubar.net, then change director to mystuff, then
upload the file collected-letters:
% ftp solitude
Connected to fubar.net.
220 fubar.net FTP server (Version wu-2.4(11) Mon Apr 18 17:26:33 MDT 1994) ready.
Name (solitude:carlson): jeremy
331 Password required for jeremy.
Password:
230 User jeremy logged in.
ftp> cd mystuff
250 CWD command successful.
ftp> put collected-letters
ftp> quit
The ftp program sends files in ascii (text) format unless you specify binary mode:
ftp> binary
ftp> put foo
ftp> ascii
ftp> get bar
The file foo was transferred in binary mode, the file bar was transferred in ascii mode.
grep
Use this command to search for information in a file or files. For example, suppose that we
have a file dict whose contents are
red rojo
green verde
blue azul
white blanco
black negro
Then we can look up items in our file like this;
% grep red dict
red rojo
% grep blanco dict
white blanco
% grep brown dict
%

TCS Internal

Notice that no output was returned by grep brown. This is because "brown" is not in our
dictionary file.
Grep can also be combined with other commands. For example, if one had a file of phone
numbers named "ph", one entry per line, then the following command would give an
alphabetical list of all persons whose name contains the string "Fred".
% grep Fred ph | sort
Alpha, Fred: 333-6565
Beta, Freddie: 656-0099
Frederickson, Molly: 444-0981
Gamma, Fred-George: 111-7676
Zeta, Frederick: 431-0987
The symbol "|" is called "pipe." It pipes the output of the grep command into the input of the
sort command.
For more information on grep, consult
% man grep
head
Use this command to look at the head of a file. For example,
% head essay.001
displays the first 10 lines of the file essay.001 To see a specific number of lines, do this:
% head -20 essay.001
This displays the first 20 lines of the file.
ls
Use ls to see what files you have. Your files are kept in something called a directory.
% ls
foo
letter2
foobar letter3
letter1 maple-assignment1
%
Note that you have six files. There are some useful variants of the ls command:
% ls l*
letter1 letter2 letter3
%
Note what happened: all the files whose name begins with "l" are listed. The asterisk (*) is the
" wildcard" character. It matches any string.
lpr
This is the standard Unix command for printing a file. It stands for the ancient "line printer."
See

TCS Internal

% man lpr
for information on how it works. See print for information on our local intelligent print
command.
mkdir
Use this command to create a directory.
% mkdir essays
To get "into" this directory, do
% cd essays
To see what files are in essays, do this:
% ls
There shouldn't be any files there yet, since you just made it. To create files, see cat or emacs.
more
More is a command used to read text files. For example, we could do this:
% more poems
The effect of this to let you read the file "poems ". It probably will not fit in one screen, so you
need to know how to "turn pages". Here are the basic commands:

q --- quit more


spacebar --- read next page
return key --- read next line
b --- go back one page

For still more information, use the command man more.


mv
Use this command to change the name of file and directories.
% mv foo foobar
The file that was named foo is now named foobar
ncftp
Use ncftp for anonymous ftp --- that means you don't have to have a password.
% ncftp ftp.fubar.net
Connected to ftp.fubar.net
> get jokes.txt
The file jokes.txt is downloaded from the machine ftp.fubar.net.
print

TCS Internal

This is a moderately intelligent print command.


% print foo
% print notes.ps
% print manuscript.dvi
In each case print does the right thing, regardless of whether the file is a text file (like foo ), a
postcript file (like notes.ps, or a dvi file (like manuscript.dvi. In these examples the file is
printed on the default printer. To see what this is, do
% print
and read the message displayed. To print on a specific printer, do this:
% print foo jwb321
% print notes.ps jwb321
% print manuscript.dvi jwb321
To change the default printer, do this:
% setenv PRINTER jwb321
pwd
Use this command to find out what directory you are working in.
% pwd
/u/ma/jeremy
% cd homework
% pwd
/u/ma/jeremy/homework
% ls
assign-1 assign-2 assign-3
% cd
% pwd
/u/ma/jeremy
%
Jeremy began by working in his "home" directory. Then he cd 'd into his homework
subdirectory. Cd means " change directory". He used pwd to check to make sure he was in the
right place, then used ls to see if all his homework files were there. (They were). Then he
cd'd back to his home directory.
rm
Use rm to remove files from your directory.
% rm foo
remove foo? y
% rm letter*
remove letter1? y
remove letter2? y
remove letter3? n
%
The first command removed a single file. The second command was intended to remove all
files beginning with the string "letter." However, our user (Jeremy?) decided not to remove
letter3.
rmdir
Use this command to remove a directory. For example, to remove a directory called "essays",
do this:

TCS Internal

% rmdir essays
A directory must be empty before it can be removed. To empty a directory, use rm.
rsh
Use this command if you want to work on a computer different from the one you are currently
working on. One reason to do this is that the remote machine might be faster. For example,
the command
% rsh solitude
connects you to the machine solitude. This is one of our public workstations and is fairly fast.
See also: telnet
setenv
% echo $PRINTER
labprinter
% setenv PRINTER myprinter
% echo $PRINTER
myprinter
sort
Use this commmand to sort a file. For example, suppose we have a file dict with contents
red rojo
green verde
blue azul
white blanco
black negro
Then we can do this:
% sort dict
black negro
blue azul
green verde
red rojo
white blanco
Here the output of sort went to the screen. To store the output in file we do this:
% sort dict >dict.sorted
You can check the contents of the file dict.sorted using cat , more , or emacs .

tail
Use this command to look at the tail of a file. For example,
% head essay.001
displays the last 10 lines of the file essay.001 To see a specific number of lines, do this:
% head -20 essay.001
This displays the last 20 lines of the file.

TCS Internal

tar
Use create compressed archives of directories and files, and also to extract directories and
files from an archive. Example:
% tar -tvzf foo.tar.gz
displays the file names in the compressed archive foo.tar.gz while
% tar -xvzf foo.tar.gz
extracts the files.

telnet
Use this command to log in to another machine from the machine you are currently working
on. For example, to log in to the machine "solitude", do this:
% telnet solitude
See also: rsh.
wc
Use this command to count the number of characters, words, and lines in a file. Suppose, for
example, that we have a file dict with contents
red rojo
green verde
blue azul
white blanco
black negro
Then we can do this
% wc dict
5
10
56 tmp
This shows that dict has 5 lines, 10 words, and 56 characters.
The word count command has several options, as illustrated below:
% wc -l dict
5 tmp
% wc -w dict
10 tmp
% wc -c dict
56 tmp

dummy
Under construction

TCS Internal

11.1 Unix Commands

Unix Commands
Command/Syntax

What it will do

awk/nawk [options] file

scan for patterns in a file and process the results

cat [options] file

concatenate (list) a file

cd [directory]

change directory

chgrp [options] group file

change the group of the file

chmod [options] file

change file or directory access permissions

chown [options] owner file

change the ownership of a file; can only be done by the superuser

chsh (passwd -e/-s)


username login_shell

change the user's login shell (often only by the superuser)

cmp [options] file1 file2

compare two files and list where differences occur (text or binary
files)

compress [options] file

compress file and save it as file.Z

cp [options] file1 file2

copy file1 into file2; file2 shouldn't already exist. This command
creates or overwrites file2.

cut (options) [file(s)]

cut specified field(s)/character(s) from lines in file(s)

date [options]

report the current date and time

dd [if=infile] [of=outfile]
[operand=value]

copy a file, converting between ASCII and EBCDIC or swapping


byte order, as specified

diff [options] file1 file2

compare the two files and display the differences (text files only)

df [options] [resource]

report the summary of disk blocks and inodes free and in use

du [options] [directory or
file]

report amount of disk space in use

echo [text string]

echo the text string to stdout

ed or ex [options] file

Unix line editors

emacs [options] file

full-screen editor

expr arguments

evaluate the arguments. Used to do arithmetic, etc. in the shell.

file [options] file

classify the file type

find directory [options]


[actions]

find files matching a type or pattern

finger [options]
user[@hostname]

report information about users on local and remote machines

ftp [options] host

transfer file(s) using file transfer protocol

grep [options] 'search string'


argument
egrep [options] 'search
string' argument

search the argument (in this case probably a file) for all
occurrences of the search string, and list them.

fgrep [options] 'search


string' argument
gzip [options] file

TCS Internal

compress or uncompress a file. Compressed files are stored with


a .gz ending

gunzip [options] file


zcat [options] file
head [-number] file

display the first 10 (or number of) lines of a file

hostname

display or set (super-user only) the name of the current machine

kill [options] [-SIGNAL]


[pid#] [%job]

send a signal to the process with the process id number (pid#) or


job control number (%n). The default signal is to kill the process.

ln [options] source_file
target

link the source_file to the target

lpq [options]
show the status of print jobs
lpstat [options]
lpr [options] file
print to defined printer
lp [options] file
lprm [options]
remove a print job from the print queue
cancel [options]
ls [options] [directory or file] list directory contents or file permissions
mail [options] [user]
mailx [options] [user]

simple email utility available on Unix systems. Type a period as


the first character on a new line to send message out, question
mark for help.

Mail [options] [user]


man [options] command

show the manual (man) page for a command

mkdir [options] directory

make a directory

more [options] file


less [options] file

page through a text file

pg [options] file
mv [options] file1 file2

move file1 into file2

od [options] file

octal dump a binary file, in octal, ASCII, hex, decimal, or


character mode.

passwd [options]

set or change your password

paste [options] file

paste field(s) onto the lines in file

pr [options] file

filter the file and print it on the terminal

ps [options]

show status of active processes

pwd

print working (current) directory

rcp [options] hostname

remotely copy files from this machine to another machine

rlogin [options] hostname

login remotely to another machine

rm [options] file

remove (delete) a file or directory (-r recursively deletes the


directory and its contents) (-i prompts before removing files)

rmdir [options] directory

remove a directory

rsh [options] hostname

remote shell to run on another machine

TCS Internal

script file

saves everything that appears on the screen to file until exit is


executed

sed [options] file

stream editor for editing files from a script or from the command
line

sort [options] file

sort the lines of the file according to the options chosen

source file
. file

read commands from the file and execute them in the current
shell. source: C shell, .: Bourne shell.

strings [options] file

report any sequence of 4 or more printable characters ending in


<NL> or <NULL>. Usually used to search binary files for ASCII
strings.

stty [options]

set or display terminal control options

tail [options] file

display the last few lines (or parts) of a file

tar key[options] [file(s)]

tape archiver--refer to man pages for details on creating, listing,


and retrieving from archive files. Tar files can be stored on tape or
disk.

tee [options] file

copy stdout to one or more files

telnet [host [port]]

communicate with another host using telnet protocol

touch [options] [date] file

create an empty file, or update the access time of an existing file

tr [options] string1 string2

translate the characters in string1 from stdin into those in string2


in stdout

uncompress file.Z

uncompress file.Z and save it as a file

uniq [options] file

remove repeated lines in a file

uudecode [file]

decode a uuencoded file, recreating the original file

uuencode [file] new_name

encode binary file to 7-bit ASCII, useful when sending via email,
to be decoded as new_name at destination

vi [options] file

visual, full-screen editor

wc [options] [file(s)]

display word (or character or line) count for file(s)

whereis [options] command

report the binary, source, and man page locations for the
command named

which command

reports the path to the command or the shell alias in use

who or w

report who is logged in and what processes are running

zcat file.Z

concatenate (list) uncompressed file to screen, leaving file


compressed on disk

TCS Internal

Potrebbero piacerti anche