Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Wednesday, 22 January 2020

mv and rename Commands

I created a file called myself:
 
Red Hat > ls
Red Hat > touch myself
Red Hat > ls
myself
Red Hat >
 
I decided it should have been called yourself so I used mv to change its name:
 
Red Hat > mv myself yourself
Red Hat > ls
yourself
Red Hat >
 
Then I decided to use the rename command to call it himself instead. To do this I had to provide the part of the name I wanted to change, what I wanted to change it to and the filename to act upon (if that makes sense):
 
Red Hat > rename yourself himself yourself
Red Hat > ls
himself
Red Hat >
 
This approach is more complicated but it gives greater flexibility. To try this out I created several files with .txt suffixes:
 
Red Hat > rm *
Red Hat > touch file{1..5}.txt
Red Hat > ls
file1.txt  file2.txt  file3.txt  file4.txt  file5.txt
Red Hat >
 
I then decided to remove the suffixes and did this as follows:
 
Red Hat > rename .txt '' file*
Red Hat > ls
file1  file2  file3  file4  file5
Red Hat >

Tuesday, 21 January 2020

Octal Numbers

This is a variation on a Linux challenge I found elsewhere on the Internet. I created a file of numbers as follows:
 
Red Hat >cat file_of_numbers
134
065
258
168
Red Hat >
 
I wrote the following simple script to add the numbers, hoping that the result would be 625:
 
Red Hat >cat count_total.bash
#!/bin/bash
total=0
for x in `cat file_of_numbers`
do
((total+=$x))
done
echo "Total = $total"
Red Hat >
 
However, when I ran it the result was slightly different:
 
Red Hat >./count_total.bash
Total = 613
Red Hat >
 
That was because the leading zero in 065 told Linux to treat it as octal which equals 6x8+5 = 53 in decimal.
 
I removed the leading zero:
 
Red Hat >cat file_of_numbers
134
65
258
168
Red Hat >
 
…then Linux gave me the answer I wanted:
 
Red Hat >./count_total.bash
Total = 625
Red Hat >

Friday, 17 January 2020

How to See the OS Version in Red Hat Linux

You can see your operating system version in Red Hat Linux as follows:

Red Hat > cat /etc/redhat-release
Red Hat Enterprise Linux Server release 7.7 (Maipo)
Red Hat >

Tuesday, 14 January 2020

How to Process a File Line by Line


Sometimes you need to process a file line by line. You can do this as follows:
 
First you need a file. Here is one I made earlier:
 
Linux > cat input_file
0123456789
9876543210
ABCDEFGHIJ
KLMNOPQRST
Linux >
 
I wrote this script to process the file I had created:
 
Linux > cat script.ksh
#!/bin/bash
i=1
for j in `cat input_file`
do
echo "Line $i = $j"
((i+=1))
done
Linux >
 
It produced the following output when I ran it:
 
Linux > ./script.ksh
Line 1 = 0123456789
Line 2 = 9876543210
Line 3 = ABCDEFGHIJ
Line 4 = KLMNOPQRST
Linux >

Saturday, 13 September 2014

How to Change Your Command Prompt in UNIX or Linux

If you do not like your command prompt in UNIX or Linux, you can change it with the export PS1= command. In the example below, I started off with a command prompt of UBUNTU > :

UBUNTU > 

... then I changed it to show the name of the directory I was in. Here is one way to do this:

UBUNTU > export PS1='`pwd` > '
/home/andrew > cd Java
/home/andrew/Java > cd /
/ >

... and here is another:

/ > export PS1='$PWD > '
/ > cd
/home/andrew > cd Java
/home/andrew/Java > 

Finally, I changed it to say Andrew's PC: 

/home/andrew/Java > export PS1="Andrew's PC: "
Andrew's PC:

Thursday, 9 January 2014

How to See your IP Address in UBUNTU

You can do this from the Linux prompt as follows. The value you want is in bold. I hope to be using this a lot in future posts:

UBUNTU: ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0c:6e:e7:f1:92 
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
          Interrupt:19 Base address:0x9800

lo        Link encap:Local Loopback 
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:2102 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2102 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:217564 (217.5 KB)  TX bytes:217564 (217.5 KB)

wlan0     Link encap:Ethernet  HWaddr 00:14:6c:8a:f4:72 
          inet addr:192.168.2.2  Bcast:192.168.2.255  Mask:255.255.255.0
          inet6 addr: fe80::214:6cff:fe8a:f472/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:39507 errors:0 dropped:0 overruns:0 frame:0
          TX packets:37777 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:39342715 (39.3 MB)  TX bytes:7605863 (7.6 MB)

