Skip to main content

Posts

Showing posts with the label UNIX

UNIX : How to get size of file

To get the  size of the file in human readable form try the following commands. > du -sh * This will give the disk usage of the file in the current directory. Please note the argument 'h' stands for human readable form. Alternatively you can see the size of the file with the list directory command with 'h' attribute > ls -lh This will list the file with file size in human readable form along with other arguments like file owner,time stamp etc

UNIX : How to create many files quickly

Its easy to create many file quickly with touch command >touch file{1..100}.txt The touch command is used to create file or change last access timestamp of the existing file to current time of the day.In the above example 100 files with .txt will be created in current directory with 0 bytes.You can change the value of 100 to any number you like. Another variations >touch {1.txt,2.txt,3.txt} It will create 3 files as specified >touch {A..Z}.txt It will create files with name as alphabets from A to Z >touch {A..Z}_{1..10}.txt It will create files with name as alphabets from A to Z suffix with 1 to 10

UNIX : PATH variable

All the UNIX commands are defined in some location.Either the absolute path followed by arguments has to be entered in command line or the path can be set in the variable PATH. >echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/jvm/java-8-oracle/bin

UNIX : Using IF in bash

The IF statement is used to test the given condition and based on the truth value of that the expression the statement followed is executed.Otherwise the else part is executed. The basic structure of if is as follows. If [[ expression ]];then      expression elif [[expression ]];then      expression else      expression 1) Integer comparison    2) String comparison    3) Using multiple conditions in IF statement    4) Compare regular expression in bash

UNIX : How to take backup of entire directory ?

Sometimes we run of of disk space.We try to clean up some unwanted files . But everything seems to be needed in future. In this case the entire directory can be compressed and bakup can be taken. Lets say we need to take backup of the location /home/user/mydir > tar cfvz backup.tar.gz /home/user/mydir This will make a tar file backup.tar.gz file containig all the files.

UNIX : View partitions of the filesystem

To see all the partition of the filesystem give the command >sudo fdisk -l Disk /dev/sda: 500.1 GB, 500107862016 bytes 255 heads, 63 sectors/track, 60801 cylinders, total 976773168 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 4096 bytes I/O size (minimum/optimal): 4096 bytes / 4096 bytes Disk identifier: 0x1767f5e2    Device Boot      Start         End      Blocks   Id  System /dev/sda1   *        2048      206847      102400    7  HPFS/NTFS/exFAT /dev/sda2          206848   209919999   104856576    7  HPFS/NTFS/exFAT /dev/sda3       209920000   405279374    97679687+  83  Linux /dev/sda4       405279375   976773167   285746896+   7  HPFS/NTFS/exFAT Partition 4 does not start on physical sector boundary. Disk /dev/mmcblk0: 7948 MB, 7948206080 bytes 81 heads, 10 sectors/track, 19165 cylinders, total 15523840 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal):

UNIX : Kill a process

How to kill a process in UNIX ? You need to get the process ID (PID) of the running job. With that process id use the command 'kill' to stop the running process. Suppose the name of the process is know the use the command >pidof <process> This can give the process ID . use the process id and give the command to stop the process. >kill <pid> 

UNIX : How to find number of blank lines in a file

There are many situation where we need to know whether a file conatins blank line. If the file is small we can manually open it and see . But if it is very large it will be easy to find it by wrtitng a small script . Here I am using awk to find the number of blank lines in a file. Let say the file abc.txt contains the following data. > cat abc.txt abc abc abc abc [EOF] Here there are 9 lines in the file out of which 5 line are blank lines.The below sctipt will find the number of blank line in this file. > cat abc.txt | awk '/^$/{ ++count } END { print "Number of blank lines is : " count}' Output  Number of blank lines is : 5

UNIX environment check

1. How to check the shell you are running? ps utility can show the current shell you are working with. >ps   PID TTY          TIME CMD  2549 pts/0    00:00:00 bash  2598 pts/0    00:00:00 ps 2. How to change to superuser / root privilege in unix? > sudo su - This command will ask for passoword and once it is give the prompt will also chnage to "#" . Be careful the with root privilege you can modify systems files. 3. How to change password in unix? Give the command passwd .It will ask for the current password and the new password. >passwd Changing password for user. (current) UNIX password: Enter new UNIX password: 4. How the check the environment you are working on ? > hostname This will display the environment on shell. 5. To display which all users have logged in . > who user  :0           2015-04-25 16:35 (:0) user   pts/0        2015-04-25 16:40 (:0) user   pts/16       2015-04-25 17:20 (:0) The first cloumn is s