Unix Commans
Unix Commans
If you want to see the list your UNIX of files on or Linux system, use the ‘ls’ command. It shows the
files /directories in your current directory. Note:
You can use ‘ls -R’ to shows all the files not only in directories but also subdirectories
basics commands
are case-sensitive.
If you enter,
an error.
‘ls -al’ gives detailed information of the files. The command provides information in a columnar
format. The columns contain the following information:
Hidden items in UNIX/Linux begin with –at the start, of the file or directory. Any Directory/file
starting with a ‘.’ will not be seen unless you request for it. To view
hidden files, use the command.
ls -a
The ‘cat’ server command is used to display text files. It can also be used for copying, combining and
creating new text files. Let’s see how it works.
2. Add content
As soon as you insert this command and hit enter, the files are concatenated, but you do not see a
result. This is because Bash Shell (Terminal) is silent type. Shell Commands will never give you a
confirmation message like “OK” or “Command Successfully Executed”. It will only show a message
when something goes wrong or when an error has occurred.
cat sample
Note: Only text files can be displayed and combined using this command.
Deleting Files
The ‘rm’ command removes files from the system without confirmation. To remove a file use syntax
–
rm filename
mv filename new_file_location
mv sample2 /home/guru99/Documents
mv command needs super user permission. Currently, we are executing the command as a standard
user. Hence we get the above error. To overcome the error use command.
sudo command_you_want_to_execute
Sudo program allows regular users to run programs with the security privileges of the superuser or
root.
Sudo command will ask for password authentication. Though, you do not need to know the root
password. You can supply your own password. After authentication, the system will invoke the
requested command.
Sudo maintains a log of each command run. System administrators can trackback the person
responsible for undesirable changes in the system.
guru99@VirtualBox:~$
mv filename newfilename
NOTE: By default, the password you entered for sudo is retained for 15 minutes per terminal. This
eliminates the need of entering the password time and again.
You only need root/sudo privileges, only if the command involves files or directories not owned by
the user or group running the commands
Directory Manipulations
Enough with File manipulations! Let’s learn some directory manipulation Linux
commands with examples and syntax.
Creating Directories
Directories can be created on a Linux operating system using the following command
mkdir directoryname
This command will create a subdirectory in your present working directory, which is usually your
“Home Directory”.
For example,
mkdir mydirectory
If you want to create a directory in a different location other than ‘Home directory’, you could use
the following command –
mkdir
For example:
mkdir /tmp/MUSIC
Removing Directories
rmdir directoryname
Example
rmdir mydirectory
Renaming Directory
The ‘mv’ (move) command (covered earlier) can also be used for renaming directories. Use the
below-given format:
mv directoryname newdirectoryname
Man stands for manual which is a reference book of a Linux operating system. It is similar to HELP
file found in popular software.
To get help on any command that you do not understand, you can type
man
The terminal would open the manual page for that command.
For an example, if we type man man and hit enter; terminal would give us information on man
command
The History Command
History command shows all the basic commands in Linux that you have used in the past for the
current terminal session. This can help you refer to the old commands you have entered and re-used
them in your operations again.
This command clears all the clutter on the terminal and gives you a clean window to work on, just
like when you launch the terminal.
Pasting commands into the terminal
Many times you would have to type in long commands on the Terminal. Well, it can be annoying at
times, and if you want to avoid such a situation then copy, pasting the commands can come to
rescue.
For copying, the text from a source, you would use Ctrl + c, but for pasting it on the Terminal, you
need to use Ctrl + Shift + p. You can also try Shift + Insert or select Edit>Paste on the menu
NOTE: With Linux upgrades, these shortcuts keep changing. You can set your preferred shortcuts via
Terminal> Edit> Keyboard Shortcuts.
Printing in Unix/Linux
Let’s try out some Linux basic commands with examples that can print files in a format you want.
What more, your original file does not get affected at all by the formatting that you do. Let us learn
about these commands and their use.
pr’ command
This command helps in formatting the file for printing on the
terminal. There are many Linux terminal commands available with
this command which help in making desired format changes on
file. The most used ‘pr’ Unix commands with examples are listed
below.
Option Function
-x Divides the data into ‘x’ columns
-h “header” Assigns “header” value as the report header
-t Does not print the header and top/bottom margins
-d Double spaces the output file
-n Denotes all line with numbers
-l page length Defines the lines (page length) in a page. Default is 56
-o margin Formats the page by the margin number
Let us try some of the options and study their effects.
Dividing data into columns
‘Tools’ is a file (shown below).
Assigning a header
The syntax is:
pr -h "Header" Filename
The ‘-h’ options assigns “header” value as the report header.
lp -nc
Prints “c” copies of the File
lpr c
lp -d
Specifies name of the printer
lpr -P
Command used to install and update
apt-get
packages
How Do I Do That?
Each command has three numbered file
descriptors that are opened automatically:
standard input (stdin, file descriptor 0)
The normal input to the program
standard output (stdout, file descriptor 1)
The normal output from the program
standard error (stderr, file descriptor 2)
Error messages from the program
By default, these file descriptors are connected to
the terminal, if one is available, so standard input
comes from the terminal keyboard, and standard
output and standard error go to the terminal screen.
Programs may open any other connections they need
to read or write files, communicate with other local
programs, or communicate with programs over the
network.
Tip
$ cal 7 2006
July 2006
Su Mo Tu We Th Fr Sa
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
$ cal 7 2006 >
month.txt
$ cat
month.txt
July 2006
Su Mo Tu We Th Fr Sa
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
$ cal
3 2009
>>
month.txt
$ cat
month.txt
July 2006
Su Mo Tu We Th Fr Sa
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
March 2009
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
$ cal
17 2009
>
month.txt
$ cal
17 2009 2
>errors
$ cat
errors
$ cal
17 2009
>
month.txt 2
>errors
$ echo "
2^8
" >
problem
$ bc <
problem
256
$ bc <
problem
>
result
Piping
$ mount
/dev/mapper/main-root on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/hdc2 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
/dev/mapper/main-home on /home type ext3 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
/dev/sdb on /media/disk type vfat (rw,noexec,nosuid,nodev,shortname=winnt,uid=503)
$ mount | grep
/dev/mapper
$ mount | grep
/dev/mapper
| sort
/dev/mapper/main-home on /home type ext3 (rw)
/dev/mapper/main-root on / type ext3 (rw)
output
$ cat
output
adm
apache
avahi
beaglidx
bin
chip
chris
daemon
dbus
distcache
output
|head
$ cal >
foo/bar/baz
Tip
Note that the error message starts with bash, indicating that it was
produced by the shell and not by the cal command.
A command is not aware of file redirection unless it
has specifically been programed to check the
standard file descriptors or perform special
operations on them (such as changing terminal
characteristics). Redirected file descriptors are
inherited by applications that were started by
commands; in this example, the nice command
starts the cal command, and cal inherits the
redirection set up for nice:
test.txt
$ cat
test.txt
July 2006
Su Mo Tu We Th Fr Sa
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
What About...
...redirecting standard output and standard error to the same destination?
/tmp/calresult
2>&1
/tmp/calresult
...redirecting to a device?
$ head
-50 /etc/services
>
/dev/lp0
$ cal -y | tee
/tmp/thisyear.txt
| head
-2
Tip
To send a copy of the data to the screen, use tee with the device
file /dev/tty (the current terminal):
$ djpeg
/usr/share/wallpapers/floating-leaves.jpg
| pnmscale
0.5
|
ppmtopgm | ppmnorm | cjpeg | tee
/tmp/final.jpg
| display -
Piping in Unix or Linux
A pipe is a form of redirection (transfer of standard output to some other destination)
that is used in Linux and other Unix-like operating systems to send the output of one
command/program/process to another command/program/process for further
processing. The Unix/Linux systems allow the stdout of a command to be connected
to the stdin of another command. You can make it do so by using the pipe
character ‘|’.
The pipe is used to combine two or more commands, and in this, the output of one
command acts as input to another command, and this command’s output may act as
input to the next command, and so on. It can also be visualized as a temporary
connection between two or more commands/ programs/ processes. The command line
programs that do the further processing are referred to as filters.
Syntax:
command_1 | command_2 | command_3 | .... | command_N
ls | grep file.txt
In this first we are using `ls` to list all file and directories in the current directory, then
passing its output to `grep` command and searching for file name `file.txt`. The output
of the ls command is sent to the input of the grep command, and the result is a list of
files that match the search term.
2. List all files and directories and give them as input to `more`
commands using piping in Linux.
$ ls -l | more
$ ls -l | more
The more command takes the output of $ ls -l as its input. The net effect of this
command is that the output of ls -l is displayed one screen at a time. The pipe acts as a
container which takes the output of ls -l and gives it to more as input. This command
does not use a disk to connect standard output of ls -l to the standard input of more
because pipe is implemented in the main memory.
In terms of I/O redirection operators, the above command is equivalent to the
following command sequence.
$ ls -l -> temp
more -> temp (or more temp)
[contents of temp]
rm temp
ls -l sort -k 5
This command lists all the files in the current directory, and then uses the `sort` command to sort the list by the fifth
column, which represents the file size.
4. Use sort and uniq command to sort a file and print unique
values using piping in Linux
$ sort record.txt | uniq
This will sort the given file and print the unique values only.
This command selects the first 7 lines through (head -7) command and that will be input to (tail -5) command which will
finally print last 5 lines from those 7 lines.
6. Use ls and find to list and print all lines matching a particular
pattern in matching files.
$ ls -l | find ./ -type f -name "*.txt" -exec grep "program" {} \;
This command selects files with .txt extension in the given directory and searches for patterns like “program” in the
above example and prints those which have program in them.
ls -l | find ./ -type f -name “*.txt” -exec grep “program” {} \;
7. Use cat, grep, tee and wc command to read the particular entry
from user and store in a file and print line count.
$ cat result.txt | grep "Rajat Dua" | tee file2.txt | wc -l
This command selects Rajat Dua and store them in file2.txt and print total number of lines matching Rajat Dua
If we want to redirect both into the same file, then we can use &> as we saw above, or else we can
use file descriptor combination operators. If we wish to use the file descriptor combination
operators, then we must do this after we have redirected into the file, or else only the standard
output gets redirected:
So we have a command here to get the number of unique docker images, ignoring the version
of the image.
Many console applications are designed for exactly this use, which is why they can often
consume input from standard input and write to standard output.
Certain applications also have special modes that allow for this – git, for example, has what is
termed porcelain and plumbing commands, where the plumbing commands are specially
designed to be combined in this manner while the porcelain commands are designed for
human consumption.
Objectives:
First we will discuss the topics left over from Chapter 8. Then, Chapter 9 discusses methods of
communicating in UNIX. The objectives important to this chapter are:
using write
using mesg
using wall
UNIX mail utilities
sending files in the mail