Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

Unix Commands

Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Unix Commands

Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

man:

Syntax: man [options] command_name


Ex: $ man password

Navigation and Directory Control Commands

Command / Syntax What it will do

cd [directory] Change directory

ls [options] [directory or files] List directory contents and file permissions

mkdir [options] drectory Make a directory

pwd Print working (current) directory

rmdir [options] directory Remove a directory


Unix vs DOS Navigation and Directory Control Commands

Command UNIX DOS


List directory contents ls dir
Make directory mkdir md &mkdir
Change directory cd cd & chdir
Delete (remove) directory rmdir rd & rmdir
Returns to user home directory cd cd\
Location in path
pwd cd
(present working directory)
pwd – print working directory

At any time you can determine where you are in the file
system hierarchy with the pwd, print working directory,
command
Ex: $ pwd
/home/username

cd – change directory
You can change to a new directory with the cd, change
directory, command. Cd will accept both absolute and
relative path names.
Ex: cd directory
cont…
cd – change directory
Examples
cd (also chdir in some shells) change directory
cd Changes to user’s home directory
cd / Changes directory to the systems’ root
cd .. Goes up one directory level
cd ../.. Goes up two directory levels

cd /full/path/name/from/root Changes dir. To absolute path named


(note the leading slash)

cd path/from/current/location Changes directory to path relative to


current location (no leading slash)

cd ~username/directory Changes directory to the named usernames’s


indicated directory
(Note: the ~ is not valid in the Bourne shell)
mkdir – make a directory
You entered your home hierarchy by making sun-directories underneath it. This is
done with the mkdir, making directory. Again, you specify either the full or path of
the directory.
Syntax: mkdir [options] directory
Ex: mkdir –p /home/frank/data
Common Options:
-p Create the intermediate (parent) directories, as needed
-m Access permissions

rmdir – remove directory


A directory needs to be empty before you can remove it. If it’s not, you need to
remove the files first. Also, you can’t remove a directory if it is your present working
directory: you must first change out of it.
Syntax: rmdir directory
Examples: To remove the empty directory /home/frank/data in /home/frank use:
# rmdir data (or)
# rmdir /home/frank/data
ls – list directory contents
The command to list your directories and files is ls. With options it can provide
information about the size, type of file, permissions, dates of file creation, change
and access.
Syntax: ls [options] [argument]
Ex: $ ls $ ls –a $ ls -al

Common Options: -a, -d, -F, -g, -L, -l


The mode field is given by the –l options and consists of 10 characters. d, -, b, c, l, s
The next 9 chars are in 3 sets of 3 chars each. They indicate the file access
permissions: the first 3 chars refer to the permissions for the user, the next three for
the users in the Unix group assigned to the file, and the last 3 to the permissions for
other users on the system. Designations are as follows…
r read permissions
w write permissions
x execute permissions
- no permissions
File Maintenance Commands
Command / Syntax What it will do
chgrp [options] group file Change the group of the file
chmod [options] Change file or directory access permissions
Change the ownership of a file; can only be done by
chown [options] owner file
the supervisor
Copy file1 into file2; file2 shouldn’t already exist.
cp [options] file1 file2
This command creates or overwrites file2
mv [options] file1 file2 Move file1 into file2
Remove (delete) a file or directory (-r recursively
rm [options] file delete the directory and its contents) (-i prompts
before removing the file}

Unix vs DOS File Maintenance Commands


