To list only files and avoid all directories
The following
command will only list files and will exclude directories, special files,
pipes, symbolic links etc:
$ find . -type f -name '*.pl'
$ find . -type f -name '*.pl'
find / -type f
-name hosts 2>/dev/null
Search all directories
Search file called httpd.conf in all directories:
$ find /
-type f -name httpd.conf
Execute command on all
files
Run ls -l command on all *.c files to get extended information :
$ find .
-name "*.c" -type f -exec ls -l {} \;
~ $ echo AMAZON | fold -w 1
A
M
A
Z
O
N
UNIX / Linux pipes and
grep command
grep command often used with shell pipes. In this example, show the name of the hard disk devices:
Display cpu model name:
However, above command can be also used as follows without shell pipe:
Sample outputs:
# dmesg |
egrep '(s|h)d[a-z]'
Display cpu model name:
# cat
/proc/cpuinfo | grep -i 'Model'
However, above command can be also used as follows without shell pipe:
# grep -i
'Model' /proc/cpuinfo
Sample outputs:
model : 30
model name : Intel(R) Core(TM) i7 CPU Q 820 @ 1.73GHz
model : 30
model name : Intel(R) Core(TM) i7 CPU Q 820 @ 1.73GHz
Use grep to search words
only
When you search for boo, grep will match fooboo, boo123,
barfoo35 and more. You can force the grep command to select only those lines
containing matches that form whole words i.e. match only boo word:
$ grep -w
"boo" file
Use grep to search 2
different words
Use the egrep command as follows:
$ egrep
-w 'word1|word2' /path/to/file
Count line when words has
been matched
The grep can report the number of times that the pattern has
been matched for each file using -c (count) option:
Pass the -n option to precede each line of output with the number of the line in the text file from which it was obtained:
Sample outputs:
$ grep -c
'word' /path/to/file
Pass the -n option to precede each line of output with the number of the line in the text file from which it was obtained:
$ grep -n
'root' /etc/passwd
Sample outputs:
1:root:x:0:0:root:/root:/bin/bash
1042:rootdoor:x:0:0:rootdoor:/home/rootdoor:/bin/csh
3319:initrootapp:x:0:0:initrootapp:/home/initroot:/bin/ksh
Write a regular expression for a phone number in unix?
123-456-7890
(123)456-7890
1234567890
123.456.7890
+91(123)456-7890
find missing
number in 1 to 1000 random digits. ?
[/u03/appl/farbat1]$
bannersuresh
####
# # #####
###### #### # #
#
# # #
# # #
# #
####
# # #
# ##### ####
######
#
# # #####
# # # #
#
# # #
# # #
# # # #
####
#### # #
###### #### # #
[/u03/appl/farbat1/suresh]$
echo "This is a file." > file1.txt
[/u03/appl/farbat1/suresh]$
link file1.txt file2.txt
[/u03/appl/farbat1/suresh]$
ls -alrt
total 10
drwxr-xr-x 23 farbat1
arboradm 3072 Sep 28 12:06 ..
-rw-rw-r-- 2 farbat1
arboradm 16 Sep 28 12:06
file2.txt
-rw-rw-r-- 2 farbat1
arboradm 16 Sep 28 12:06
file1.txt
drwxrwxr-x 2 farbat1
arboradm 96 Sep 28 12:06 .
[/u03/appl/farbat1/suresh]$
more file2.txt
This is a file.
[/u03/appl/farbat1/suresh]$
echo "It points to data on the disk." >> file1.txt
[/u03/appl/farbat1/suresh]$
more file2.txt
This is a file.
It points to data
on the disk.
[/u03/appl/farbat1/suresh]$
more file1.txt.txt
file1.txt.txt:
No such file or directory
[/u03/appl/farbat1/suresh]$
more file1.txt
This is a file.
It points to
data on the disk.
[/u03/appl/farbat1/suresh]$
About Symbolic Links
Symbolic links, sometimes called
"soft" links, are different than "hard" links. Instead of
linking to the data of a file, they link
to another link. So in the example above, file2.txtpoints
to the link file1.txt,
which in turn points to the data of the file.
This has several potential benefits. For one
thing, symbolic links (also called "symlinks" for short) can link to
directories. Also, symbolic links can cross file system boundaries, so a
symbolic link to data on one drive or partition can exist on another drive or
partition.
You should also be aware that, unlike hard
links, removing the file (or directory) that a symlink points to will break the
link. So if we create file1.txt:
echo "This is a file." > file1.txt
...and create a symbolic link to it:
ln -s file1.txt file2.txt
...we can cat either one of these to see the
contents:
cat file1.txt
This is a file.
cat file2.txt
This is a file.
...but if we remove file1.txt:
rm file1.txt
...we can no longer access the data it
contained with our symlink:
cat file2.txt
cat: file2.txt: No such file or directory
[/u03/appl/farbat1/suresh]$
ls -alrt
total 10
drwxr-xr-x 23 farbat1
arboradm 3072 Sep 28 12:06 ..
drwxrwxr-x 2 farbat1
arboradm 96 Sep 28 12:06 .
-rw-rw-r-- 2 farbat1
arboradm 47 Sep 28 12:07
file2.txt
-rw-rw-r-- 2 farbat1
arboradm 47 Sep 28 12:07
file1.txt
[/u03/appl/farbat1/suresh]$
unlink file2.txt
[/u03/appl/farbat1/suresh]$
ls -alrt
total 8
drwxr-xr-x 23 farbat1
arboradm 3072 Sep 28 12:06 ..
-rw-rw-r-- 1 farbat1
arboradm 47 Sep 28 12:07
file1.txt
drwxrwxr-x 2 farbat1
arboradm 96 Sep 28 12:13 .
[/u03/appl/farbat1/suresh]$
Tr
command Examples:
1. Convert lower case letters to upper case
The following tr command translates the lower case letters to capital letters in the give string:
>echo "linux dedicated server" | tr "[:lower:]"
"[:upper:]"
LINUX DEDICATED SERVER
>echo "linux dedicated server" | tr "[a-z]"
"[A-Z]"
LINUX DEDICATED SERVER
2. Transform upper case letters to lower case.
Similar to the above example, you can translate the uppercase letters to small letters.
> echo "UNIX DEDICATED SERVER" | tr "[:upper:]"
"[:lower:]"
unix dedicated server
> echo "UNIX DEDICATED SERVER" | tr "[A-Z]"
"[a-z]"
unix dedicated server
3. Replace non-matching characters.
The -c option is used to replace the non-matching characters with another set of characters.
> echo "unix" | tr -c "u" "a"
uaaa
In the above example, except the character "c" other characters are replaced with "a"
4. Delete non-printable characters
The -d option can be used to delete characters. The following example deletes all the non-printable characters from a file.
>tr -cd "[:print:]" < filename
5.
Squeezing characters
You can squeeze more than one occurrence of continuous characters with single occurrence. The following example squeezes two or more successive blank spaces into a single space.
You can squeeze more than one occurrence of continuous characters with single occurrence. The following example squeezes two or more successive blank spaces into a single space.
>echo "linux server"
| tr -s " "
linux server
Here you can replace the space character with any other character by specifying in set2.
> "linux server" | tr
-s " " ","
linux,server
6. Delete characters
The following example removes the word linux from the string.
> echo "linuxserver" | tr -d "linux"
server
tee examples
ls -1 *.txt | wc -l | tee count.txt
In the above example, the ls command lists
all files in the current directory that have the filename extension .txt, one file per line; this
output is piped to wc,
which counts the lines and outputs the number; this output is piped to tee, which writes the output to
the terminal, and writes
the same information to the file count.txt.
If count.txt already exists, it is overwritten.
cat list.txt
apples
oranges
potatoes
lemons
garlic
nl list.txt
1 apples
2 oranges
3 potatoes
4 lemons
5 garlic
In the above example, we use the cat command to
display the contents of list.txt. Then we use nl to number each line and display the result to standard output.
pg examples
pg myfile.txt
Displays the first screenful of the contents
of text file myfile.txt,
and a prompt (":"). Pressing the RETURN key displays the next
page, or any of the command listed above may be entered to otherwise navigate the
file.
Paste
Command Examples:
Create the following three files in your unix or linux servers to practice to practice the examples:
Create the following three files in your unix or linux servers to practice to practice the examples:
>cat file1
Unix
Linux
Windows
>cat file2
Dedicated server
Virtual server
>cat file3
Hosting
Machine
Operating system
1. Merging files in parallel
By default, the paste command merges the files in parallel. The paste command writes corresponding lines from the files as a tab delimited on the terminal.
> paste file1 file2
Unix Dedicated server
Linux Virtual server
Windows
> paste file2 file1
Dedicated server Unix
Virtual server Linux
Windows
2. Specifying the delimiter
Paste command uses the tab delimiter by default for merging the files. You can change the delimiter to any other character by using the -d option.
>paste -d"|" file1 file2
Unix|Dedicated server
Linux|Virtual server
Windows|
In the above example, pipe delimiter is specified
3. Merging files in sequentially.
You can merge the files in sequentially using the -s option. The paste command reads each file in sequentially. It reads all the lines from a single file and merges all these lines into a single line.
>paste -s file1 file2
Unix Linux Windows
Dedicated server Virtual
server
The following example shows how to specify a delimiter for sequential merging of files:
> paste -s -d"," file1 file2
Unix,Linux,Windows
Dedicated server,Virtual server
4. Specifying multiple delimiters.
Multiple delimiters come in handy when you want to merge more than two files with different delimiters. For example I want to merge file1, file2 with pipe delimiter and file2, file3 with comma delimiter. In this case multiple delimiters will be helpful.
>paste -d"|," file1 file2 file3
Unix|Dedicatedserver,Hosting
Linux|Virtualserver,Machine
Windows|,Operating system
5. Combining N consecutive lines
The paste command can also be used to merge N consecutive lines from a file into a single line. The following example merges 2 consecutive lines into a single line
>cat file1 | paste - -
Unix Linux
Windows
CUT
$ cat test.txt
cat command for file oriented operations.
cp command for copy files or directories.
ls command to list out files and directories with its attributes.
1. Select Column of Characters
To extract only a desired column from a file
use -c option. The following example displays 2nd character from each line of a
file test.txt
$ cut -c2 test.txt
a
p
s
As seen above, the characters a, p, s are the
second character from each line of the test.txt file.
2. Select Column of Characters using Range
Range of characters can also be extracted from
a file by specifying start and end position delimited with -. The following
example extracts first 3 characters of each line from a file called test.txt
$ cut -c1-3 test.txt
cat
cp
ls
3. Select Column of Characters using either Start or End Position
Either start position or end position can be
passed to cut command with -c option.
The following specifies only the start
position before the ‘-‘. This example extracts from 3rd character to end of
each line from test.txt file.
$ cut -c3- test.txt
t command for file oriented operations.
command for copy files or directories.
command to list out files and directories with its attributes.
The following specifies only the end position
after the ‘-‘. This example extracts 8 characters from the beginning of each
line from test.txt file.
$ cut -c-8 test.txt
catcomm
cp comma
ls comma
The entire line would get printed when you
don’t specify a number before or after the ‘-‘ as shown below.
$ cut -c- test.txt
cat command for file oriented operations.
cp command for copy files or directories.
ls command to list out files and directories with its attributes.
4. Select a Specific Field from a File
Instead of selecting x number of characters,
if you like to extract a whole field, you can combine option -f and -d. The
option -f specifies which field you want to extract, and the option -d
specifies what is the field delimiter that is used in the input file.
The following example displays only first
field of each lines from /etc/passwd file using the field delimiter : (colon).
In this case, the 1st field is the username. The file
$ cut -d':' -f1 /etc/passwd
root
daemon
bin
sys
sync
games
bala
5. Select Multiple Fields from a File
You can also extract more than one fields from
a file or stdout. Below example displays username and home directory of users
who has the login shell as “/bin/bash”.
$ grep "/bin/bash" /etc/passwd | cut -d':' -f1,6
root:/root
bala:/home/bala
To display the range of fields specify start
field and end field as shown below. In this example, we are selecting field 1
through 4, 6 and 7
$ grep "/bin/bash" /etc/passwd | cut -d':' -f1-4,6,7
root:x:0:0:/root:/bin/bash
bala:x:1000:1000:/home/bala:/bin/bash
6. Select Fields Only When a Line Contains the Delimiter
In our /etc/passwd example, if you pass a
different delimiter other than : (colon), cut will just display the whole line.
In the following example, we’ve specified the
delimiter as | (pipe), and cut command simply displays the whole line, even
when it doesn’t find any line that has | (pipe) as delimiter.
$ grep "/bin/bash" /etc/passwd | cut -d'|' -f1
root:x:0:0:root:/root:/bin/bash
bala:x:1000:1000:bala,,,:/home/bala:/bin/bash
But, it is possible to filter and display only
the lines that contains the specified delimiter using -s option.
The following example doesn’t display any
output, as the cut command didn’t find any lines that has | (pipe) as delimiter
in the /etc/passwd file.
$ grep "/bin/bash" /etc/passwd | cut -d'|' -s -f1
7. Select All Fields Except the Specified Fields
In order to complement the selection field
list use option –complement.
The following example displays all the fields
from /etc/passwd file except field 7
$ grep "/bin/bash" /etc/passwd | cut -d':' --complement -s -f7
root:x:0:0:root:/root
bala:x:1000:1000:bala,,,:/home/bala
8. Change Output Delimiter for Display
By default the output delimiter is same as
input delimiter that we specify in the cut -d option.
To change the output delimiter use the option
–output-delimiter as shown below. In this example, the input delimiter is :
(colon), but the output delimiter is # (hash).
$ grep "/bin/bash" /etc/passwd | cut -d':' -s -f1,6,7 --output-delimiter='#'
root#/root#/bin/bash
bala#/home/bala#/bin/bash
9. Change Output Delimiter to Newline
In this example, each and every field of the
cut command output is displayed in a separate line. We still used
–output-delimiter, but the value is $’\n’ which indicates that we should add a
newline as the output delimiter.
$ grep bala /etc/passwd | cut -d':' -f1,6,7 --output-delimiter=$'\n'
bala
/home/bala
/bin/bash
Install the mailx command
On Ubuntu/Debian based
systems the mailx command is available from 2 different packages -
We shall be using the heirloom-mailx package because it has more
features and options.
On CentOS/Fedora based systems, there is only one package named "mailx" which is the heirloom package.
On CentOS/Fedora based systems, there is only one package named "mailx" which is the heirloom package.
To find out what mailx
package is installed on your system, check the "man mailx" output and
scroll down to the end and you should see some useful information.
# ubuntu/debian
$ sudo apt-get install heirloom-mailx
# fedora/centos
$ sudo yum install mailx
Using the mailx command
Once installed, the mailx
command can be directly referenced with the name mail, so you just type in that
in the command line.
1. Simple mail
Run the following
command, and then mailx would wait for you to enter the message of the email.
You can hit enter for new lines. When done typing the message, press Ctrl+D and
mailx would display EOT.
After than mailx
automatically delivers the email to the destination.
$ mail -s "This is the subject" someone@example.com
Hi someone
How are you
I am fine
Bye
EOT
2. Take message from a file
The message body of the
email can be taken from a file as well.
$ mail -s "This is Subject" someone@example.com < /path/to/file
The message can also be
piped using the echo command -
$ echo "This is message body" | mail -s "This is Subject" someone@example.com
3. Multiple recipients
To send the mail to
multiple recipients, specify all the emails separated by a comma
$ echo "This is message body" | mail -s "This is Subject" someone@example.com,someone2@example.com
4. CC and BCC
The "-c" and
"-b" options can be used to add CC and BCC addresses respectively.
$ echo "This is message body" | mail -s "This is Subject" -c ccuser@example.com someone@example.com
5. Specify From name and address
To specify a
"FROM" name and address, use the "-r" option. The name
should be followed by the address wrapped in "<>".
$ echo "This is message body" | mail -s "This is Subject" -r "Harry<harry@gmail.com>" someone@example.com
6. Specify "Reply-To" address
The reply to address is
set with the internal option variable "replyto" using the
"-S" option.
# replyto email
$ echo "This is message" | mail -s "Testing replyto" -S replyto="mark@gmail.com" someone@example.com
# replyto email with a name
$ echo "This is message" | mail -s "Testing replyto" -S replyto="Mark<mark@gmail.com>" someone@example.com
7. Attachments
Attachments can be added
with the "-a" option.
$ echo "This is message body" | mail -s "This is Subject" -r "Harry<harry@gmail.com>" -a /path/to/file someone@example.com
[/u03/appl/farbat1/suresh]$
finger -p ch
Login name:
ankloe In real life:
Sysadmin CH
Directory:
/home_ldap/ankloe Shell:
/bin/bash
Never logged
in.
No unread mail
Login name:
bjholt In real life:
Sysadmin CH
Directory:
/home_ldap/bjholt Shell:
/bin/bash
Never logged
in.
No unread mail
Login name:
roleem In real life:
SysAdmin CH
Directory:
/home/roleem Shell: /bin/bash
Never logged
in.
No unread mail
[/u03/appl/farbat1/suresh]$
finger -p suresh
Login name:
skunku In real life:
Suresh Kunku
Directory:
/home_ldap/skunku Shell:
/bin/bash
On since Sep 28
12:00:30 on pts/2 from nlamsp6xen152.upcit.ds.upc.biz
No unread mail
[/u03/appl/farbat1/suresh]$
EmoticonEmoticon