UBUNTU:

Monday, 30 September 2013

A Problem We Had With cron

We had a problem recently where a job was scheduled on cron something like this:
 
25 14 23 09 1 /bin/date > /usr/users/oracle/andrew/date.log 2>&1
 
This should run at 1425 hours but will it run just on Monday 23rd September or will it run every Monday? I didn’t know as I only ever specify the day and month OR the day of the week, never both. I waited until the following time:
 
Tru64 > date
Mon Sep 30 14:20:40 BST 2013
Tru64 >
 
I added the line above to the cron file then waited until the following time:
 
Tru64 > date
Mon Sep 30 14:27:32 BST 2013
Tru64 >
 
The job had run as the output file had been updated with the run time so it seems that, on Tru64 at least, it will run every Monday irrespective of the day and month:
 
Tru64 > pwd
/usr/users/oracle/andrew
Tru64 > cat date.log
Mon Sep 30 14:25:00 BST 2013
Tru64 >
 
If you want it to run only on 23rd September, you should schedule it like this:
 
25 14 23 09 * /bin/date > /usr/users/oracle/andrew/date.log 2>&1
 
I moved to a Solaris machine and added the line below to the cron file.
 
16 15 30 09 2 /usr/bin/date > /export/home/oracle/andrew/date.log 2>&1
 
This should run at 1516 hours but will it run just on Tuesday 30th September or will it run every 30th September? I waited until the following time:
 
Solaris > date
Monday, 30 September 2013 15:16:22 BST
Solaris >
 
The job had run as the output file had been updated with the run time so it seems that, on Solaris too, it will run every 30th September irrespective of the day of the week:
 
Solaris > pwd
/export/home/oracle/andrew
Solaris > cat date.log
Mon Sep 30 15:16:00 BST 2013
Solaris >
 
If you want it to run only on Tuesdays, you should schedule it like this:
 
16 15 * * 2 /usr/bin/date > /export/home/oracle/andrew/date.log 2>&1
 
To summarise, if you schedule a job on cron and specify a day and month AND a day of the week, it will run when EITHER condition is satisfied, it does not need to wait until both conditions are satisfied.

Tuesday, 5 February 2013

UNIX compress Command

You can use the UNIX compress command to make a file smaller. Here is a worked example. I started by making a file called directory_list. I copied this file into another file called directory_list_save for future reference. Then I compressed the file called directory_list. This made the file much smaller and gave it a suffix of .Z. Finally, I compared the sizes of directory_list_save and directory_list.Z:

Tru64:/usr/users/oracle/andrew > ls -R $ORACLE_HOME > directory_list
Tru64:/usr/users/oracle/andrew > cp directory_list directory_list_save
Tru64:/usr/users/oracle/andrew > ls -l directory_list*
-rw-r--r--   1 oracle   dba      1971685 Feb  5 14:43 directory_list
-rw-r--r--   1 oracle   dba      1971685 Feb  5 14:43 directory_list_save
Tru64:/usr/users/oracle/andrew > compress directory_list
Tru64:/usr/users/oracle/andrew > ls -l directory_list*
-rw-r--r--   1 oracle   dba       558245 Feb  5 14:43 directory_list.Z
-rw-r--r--   1 oracle   dba      1971685 Feb  5 14:43 directory_list_save
Tru64:/usr/users/oracle/andrew >

You cannot read a file once you have compressed it as it is then in a different format. The purpose of compressing a file is to make it smaller so that it is easier to store or to send to somewhere else.  I created the file on a Tru64 machine then decided to send it to a Solaris machine using scp.
 
Tru64:/usr/users/oracle/andrew > scp directory_list.Z \
> zge-mktred-ddb1:/export/home/oracle/andrew
The authenticity of host 'zge-mktred-ddb1 (10.80.1.116)' can't be established.
RSA key fingerprint is d6:59:ba:8d:c6:24:21:1b:91:c2:3b:c5:5a:5b:8c:f9.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'zge-mktred-ddb1,10.80.1.116' (RSA) to the list of known hosts.
Password:
directory_list.Z                                                                                                           100%  545KB 545.2KB/s   00:00
Tru64:/usr/users/oracle/andrew >

I read somewhere that you could compress a file on one platform and uncompress it on another. So, once it was on the Solaris machine, I uncompressed it. This proved that a file compressed on Tru64 could be uncompressed on Solaris. Notice how this removed the .Z suffix from the file name. Finally, I compressed the file again on the Solaris machine:
 
