UNIX Commands All 11
UNIX Commands All 11
Root (/)
<files> <files>
➢ /tmp : used by some UNIX utilities especially vi and by user to store temporary
files
➢ /usr : contains all the files created by user, including login directory
➢ /unix : kernel
➢ Release V:
– It does not contain / bin.
– It contains / home instead of /usr.
➢ Permissions are associated with every file, and are useful for security.
➢ There are three categories of users:
– Owner (u)
– Group (g)
– Others (o)
➢ There are three types of “access permissions”:
– Read (r)
– Write (w)
– Execute (e)
pwd Command
$ pwd
• Output: /usr/user12
$ pwd
• Output: /usr/user12
$ cd Prog
$ pwd
• Output: /usr/user12/Prog
$ cd ..
Switching to home directory:
$ cd
➢ Switching to /usr/sharma:
$ cd /usr/Sharma
$ cd /
$ logname
Output: user12
$ ls
Output:
a.out
pack1
pack2
test
test.c
Option Description
-x Displays multi columnar output (prior to Release 4)
-F Marks executables with *and directories with /
-r Sorts files in reverse order (ASCII collating sequence by default)
-l The long listing showing seven attributes of a file
-d Forces listing of a directory
-a Shows all files including ., .. And those beginning with a dot
Option Description
-t Sorts files by modification time
-R Recursive listing of all files in sub-directories
-u Sorts files by access time (when used with the –t option)
-i Shows i-node number of a file
-s Displays number of blocks used by a file
➢ Example:
$ ls – l
➢ File type
– 1 st character represents file type:
- rwx rwx rwx
• - --> regular file
• d --> directory file
• c --> character - read
• b --> block read
$ cat dept.lst
01|accounts|6213
02|admin|5423
:
06|training|1006
– To create a file:
➢ The cat command can be used to display contents of more than one file.
➢ Redirection operators:
– < : Input Redirection
– > : Output Redirection
– 2> : Error Redirection
– >> : Append Redirection
– $ cat < file1.txt > result is same as $cat file1.txt > result.
$ cat result
Output: 2 15 20
– >> is append redirection
– The given command will append the contents of file1.lst in result file.
Output: 2 15 20
4 4 8
tac is practically the reverse version of cat command (also spelled backwards)
which prints each line of a file starting from the bottom line and finishing on
the top line to your machine standard output.
$tac myfile.txt
This is last line four
This is line three
This is line two
This is first line one
Usually use of tac command is, that it can provide a great
help in order to debug log files,
reversing the chronological order of log contents.
$ tac /var/log/auth.log
$ cp pack1 temp/pack2
Option - i (interactive)
$cp - i pack1 pack2
cp: overwrite pack2 ?y
Option -r (recursive) to copy entire directory
$cp - r temp mytemp
$ mv pack1 man1
$ wc myfile
3 20 103 myfile
$wc or $wc -
This is standard input
press ctrl-z to stop
• Output: 2 8 44
$ wc infile test
Output: 3 20 103 infile
$ wc - l infile
Output: 3 infile
$ wc - wl infile
Output: 20 3 infile
The following command will take i/p from infile and send o/p to result file
Output: 2 12 60
➢ cmp Command:
➢ comm Command:
– The comm command compares two sorted files. It gives a 3
columnar output:
• First column contains lines unique to the first file.
• Second column contains lines unique to the second file.
• Third column displays the common lines.
$ cat cfile2.lst
$ cat cfile1.lst
A
A F
k K
p W
X X
Z
➢ The diff command is used to display the file differences. It tells the lines of one
file that need to be changed to make the two files identical.
– Example:
Even though you can find all these details in respective files present under the proc
filesystem, it is easier to use uname utility to get these information quickly.
uname [OPTION]...
Option
Uname –r
$ uname
Linux
$ uname -s
Linux
$ uname -n
dev-server
Get kernel release using -r option
to fetch the kernel release information(version
information).
$ uname -r
2.6.32-100.28.5.el6.x86_64
tr Command
$ tr –s “ ” < file1.txt
➢ Example 2: To convert small case into capital case:
If you do not specify relative path then you cannot read original file using
symbolic link because it tries to find original file using relative path
more Command
➢ The more command, from the University of California, Berkeley, is a paging tool.
➢ The more command is used to view one page at a time. It is particularly useful
for viewing large files.
➢ Syntax for more command is as follows:
$ more file1.txt
➢ Example 1:
➢ Example 2:
– When we use + symbol, the previous permissions will be retained and new permissions
will be added.
– When we use = symbol, previous permissions will be overwritten.
➢ Example 3:
➢ Octal notation:
– It describes both category and permission.
– It is similar to = operator (absolute assignment).
• read permission: assigned value is 4
• write permission: assigned value is 2
• execute permission: assigned value is 1
– Example 1:
– Example 2:
– Example 3:
$ mkdir mytemp
$ rmdir doc
➢ External commands
– A new process will be set up
– The file for external command should be available in BIN directory
– E.g – cat, ls , Shell scripts
➢ Internal commands
– shell’s own built in statements, and commands
– No process is set up for such commands.
– E.g cd , echo
Whatis searches the manual page names and displays the manual page descriptions of
any name matched.
Index databases are used during the search and are updated by the mandb
program.
whereis
Whereis
The whereis command lets users locate binary, source, and
manual page files for a command. Following is its syntax:
whereis whereis
The whereis command also produces paths for bonary files, manual pages and source
code
How to specifically search for binaries, manuals, or source code?
If you want to search specifically for, binary, then you can use the –b option.
For example:
whereis -b cp
Similarly, the -m and -s options are used in case you want to find manuals and
sources.
How to limit whereis search as per requirement?
By default whereis tries to find files from hard-coded paths, which are defined
with glob patterns.
However, if you want, you can limit the search using specific command line
options.
if you want whereis to only search for binary files in /usr/bin, use the –B option.
whereis -B /usr/bin/ -f cp
Note: Since you can pass multiple paths this way, the -f command line option
terminates the directory list and signals the start of file names.
Similarly, if you want to limit manual or source searches, you can use the -M and
-S command line options.
Locate
The locate command is very easy to use. All you have to do is to pass it the filename
you want to search.
locate [filename]
For example, if want to search for all filenames that have the string 'dir2' in them,
then
$locate *dir2*
Note: The command 'locate dir2' (no asterisks) will also do as locate
implicitly replaces the name you pass (say NAME) with *NAME*
How locate command works, or, why is it so fast
locate is so fast is because it doesn't read the file system for the searched file or
directory name.
It actually refers to a database (prepared by the command updatedb) to find what
user is looking.
While this is a good approach, it has its share of drawbacks.
The main issue is that after every new file or directory is created on the system,
you need to update the tool's database for it to work correctly. Otherwise, the
command will not be able to find files/directories that are created after the last
database update.
For example, if I try finding files with names containing ‘myfile' string in the
'Downloads' directory of my system, the find command produces one result in the
output:
But when I try performing the same search using the locate command, it produces no
output.
This means that the database locate searches in wasn't updated after the file was
created on the system. So, let's update the database, which can be done using the
updatedb command
To do that:
sudo updatedb
To update database used in locate
sudo updated
if you want, you can make the tool suppress all this information,
and just print the number or count of matching entries instead.
Use -c option.
Zcat
Zcat is a command line utility for viewing the contents of a compressed file without
literally uncompressing it.
It expands a compressed file to standard output allowing you to have a look at its
contents. In addition, zcat is identical to running gunzip -c command. In this guide, we
will explain zcat command examples for beginners.
1. The first example shows how to view contents of a normal file using cat command,
compress it using gzip command and view the contents of the zipped file using zcat as
shown.
$ cat users.list
$ gzip users.list
$ zcat users.list.gz
UNIX
Filters
➢ The head command, by default, will display the first 10 lines of a file.
– Example 1: To display fist 10 lines from file books:
$head books
– Example 2: To display first 5 lines from file books:
$head –5 books
➢ Single command can be used to display lines from more than one file.
➢ The tail command is useful to display last few lines or characters of the file.
– Example 1: To display last ten lines from books:
$tail books
$tail –7 books
– Example 3: To display lines from the 10th line till end of the file:
– Options :
• -c : selects columns specified by list
• -f : selects fields specified by list
• -d : field delimiter (default is tab)
– Example 2: To display characters from 1st to 4th and 31st to 35th from
file bookDetails.lst :
$paste <file1><file2><Enter>
– Options : -d (Field delimiter)
$sort <filename>
– Options are:
• -r : Reverse order
• -n : Numeric sort
• -f : Omit the difference between Upper and lower case alphabets
• -t : Specify delimiter
• -k : to specify fields as primary or secondary key
– Example:
➢ The uniq command fetches only one copy of redundant records and
writes the same to standard output.
– –u option: It selects only non-repeated lines.
– –d option: It selects only one copy of repeated line.
– -c option: It gives a count of occurrences.
➢ To find unique values, the file has to be sorted on that field.
– Example: To find unique values from file myfile.lst
$ uniq myfile.lst
Output file
find Command
– To find the file named .profile starting at the root directory in the
system -print specify the action:
$ find / -name .profile –print
– To find the file named myfile starting at the root directory in the
system
– The following example will search for the string Unix in the file
books.lst. The lines which match the pattern will be displayed.
➢ Options of grep:
– c : It displays count of lines which match the pattern.
– n : It displays lines with the number of the line in the text file which
match the pattern.
– v : It displays all lines which do not match pattern.
– i : It ignores case while matching pattern.
– -w : It forces grep to select only those lines containing matches that
form whole words
➢ Regular Expression:
Expression Description
^ (Caret) match expression at the start of a line, as in ^A.
$ (Question) match expression at the end of a line, as in A$.
\ (Back Slash) turn off the special meaning of the next character, as in \^.
match any one of the enclosed characters, as in [aeiou]. Use Hyphen "-" for a
[ ] (Brackets)
range, as in [0-9].
grep '\^s' files lines starting with '^s', "\" escapes the ^
➢ The fgrep command is useful to search files for one or more patterns,
which cannot be combined together.
➢ It does not use regular expressions. Instead, it does direct string
comparison to find matching lines of text in the input.
Look at the above snapshot, both the files ' file1.txt' and 'file2.txt' are compressed as
'final.gz'.
Each file will get replace by zip version of the file with
extension .gz
How To Compress A Directory
The gzip command will not be able to compress a directory because it can only
compress a single file. To compress a directory you have to use 'tar' command.
Hyphen (-) is not mandatory in 'tar' command.
'c' is to create,
'v' is for verbose, to display output,
'f' to mention destination of your output file,
'z' for specifying compress with gzip.
Syntax:
1. tar cf - <directory> | gzip > <directoryName>
OR
1. tar cvfz office.tar.gz office
Example:
1. tar cf - office | gzip > office.tar.gz
ZIP command in Linux with examples
ZIP is a compression and file packaging utility for Unix. Each file is stored in single .zip
{.zip-filename} file with the extension .zip.
• zip is used to compress the files to reduce file size and also used as file
package utility. zip is available in many operating systems like unix, linux, windows etc.
• If you have a limited bandwidth between two servers and want to transfer
the files faster, then zip the files and transfer.
• The zip program puts one or more compressed files into a single zip archive,
along with information about the files (name, path, date, time of last modification,
protection, and check information to verify file integrity). An entire directory structure
can be packed into a zip archive with a single command.
• Compression ratios of 2:1 to 3:1 are common for text files. zip has one
compression method (deflation) and can also store files without compression. zip
automatically chooses the better of the two for each file to be compressed.
The program is useful for packaging a set of files for distribution; for archiving files;
and for saving disk space by temporarily compressing unused files or directories.
Syntax :
zip [options] zipfile files_list
They let you define your own commands, or command shortcuts, so you can customize
the command line, and make it work the way you want it to work.
In another simple alias example, instead of always typing this ls command to get a
directory listing:
ls -al
You may create alias so I only have to type the lowercase letter "L" like this:
l
Whenever I use this alias, it's exactly the same as if I had typed out the longer ls -al Linux
command.
Using aliases like this you can create anything from simple shortcuts like this to powerfu
custom commands.
Creating a Linux alias is very easy.
You can either enter them at the command line as you're working, or more likely, you'll
put them in one of your startup files, like your .bashrc file, so they will be available
every time you log in.
To create alias above by entering the following command into .bashrc file:
alias l="ls -al"
As you can see, the Linux alias syntax is very easy:
More examples
The unalias command is used to remove entries from the current user's list of
aliases.
Aliases can be created by using the alias command or by making entries in the
appropriate configuration files.
unalias command is also built into several of the most commonly used shells,
including ash, bash (the default shell on most Linux systems), csh and ksh.
The syntax of unalias is:
For example, if a user had an alias named p for the pwd (i.e., present working
directory) command, such alias could be removed with the following:
unalias p
unalias removes aliases created during the current login session.
It also suppresses permanent aliases; however, they are affected only for the current
login session and are restored after the user logs in again.