Command Unix DOS
Copy file cp copy
Move file mv move
Rename file mv rename & ren
Delete (remove) file rm erase & del
Display file to screen entire file one page cat type
at a time more, less, pg type/p
cp – copy a file
Copy the contents of one file to another with the cp command
Syntax: cp [options] old-filename new-filename
Common Options:
-i interactive
-r recursively copy a directory
Ex: $ cp old-filename new-filename
You now have two copies of the file, each with identical contents. They are
completely independent of each other and you can edit and modify either as
needed. They each have their own inode, data blocks, and directory table
entries.
mv – move a file
Rename a file with the move command, mv.
Syntax: mv [options] old-filename new-filename
Common Options:
-i interactive
-f don’t prompt, even when copying over an existing target file
Examples: $ mv old-filename new-filename
You now have a file called new-filename and the file old-filename is gone.
Actually all you have done is to update the directory table entry to give the
file a new name. The contents of the file remain where they were.
chown – change ownership
Ownership of a file can be changed with chown command. On most versions
of Unix this can only be done by the super-user, i.e. a normal user can’t give
away ownership of their files. Chown is used as below, where # represents
the shell prompt for the super-user.
Syntax: chown [options] user[:group] file
Common Options:
-R recursively descend through the directory structure
-f force, and don’t report any errors
Ex: # chown new_owner file

chgrp – change group


Any one can change the group of files they own, to another group they
belong to, with the chgrp command
Syntax: chgrp [options] group file
Common Options:
-R recursively descend through the directory structure
-f force, and don’t report any errors
Ex: $ chgrp new_group file
Display Commands
Command / Syntax What it will do
cat [options] file Concatenate (list) a file
echo [test string] Echo the text string to stdout
head [-number] file Display the first 10 (or number of) lines of a file
more (or less or pg) [options] file Page through a text file
tail [options] file Display the last few lines (or parts) of a file

echo – echo a statement


The echo command is used to repeat, or echo, the argument you give it back
to the standard output device. It normally ends with a line-feed, but you can
specify an option to prevent this.
Syntax: echo [string]
Common Options:
-n don’t print <new-line> (BSD, shell built-in)
\c don’t print <new-line> (SVR4)
\0n where n is the 8 bit ASCII character code (SVR4)
\t tab (SVR4)
\f form-feed (SVR4)
\n new-line (SVR4)
\v vertical tab (SVR4)
cont…
Ex: $echo “Hello Class”

To preventthe line feed: $ echo –n Hello Class or echo “Hello Class \c”

Where the style to use in the last example depends on the echo command in use.

The \x options must be within pairs of single or double quotes, with or without other
string characters.

cat – concatenate a file


Display the contents of a file with the concatenate command cat
Syntax: cat [options] [file]
Common Options:
-n precede each line with a line number
-v display non-printing characters, except tabs, new-lines, and
form-feeds.
-e display $ at the end of each line (prior to new-line)
(when used with –v option)

Example: $ cat filename


You can list a series of files on the command line, and cat will concatenate them,
starting each in turn, immediately after completing the previous one.
Ex: $ cat file1 file2 file3
more, less and pg – page through a file
More, less and pg let you page through the contents of a file one screenful at a
time. These may not all be available on your Linux system. They allow you to back
up through the previous pages and search for words, etc.
Syntax: more [options] p+/patters] [filename]
less [options] [+/pattern] [filename]
pg [options] [+/pattern] [filename]
Options:
more less pg Action
-c -c -c clear display before displaying
-I ignore case
-w default default don’t exit at end of input, but prompt and wait
-lines -lines # of lines/screenful
+/pattern +/pattern +/pattern search for the pattern

Internal Controls:
More Display (one screen at a time) the file requested
<space bar> to view next screen
<return> or <CR> to view one more line
q, h, b to quit viewing the file, help, go back up one screenful
/word search for word in the remainder of the file
Less similar to more,
head – display the start of a file
head displays the head, or start, of the file.
Syntax: head [options] file
Common Options:
-n number number of lines to display, counting from the top of the file
-number same as abouve
Example:
By default head displays the first 10 lines. You can display more with the “-n number”, or
“-number” options, e.g., to display the first 40 lines:
$ head -40 filename or head –n 40 filename

tail – display the end of a file


tail displays the tail, or end, of the file.
Syntax: tail [options] file
Common Options:
-number number of lines to display, counting from the bottom of the file
Example:
By default head displays the first 10 lines, but you can specify different line or byte
numbers, or a different starting point within the file. To display the last 30 line of a file use
the -number style:
$ tail -30 filename
System resources commands
Command / Syntax What it will do

