0% found this document useful (0 votes)
16 views6 pages

Ex 1 Basic Unix Commands

This document provides a comprehensive overview of essential UNIX commands, including their usage and descriptions. It also explains file and directory permissions, the vi text editor commands, file extensions, output redirection, and managing background processes. Additionally, it includes examples and syntax for various commands to aid users in navigating and utilizing UNIX effectively.

Uploaded by

Pintu Jack
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views6 pages

Ex 1 Basic Unix Commands

This document provides a comprehensive overview of essential UNIX commands, including their usage and descriptions. It also explains file and directory permissions, the vi text editor commands, file extensions, output redirection, and managing background processes. Additionally, it includes examples and syntax for various commands to aid users in navigating and utilizing UNIX effectively.

Uploaded by

Pintu Jack
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Unix Tutorial

The following is a quick overview of essential UNIX commands.

Example
Command Description
Usage

ls ls List directory contents


ls –l Long listing
ls –ld List permissions on directory
directory

pwd pwd Display current directory

cp cp source Copy file source to target


target
Copy directory, recursive copy
cp –r source
target

mv mv source Move file or directory from source to target (the source is removed after the
target move)

rm rm filename Remove a file


rm –f Remove a file in which you have directory write privileges but not file
filename write privileges
rm –rf Remove non-empty directory
directory

mkdir mkdir Create a directory


directory

rmdir rmdir Remove empty directory


directory

ps ps Display process statuses


ps –ef Display all processes
ps –u userid Display all process for userid

kill kill PID Terminate process with process ID (PID). Sends UNIX process the TERM
signal. This signal should terminate most UNIX processes.
kill -9 PID
Terminate process with PID. Sends UNIX process the KILL signal. This
should only be used when normal attempts to terminate the process are
ineffective.
grep grep pattern Display all occurrences of pattern in filename
filename
Display all filename(s) containing pattern
grep –l
pattern
filename(s)

cat cat filename Display contents of filename


cat file file2 Display contents of file1 and file2
cat file1 Create file3 from contents of file1 and file2
file2>file3

more more Display file contents, one page at a time


filename

man man Display UNIX man(ual) page for given command


command

tar tar tvf Display contents of tarfile


tarfile.tar
Extract contents of tarfile
tar xvf
Create tarfile named tarfile.tar with all files and directories in the current
tarfile.tar
directory
tar cvf
tarfile.tar*

chmod chmod 755 Change file or directory permissions


directory

gunzip gunzip Uncompress .gz file (generally used with tarfiles). If it is not in your path,
file.gz check /vol.nsm/tools/bin.

vi vi file Edit file

find find / -name Locate file starting in root (/) directory


file

exit exit Logout of your UNIX session

id id Display user name, user ID, and primary group ID

groups groups Display all UNIX groups to which you belong


-- or -- -- or --
id -a id –a

hostname hostname Display the hostname of the UNIX machine


-- or -- -- or --
uname -n uname -n

passwd passwd Change your UNIX password

env env Display your environment variables

nohup nohup Execute command in the background, and do not terminate the process if
command & you logout of your UNIX session.

command command & Execute the command in the background. Output will be sent to a file
& named nohup.out rather than your display.

jobs jobs List background processes

Ctrl-Z and Suspend current foreground process, put process in background for
bg execution

gzip gzip Compress file (generally used with tarfiles)


filename

File and Directory Permissions Demystified


When doing a long listing (ls –l) of a directory, we see the following entry:
drwxr-xr-x 2 hutchib nsm 512 Apr 20 11:09 bin
This is a directory named bin that is owned by user hutchib and group nsm. Let's explore the first 10
characters in detail.
• The first character is a d, which indicates that we are looking at a directory. Other possibilities for the
first character include an - for an ordinary file and an l for a symbolic link (like a shortcut in
Windows).
• Characters 2 through 4 correspond to user permissions, or permissions corresponding to the owner of
the directory. They are rwx, which indicates that the directory is readable, writable, and executable by
the user (the owner of the directory, hutchib).
• Characters 5 through 7 correspond to group permissions. In this example, these permissions apply to
the group nsm. They are r-x, indicating that the directory is readable and executable by any user
belonging to group nsm.
• Characters 8 through 10 correspond to other permissions. If you are not the owner of the directory,
nor a member of the group, the other permissions are applicable. They are r-x, meaning that the
directory is readable and executable by everyone else.
What do read, write, and execute permissions mean on a directory?
Read = You are able to list contents of the directory and display contents of files.
Write = You are able to create, modify, or delete files in the directory.
Execute = You are able to change into the directory. (Even if you had read and write privileges
on a directory, if you do not have execute privileges on the directory, you would not be able to get into
(cd) the directory.)
Note: If you have write privileges to a directory, you may delete any file in that directory, even if it is not
writable, and is owned by somebody else.
What do read, write, and execute permissions mean on a file?
Read = You are able to list the contents of a file.
Write = You are able to modify or delete the file. (If you have permission to write to a file, you
may modify it, but may not delete it, if the directory is not writable.)
Execute = You are able to execute the file. (This is needed if the file is a shell script or Executable.)
How do I change file/directory permissions?
You may change file and directory permissions with the chmod command.
The syntax is:
chmod octal-mode file_or_directory
You may recursively change permissions with the chmod –R command.
When looking at the following table, remember these values when setting file or directory permissions:
Read = 4
Write = 2
Execute = 1