Solaris:/export/home/oracle/andrew > ls -l directory_list*
-rw-r--r--   1 oracle   dba       558245 Feb  5 14:49 directory_list.Z
Solaris:/export/home/oracle/andrew > uncompress directory_list.Z
Solaris:/export/home/oracle/andrew > ls -l directory_list*
-rw-r--r--   1 oracle   dba      1971685 Feb  5 14:49 directory_list
Solaris:/export/home/oracle/andrew > compress directory_list
Solaris:/export/home/oracle/andrew > ls -l directory_list*
-rw-r--r--   1 oracle   dba       558245 Feb  5 14:49 directory_list.Z
Solaris:/export/home/oracle/andrew >

Then I sent the compressed file back to the Tru64 machine:

Solaris:/export/home/oracle/andrew > scp directory_list.Z \
> sredsvr8:/usr/users/oracle/andrew
The authenticity of host 'sredsvr8 (10.80.1.32)' can't be established.
DSA key fingerprint is 10:c8:14:df:7a:bb:a7:56:1d:7d:f5:c7:76:ee:2a:33.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'sredsvr8,10.80.1.32' (DSA) to the list of known hosts.
oracle@sredsvr8's password:
directory_list.Z     100% |**********************************************************************************************************|   545 KB    00:00
Solaris:/export/home/oracle/andrew >

Once the file was back on the Tru64 machine, I uncompressed it to prove that you could compress a file on a Solaris machine then uncompress it on a Tru64 machine. Finally I used the diff command to check that the uncompressed file matched the backup copy, which I made at the start of the example:
 
Tru64:/usr/users/oracle/andrew > ls -l directory_list*
-rw-r--r--   1 oracle   dba       558245 Feb  5 15:15 directory_list.Z
-rw-r--r--   1 oracle   dba      1971685 Feb  5 14:43 directory_list_save
Tru64:/usr/users/oracle/andrew > uncompress directory_list.Z
Tru64:/usr/users/oracle/andrew > ls -l directory_list*
-rw-r--r--   1 oracle   dba      1971685 Feb  5 15:15 directory_list
-rw-r--r--   1 oracle   dba      1971685 Feb  5 14:43 directory_list_save
Tru64:/usr/users/oracle/andrew > diff directory_list directory_list_save
Tru64:/usr/users/oracle/andrew >

Wednesday, 23 January 2013

The UNIX and Linux banner Command

With the banner command, you can use the # symbol to create large messages:

UBUNTU > banner Andrew
   #
  # #    #    #  #####   #####   ######  #    #
 #   #   ##   #  #    #  #    #  #       #    #
#     #  # #  #  #    #  #    #  #####   #    #
#######  #  # #  #    #  #####   #       # ## #
#     #  #   ##  #    #  #   #   #       ##  ##
#     #  #    #  #####   #    #  ######  #    #

UBUNTU > 


If the message has two words, the second appears below the first:

UBUNTU > banner Red Rum
######
#     #  ######  #####
#     #  #       #    #
######   #####   #    #
#   #    #       #    #
#    #   #       #    #
#     #  ######  #####

######
#     #  #    #  #    #
#     #  #    #  ##  ##
######   #    #  # ## #
#   #    #    #  #    #
#    #   #    #  #    #
#     #   ####   #    #

UBUNTU >


But, if you put the message in quotes, the two words appear on the same line:

UBUNTU > banner 'Red Rum'
######                          ######
#     #  ######  #####          #     #  #    #  #    #
#     #  #       #    #         #     #  #    #  ##  ##
######   #####   #    #         ######   #    #  # ## #
#   #    #       #    #         #   #    #    #  #    #
#    #   #       #    #         #    #   #    #  #    #
#     #  ######  #####          #     #   ####   #    #

UBUNTU >

Monday, 21 January 2013

The UNIX pwd Command

With the pwd command, you can see the name of the directory you are currently using: 

[andrew@localhost ~]$ cd /home/andrew
[andrew@localhost ~]$ pwd
/home/andrew
[andrew@localhost ~]$

Sunday, 20 January 2013

chown

You can change the owner of a file with the chown command. Here is a file:

UBUNTU > ls -l
total 0
-rw-rw-r-- 1 andrew users 0 Aug 22 22:09 file1
UBUNTU >

The file is owned by andrew. If you want to change the owner to fred, you can do so as follows:

UBUNTU > sudo chown fred file1
UBUNTU > ls -l
total 0
-rw-rw-r-- 1 fred users 0 Aug 22 22:09 file1
UBUNTU >

Thursday, 17 January 2013

Memory Size on a Linux Machine

You can see how much memory there is on a Linux machine with the following command. Then you need to look at the value (131047424) in the column called total and the line called Mem:

Linux > cat /proc/meminfo
        total:    used:    free:  shared: buffers:  cached:
Mem:  131047424 81170432 49876992 23453696 23302144 33415168
Swap: 139788288  8556544 131231744
MemTotal:    127976 kB
MemFree:      48708 kB
MemShared:    22904 kB
Buffers:      22756 kB
Cached:       32632 kB
BigTotal:         0 kB
BigFree:          0 kB
SwapTotal:   136512 kB
SwapFree:    128156 kB
Linux >

You can see the same details (in bytes) with free -b. If you want to have the answer in kilobytes, which is the default option, you can do it with free -k. If you want to have the result in megabytes, you can do it with free -m:

Linux > free -b
             total       used       free     shared    buffers     cached
Mem:     131047424   81235968   49811456   23449600   23302144   33480704
-/+ buffers/cache:   24453120  106594304
Swap:    139788288    8556544  131231744
Linux > free -k
             total       used       free     shared    buffers     cached
Mem:        127976      79332      48644      22900      22756      32696
-/+ buffers/cache:      23880     104096
Swap:       136512       8356     128156
Linux > free
             total       used       free     shared    buffers     cached
Mem:        127976      79332      48644      22896      22756      32696
-/+ buffers/cache:      23880     104096
Swap:       136512       8356     128156
Linux > free -m
             total       used       free     shared    buffers     cached
Mem:           124         77         47         22         22         31
-/+ buffers/cache:         23        101
Swap:          133          8        125
Linux >

Tuesday, 15 January 2013

How to Create a File of a Particular Size in UNIX or Linux

I needed a file with a size of 512 bytes for an Oracle test. I created it as follows:

UBUNTU > dd if=/dev/zero of=test1 bs=1 count=512
512+0 records in
512+0 records out
512 bytes (512 B) copied, 0.0140867 s, 36.3 kB/s
UBUNTU > ls -l test1
-rw-rw-r-- 1 andrew andrew 512 Aug 11 22:34 test1
UBUNTU >

To create a file with a size of 1000 bytes, you only have to change count=512 to count=1000:

UBUNTU > dd if=/dev/zero of=test2 bs=1 count=1000
1000+0 records in
1000+0 records out
1000 bytes (1.0 kB) copied, 0.0174102 s, 57.4 kB/s
UBUNTU > ls -l test2
-rw-rw-r-- 1 andrew andrew 1000 Aug 11 22:35 test2
UBUNTU >

Saturday, 12 January 2013

Examples Using wc

Here is a file:

Linux > cat file1
Increase productivity across your entire Oracle stack
with access to unique on-line tools and support
communities - all part of your Oracle Premier Support
service.
Use Oracle proactive support to reduce risks and
lower your organisations costs through preventive
maintenance. Benefit from health services, intelligent
fault management and timely, personalized advice,
backed by Oracle support experts.
Linux >

You can count the number of lines, words and characters in the file like this:


Linux > wc file1
      9      56     403 file1
Linux >

If you only need the number of lines, here is what you must do:


Linux > wc -l file1
      9 file1
Linux >

The following example counts the words in the file:


Linux > wc -w file1
     56 file1
Linux >

With wc -c you can count the number of characters in the file:

Linux > wc -c file1
    403 file1
Linux >

You can find the length of the longest line like this:


Linux > wc -L file1
     54 file1
Linux >

Here is another file:

Linux > cat file2
The 2012 threat landscape is one of constant and rapid
change.
Where new website vulnerabilities are tirelessly sought
and exploited.
Where new social engineering techniques can increasingly
circumvent external security measures and infect from
within.
And where hackers now employ multiple attack
methodologies and vectors to penetrate defences and
steal information.
Linux >

You can give more than one filename as follows:

Linux > wc -lc file1 file2
      9     403 file1
     10     369 file2
     19     772 total
Linux >

In the next example, cat is used to see the result without the filename:

Linux > cat file1|wc -L
     54
Linux >

If you give the name of a file which does not exist, wc replies with an appropriate error message:


Linux > wc file3
wc: file3: No such file or directory
Linux >