chsh (passwd –e/-s) username login_shell Change the user’s login shell
date [options] Report the current date and time

Report the summary of disk blocks and inodes free


df [options] [resource]
in use
du [options] [directory or file] Report amount of disk space in use
Display or set (superuser only) the name of the
hostname/uname
current machine
Send a signal to process with the process id
kill [options] [-SIGNAL] [pid#] [%job] number (pid#) or job control number (%n). The
default signal is to kill the process

man [options] command Show the manual (man) page for a command

passwd [options] Set or change your password

ps [options] Show status of active processes

Saves everything that appears on the screen to


script file
file until exit is executed

stty [options] Set or display terminal control options

Report the binary, source, and man page locations


whereis [options] command
for the command named
Reports the path to the command or the shell alias
which command
in use
Report who is logged in and what processes are
who or w
running
df – summarize disk block and file usage
df is used to report the number of disk blocks and inodes used and free for each file
system. The output format and valid options are very specific to the OS and program
version in use.
Syntax: df [options] [resource]
Common Options:
-l local file system only
-k report in kilobytes
Example: $ df

du – report disk space in use


du report the amount of disk space in use for the files or directories you specify.
Syntax: du [options] [directory or file]
Common Options:
-a display disk usage for each file, not just subdirectories
-s display a summary total only
-k report in kilobytes
Example: $ du
ps – show status of active processes
ps is used to report on processes currently running on the system. The output format and
valid options are very specific to the OS and program version in use.
Syntax: ps [options]
Common Options:
-e all processes, all users
-l long format
-u user user oriented report
-e even processes not executed from terminals
-f full listing
Example: $ ps
du – report disk space in use
kill sends a signal to a process, usually to terminate it.
Syntax: kill [-signal] process-id
Common Options:
-l display the available kill signals
Example: $ kill –l or kill -9 15599
The –KILL signal, also specified as -9(because it is 9t on the above list), is the most
commonly used kill signal. Once seen, it can’t be ignored by the program whereas the
other signals can.
who – list current users
who reports who is logged in at the present time..
Syntax: who [am i]
Example: $ who who am i

whereis – report program locations


whereis reports the filename or source, binary, and manual page files associated with
commands
Syntax: whereis [options] command(s)
Common Options:
-b report binary file only
-m report manual sections only
-s report source files only
Example: $ whereis Mail
whereis –b Mail
whereis –m Mail
which – report the command found
which will report the name of the file that is be executed when the command is invoked.
This will be the full path name or the alias that’s found first in your path.
Syntax: which command(s)
Example: $ which Mail

hostname / uname – name of machine


which will report the name of the file that is be executed when the command is invoked.
This will be the full path name or the alias that’s found first in your path.
Syntax: which command(s)
Example: $ which Mail

date – current date and time


date displays the current data and time. A superuser can set the date and time.
Syntax: date [options] [+format]
Common Options:
-u use universal time
Example: $ date date –u date +%a%t%D
touch – create a file
The touch can be used to create a new (empty) file or to update the last access date/time
on an existing file. The commands used primarily when a script requires the pre-
existences of a file or when the script is checking for last date or time a function was
performed.
Syntax: touch [options] [date_time] file
touch [options] [-t time] file
Example: $ touch filename
wc – count words in a file
The wc for “word count”, the command can be used to count the number of lines,
characters, or words in a file.
Syntax: wc [options] file
Common Options:
-c count bytes
-m count characters
-l count lines
-w count words
If no options are specified it default to “-lwc”.
Example: $ wc users
ln – link to another file
The ln command creates a “link” or an additional way to access (or gives an additional
name to) another file.
Syntax: ln [options] source [target]
Common Options:
-f force a link regardless of target permissions, don’t report errors
-s make a symbolic link
Example: $ ln –s chkit chkmag
ln chkit chkmag

You might also like