Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Thursday, 28 September 2017

Bash commands for Navigation and File Management

Current working directory - pwd

To find out where your home directory is in relationship to the rest of the filesystem, you can use the pwd command. This command displays the directory that we are currently in.
$ pwd

List information about the files - ls

To display the directory that you are in, you use "ls" command.
$ ls
For instance, to list all of the contents in an extended form, we can use the -lflag (for "long" output):
$ ls -l

Change the working directory - cd

Begin by going back to the mydirectory directory by typing this:
$ cd mydirectory
You can use absolute or relative path.

Viewing a file - cat 

Use the "cat" command for read the contents of a file. This command concatenates one or more files to standard output. Example: 
$ cat myfile.txt

Create a file - touch

The "touch" command creates a file in your filesystem. Example:
$ touch myfile.txt

Create a directory - mkdir

The "mkdir" command creates a new directory in your filesystem. Example:
$ mkdir mydirectory
To tell mkdir that it should create any directories necessary to construct a given directory path, you can use the -p option
$ mkdir -p dir1/dir2/mydirectory

Moving and Renaming Files and Directories - mv

You can move a file to a new location using the mv command. For example, you can move myfile into the dir1 directory by typing:
$ mv myfile dir1
So to rename the dir1 directory to directory1:
$ mv dir1 directory1

Copy files and directories - cp

The cp command can make a new copy of an existing file or directory. For example, you can copy myfile.txt into same directory but with a different name (myfile2.txt):
$ cp myfile.txt myfile2.txt
Instead if you can copy a directory, use "-r" option:
$ cp -r mydir new_mydir

Remove a file - rm

The rm command remove a file. For example, you can remove myfile2.txt with the following command:
$ rm myfile2.txt

Remove a directory - rmdir

The rmdir command remove a directory. For example, you can remove new_mydir with the following command:
$ rmdir new_mydir
Alternatively you can use the following command:
$ rm -r new_mydir

Remove all files (recursively)

You can remove recursively all files with a precise name (or regular expression) from a specific path. Example: you can remove all .svn files from a your workspace, so you can execute the following command into workspace directory:

$ find . -name .svn -exec rm -rf {} \;

Find all snapshot version in a maven project
$ find . -name pom.xml | xargs grep "SNAPSHOT"

Disk Usage - du

Disk Usage - report the amount of disk space used by the specified files and for each subdirectory
$ du -h

Internal Links

May be of interest to you:

Thursday, 24 August 2017

How to compress and decompress files in bash

There are available more commands to compress and decompress file in bash. Some of them are: 
  • tar 
  • zip / unzip
  • rar / unrar
  • gzip
  • bzip2

Tar and untar 

To create a tar.gz from bash shell you can use the following command:
$ tar -zcvf myarchive.tar.gz mydirectory
Instead to extract the files from an archive:
$ tar -xvf myarchive.tar.gz

Zip and unzip 

Create an archive form a text file:
$ zip myarchive.zip myfile.txt
Create an archive form a directory:
$ zip -r myarchive.zip mydirectory
Create an archive with the subdirectories and files into a directory (without it):
$ zip -r -D myarchive.zip mydirectory
Extraction of files and directories from an archive:
$ unzip myarchive.zip
Extraction and overwrite the files and directories from an archive:
$ unzip -o myarchive.zip
Extraction the files and directories from an archive to a target directory
$ unzip myarchive.zip -d mydirectory

Rar and unrar

Create a new rar archive myarchive.rar containing myfile.txt
$ rar a myarchive.rar myfile.txt
Create a new rar archive myarchive.rar containing a directory
$ rar a myarchive.rar mydirectory/
Create a new rar archive that splits the file/files into multiple parts of equal size (10 MB)
$ rar a -v10M -R myarchive.rar mydirectory/
Extraction the files and directories from an archive:
$ unrar e myarchive.rar
List the content of a rar file without uncompressing it
$ unrar l myarchive.rar

Gzip 