Permissions Octal-mode Command

-rwxrwxrwx 777 chmod 777 file_or_directory


User: rwx = 4 + 2 + 1 = 7
Group: rwx = 4 + 2 + 1 = 7
Other: rwx = 4 + 2 + 1 = 7

-rwxr-xr-x 755 chmod 755 file_or_directory


User: rwx = 4 + 2 + 1 = 7
Group: r-x = 4 + 1 = 5
Other: r-x = 4 + 1 = 5

-rw-r--r-- 644 chmod 644 file_or_directory


User: rw- = 4 + 2 = 6
Group: r-- = 4
Other: r-- = 4

Useful 'vi' Commands for Vim Text Editor


The vi editor has two modes: Input and Command. In Input mode, any characters you type will be entered
into the text file. In Command mode, characters you type are used to move around the screen and modify
the file. If you are unsure which mode you are in, press Esc, and you will be put into Command mode.
Command Description

h Move cursor one position left

j Move cursor one position down

k Move cursor one position up

l Move cursor one position right

:1 Go to beginning of file

G Go to bottom of file

0 Go to beginning of line

$ Go to end of line

i Insert text before cursor

a Append text after cursor

A Append text at end of line

Esc Enter command mode

:w Save the file, continue working

:wq Save the file and exit

:q! Exit without saving

:%s/search/replace/g Replace all occurrences of search with replace within the file

File Extension Primer

Extension Description

.tar.gz File was archived with tar and compressed with the gzip command. Use gunzip to
uncompress it, then tar xvf filename.tar to extract it. (If gunzip is not in your path, you may
find it in /vol.nsm/tools/bin)

.gz File was compressed with the gzip command. Use gunzip to uncompress it.

.Z File was compressed with the compress command. Use uncompress to uncompress it.

.zip File was compressed with the zip command. Use unzip to uncompress it.
You can use the UNIX command file to attempt to determine the file type.
Example:
$ file /home/hutchib/long_cmd.sh
/home/hutchib/long_cmd.sh: executable shell script
$ file /usr/bin/ls
/usr/bin/ls: ELF 32-bit MSB executable SPARC Version 1, dynamically linked, stripped
How do I redirect command output?
Generally, output of your UNIX commands is sent to standard output (STDOUT). You may redirect
command output to a file by using:
command > file
If file does not exist, it will be created. If file already exists, it will be overwritten. You could instead
append to the end of file by using:
command >> file
In addition to standard output, UNIX has a mechanism called standard error (STDERR). It is sometimes
useful to discard standard error when performing commands. For example, when executing
a findcommand on a UNIX system, you normally don't want to see permission denied messages
when findattempts to read directories you don't have access to. The following command would redirect
standard error to /dev/null, effectively discarding the output:
command 2>/dev/null
If you absolutely do not want the command to generate any output (e.g. a crontab entry), use the
following command to send both standard output and standard error to /dev/null:
command >/dev/null 2>&1
How do I put a process in the background?
$ long_cmd.sh
Z[1] + Stopped (SIGTSTP) long_cmd.sh
$ bg %1
[1] long_cmd.sh&
$ jobs
[1] + Running long_cmd.sh
$ fg %1
long_cmd.sh
Execute a shell script called long_cmd.sh, which will take several minutes to complete. Since no
interaction with the terminal can occur while this script is executing, press Ctrl-Z to suspend the script.
After suspending the script, put it in the background with the bg %1 command. List the background
processes with the jobs command. The background process will continue to execute and will complete;
the shell will notify you when the job has completed. For this example, you can put the job back in the
foreground with the fg %1 command.

Ref: http://math.mit.edu/services/help/new/unix.php

You might also like