UNIX Basic Commands
UNIX Basic Commands
Introduction and
Basic commands
Kinjal Rathod
...
$ ls -a
1.5 Pathnames
Prints path of the current
directory (working directory).
$ pwd
Chapter 2
cp, mv, rm, rmdir, cat, less, head,
tail, grep, wc
$ cp ./myfile /home/artem
...
$ ln -s /usr/local/ff/firefox /usr/bin/firefox
This command will make a symbolic link /usr/bin/firefox to the
file /usr/local/ff/firefox
Symbolic links have l symbol in the beginning of 'ls -l' output
string.
$ ln /usr/local/ff/firefox /usr/bin/firefox
This will make a hard link. The difference between a symbolic
link and a hard link is that a hard link to a file is
indistinguishable from the original directory entry; just consider
it as a file alias. Hard links may not normally refer to directories.
...
This experiment proves that a
hard link is just another name
for a file. Even after deleting
original file it still exists
because we haven't deleted
the hard link. Simply there is
really no such thing as "hard
link", we just create another
name for a file.
$ ln myfile hlink
$ mv ./myfile /home/artem
$ cat myfile
...
This will print each line of
myfile containing the word
Science (it is case-sensitive).
...
Some of the other options of grep are:
-v display those lines that do NOT match
-n precede each matching line with the line number
-c print only the total count of matched lines
...
Will return the number of
words in myfile.
$ wc -w myfile
Chapter 3
>, >>, <, |, sort, who
3.1 Redirection
Type cat without specifing a
file to read. Then type a few
words on the keyboard and
press the [Return] key.
Finally hold the [Ctrl] key
down and press [d] (written
as ^D for short) to end the
input.
$ cat
It reads the standard input
(the keyboard), and on
...
Type something, then press
[Ctrl-d] to end the input.
...
Type something, then press
[Ctrl-d] to end the input.
...
This will join (concatenate)
myfile1 and myfile2 into a
new file called file3.
...
Input redirection is <
3.4 Pipes
who command returns the
list of all users currently
logged in the system. This is
a method to get a sorted list
of names by using a
temporary file usernames.
...
And this is the way to find out
how many users are logged
in. We are using a pipe
between who and wc
commands.
$ who | wc -l
Chapter 4
*, ?, man, whatis, apropos
4.1 Wildcards
The character * is called a
wildcard, and will match
against none or more
character(s) in a file (or
directory) name.
...
The character ? will match
exactly one character.
...
If you are not sure about the
exact name of the command,
this will give you the list of
commands with keyword in
their manual page header.
Chapter 5
ls -lag, chmod, command &, bg,
jobs, fg, kill, ps
Owner of the
file
File
permissions
Size
Modification
date
Number of
Group of
subdirectories (1
the owner
for a file)
$ ls -l
The symbol in the beginning of the string indicates whether this is a file, directory or a
link:
The'd'
leftindicates
group ofa3directory,
gives the'-'
fileindicates a file, 'l' indicates a symbolic link.
Filename
...
Access rights on files
r indicates read permission (or otherwise), that is, the presence or absence of
permission to read and copy the file
w indicates write permission (or otherwise), that is, the permission (or otherwise) to
change a file
x indicates execution permission (or otherwise), that is, the permission to execute a file,
where appropriate
So, in order to read a file, you must have execute permission on the directory containing
that file, and hence on any directory containing that directory as a subdirectory, and so on,
up the tree.
Also file can be written if its permissions allow Write, but it can only be deleted if its
directory's permissions allow Write.
Examples:
chmod a+r myfile
add permission for everyone to read a
file (or just: chmod +r myfile)
Operators:
+ (add access)
- (remove access)
= (set exact access)
...
The other way to use the chmod command is the absolute form. In this
case, you specify a set of three numbers that together determine all
the access classes and types. Rather than being able to change only
particular attributes, you must specify the entire state of the file's
permissions.
The three numbers are specified in the order: user (or owner), group,
other. Each number is the sum of values that specify: read (4), write
(2), and execute (1) access, with 0 (zero) meaning no access. For
example, if you wanted to give yourself read, write, and execute
permissions on myfile; give users in your group read and execute
permissions; and give others only execute permission, the appropriate
number would be calculated as (4+2+1)(4+0+1)(0+0+1) for the three
digits 751. You would then enter
the751
command
as:
chmod
myfile
...
This will allow listing long list
of processes page-by-page.
$ ps aux | more
...
This will wait 5 seconds and
then return the command line
prompt.
$ sleep 5
...
You can suspend the process
running in the foreground by
typing ^Z, i.e.hold down the
[Ctrl] key and type [z]. Then
to put it in the background,
type 'bg' and [Enter].
$ sleep 15
$ kill pid
...
This will stop (temporarily
suspend) the process.
$ sleep 15
[Ctrl+z]
Chapter 6
df, du, gzip, zcat, file, diff, find,
history
...
This will compress myfile
using Gzip compressing tool.
The original file will be
deleted.
...
Classifies the named files in
the current directory
according to the type of data
they contain, for example
ASCII text, pictures,
compressed data, directory,
etc.
$ file *
Compares the contents of
two files and displays the
differences. Lines beginning
...
Searches for all files with the
extension .txt, starting at the
current directory (.) and
working through all subdirectories, then printing the
name of the file to the screen
(simple output).
...
This will give an ordered list
of all the commands that you
have entered. Piping the
output to less command
allows both forward and
backward scrolling of the list
(more command only allows
forward scrolling).
$ history | less
This way you can change the
size of the history buffer (set
Chapter 7
export, printenv, unset, .bashrc,
source, ssh, mount, reboot,
shutdown, crontab
...
This way we can add new
directories in the end of
PATH environment variable
(all directories are divided by
: symbol).
$ export $PATH:/mydir
...
This way we can add
environment variables on
permanent basis. Just insert
export MYVAR=myvalue in
the end of file opened in VI.
This variable will be loaded
automatically at shell start.
$ vi ~/.bashrc
Force reload of environment
variables from ~/.bashrc file.
...
$ mkdir alpha
$ sudo mount -t smbfs //alpha.softservecom.com/install alpha
-o username=yourusername,password=yourpassword
This way we can mount remote SMB network filesystem,
providing credentials for authentication.
If you want the filesystem to be mounted automatically, then
you need to edit /etc/fstab file that has its own format (see the
man page for details).
6.6 Scheduling
$ crontab -e
This command opens crontab file where you can schedule commands
execution. (use sudo if you need the command to be executed with
root permissions)
The format of a line is:
minute (0-59), hour (0-23, 0 = midnight), day (1-31), month (1-12),
weekday (0-6, 0 = Sunday), command
Example:
01 04 1 1 1 /usr/bin/somedirectory/somecommand
The above example will run /usr/bin/somedirectory/somecommand
at 4:01am on January 1st plus every Monday in January.
...
An asterisk (*) can be used so that every instance (every hour, every
weekday, every month, etc.) of a time period is used. Example:
01 04 * * * /usr/bin/somedirectory/somecommand
This command will run /usr/bin/somedirectory/somecommand
at 4:01am on every day of every month.
Comma-separated values can be used to run more than one instance
of a particular command within a time period. Dash-separated values
can be used to run a command continuously. Example:
01,31 04,05 1-15 1,6 * /usr/bin/somedirectory/somecommand
The above example will run /usr/bin/somedirectory/somecommand at
01 and 31 past the hours of 4:00am and 5:00am on the 1st
through the 15th of every January and June.
...
string
meaning
@reboot
Runonce,atstartup.
@yearly
Runonceayear,"0011*".
@annually (sameas@yearly)
@monthly
Runonceamonth,"001**".
@weekly
Runonceaweek,"00**0".
@daily
Runonceaday,"00***".
@midnight (sameas@daily)
@hourly
Runonceanhour,"0****".