The command `gzip' is designed as a complement to `tar', not as a replacement. Instead the command`gunzip' can currently decompress files created by `gzip', `zip', `compress' or `pack'. The detection of the input format is automatic. 
Create an archive form a text file:
$ gzip myfile.txt
Decompress the archive created in previous step:
$ gzip myfile.gz

Bzip2

Bzip2 compresses files using the Burrows-Wheeler block sorting text compression algorithm, and Huffman coding. The command-line options are deliberately very similar to those of GNU gzip, but they are not identical. 
Create an archive form a text file:
$ bzip2 myfile.txt
Decompress the archive created in previous step:
$ bunzip2 myfile.txt.bz2

Monday, 21 August 2017

Bash scripting - examples of constructions (if else, for loop, while loop, until loop)

IF construction

The if construction is one of bash's bases and its syntax is:
 if TEST-COMMANDS; then 
     CONSEQUENT-COMMANDS; 
 else
     CONSEQUENT-ELSE-COMMANDS; 
 fi
else block is optional.

Examples:

Example 1: String comparisons
var="..."
if  [ ${var} = "text" ] 
then
   echo "true";
else
   echo "false";
fi
Example 2: String comparisons with regular expression
var="..."
if  [[ ${var} == tex* ]] 
then
   echo "true";
else
   echo "false";
fi
Example 3: Numeric comparisons
var="..."
if  [ ${var} -gt "10" ] 
then
   echo "....";
fi
Example 4: Checking files
if  [ -f file.txt ]
then
   echo "file.txt exists";
fi

For loop construction

The for loop construction is one of bash's bases and its syntax is:
 for NAME [in LIST ]; do
     COMMANDS; 
 done

Examples:

Example 1: Using command substitution for specifying LIST items:
for i in `cat list.xml`; do 
    cp "$i" "$i".txt ; 
done
Example 2: Using the content of a variable to specify LIST items:
LIST="$(ls *.xml)"
for i in "$LIST"; do
     cp "$i" "$i".txt ; 
done

While loop construction

The while construct allows for repetitive execution of a list of commands, as long as the command controlling the while loop executes successfully (exit status of zero). 
Its syntax is:
 while CONTROL-COMMAND; do
     COMMANDS; 
 done

Examples:

Example 1: Simple example using while.
 while test $index -lt $number; do
     index=$[$index+1];
 done
Example 2: infinite while loop, implement a timer. You can use "CTRL" + "z" to stop the script.
 while true; do
        date +"%T";
        sleep 1;
        clear;
 done

Until loop construction

The until loop is very similar to the while loop, except that the loop executes until the TEST-COMMAND executes successfully. As long as this command fails, the loop continues. The syntax is the same as for the while loop:
 until TEST-COMMAND; do
     COMMANDS; 
 done

Example:

Create a simple script for disk space monitor on your home:
while true; do 
    DISKSPACE=$(df -h $WEBDIR | grep -v File | awk '{print $5 }' | cut -d "%" -f1 -)
    
    until [ $DISKSPACE -ge "90" ]; do 
        alert "disk space 90%";
    done
    
done 

Internal Links

May be of interest to you:

Friday, 21 July 2017

Bash commands about Hardaware and Network

Bash is a Unix shell and command language written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell. Bash is a command processor that typically runs in a text window, where the user types commands that cause actions. Bash can also read and execute commands from a file, called a script.

Bash commands about Hardware information

Like for every thing, there are plenty of commands to check information about the hardware of your linux system. The following commands are just a subset of it.

lscpu

The lscpu command reports information about the cpu and processing units.
$ lscpu

lshw

The lshw command reports detailed and brief information about multiple different hardware units such as cpu, memory, disk, usb controllers, network adapters etc...
$ lshw
With -short as option you can display a short report.
$ lshw -short

hwinfo

The hwinfo command is another general purpose hardware probing utility that can report information about multiple different hardware components.
$ hwinfo
With --short as option you can display a short report.
$ hwinfo --short

lsusb

The lsusb command shows the USB controllers and details about devices connected to them.
$ lsusb
Use the verbose option (-v) to print detailed information about each usb port 
$ lsusb -v

lsblk

The lsblk command shows the list out information all block devices, which are the hard drive partitions and other storage devices like optical drives and flash drives.
$ lsblk

df

The df command show a report of various partitions, their mount points and the used and available space on each.
$ df
I suggest to use -h (--human-readable) as option for a better report structure.
$ df -h

fdisk

The fdisk command is a utility to modify partitions on hard drives, and can be used to list out the partition information as well. Use -l as option to show the list. This command required "sudo".
$ sudo fdisk -l

mount

The mount command is used to mount/unmount and view mounted file systems. To view the result in table use the following command:
$ mount | column -t

free (Check RAM)

The command free shows the amount of used, free and total amount of RAM on system.
$ free
Use the options -m or -g to print the results in mega or giga.
$ free -m
$ free -g
If you want see the size of physical RAM allocated your system, so use this command:
$ grep MemTotal /proc/meminfo | awk '{print $2}'

Bash commands about Network

The network commands paragraph collects various tools which can be useful when networking with other computers both within the network and accross the internet, obtaining more information about other computers.

netstat

The netstat command displays contents of /proc/net files. It works with the Linux Network Subsystem, it will tell you what the status of ports are ie. open, closed, waiting, masquerade connections. It will also display various other things. It has many different options. For example you want check the status of  port '8080'. 
$ netstat -an | grep 8080

tcpkill

The tcpkill command kills a connection on a specific port. Example: if you want kill the connection with the port 8080 use the following command:
$ tcpkill -i eth0 port 8080

tcpdump

The tcpdump command is a sniffer, a program that captures packets off a network interface and interprets them for you. It understands all basic internet protocols, and can be used to save entire packets for later inspection. This command required "sudo".
$ sudo tcpdump

ping

The ping command sends echo requests to the host you specify on the command line, and lists the responses received their round trip time.
$ ping myhostname

hostname

The hostname command tells the user the host name of the computer they are logged into.
$ hostname

traceroute

The traceroute command shows the route of a packet. It attempts to list the series of hosts through which your packets travel on their way to a given destination. Also have a look at xtraceroute (one of several graphical equivalents of this program).
$ traceroute <ip>

ifconfig

The ifconfig command is used to configure network interfaces, or to display their current configuration. Use only the command withoud parameters to show the configuration.
$ ifconfig

route

The route command is the tool used to display or modify the routing table. To add a gateway as the default you would type:

$ route add default gw some_server

Internal Links

May be of interest to you:

External Links:

Welcome

Hello everybody, Welcome in my blog called "Information technology archive". Obviously the topics will be related to Informatio...