26 September 2013

Bash in 5 minutes


Every time you login, /etc/profile script is run first to initialize.
Then these files are run
    .profile
    .bash_login
    .bash_profile
these files are best to configure PATH variables.

when you logout, .bash_logout script is run.

#refers to home - /home/indu/
~/

#refers to current folder
./

#command to check the PATH variables
echo $PATH

#changing permissions

#chmod refer - http://ss64.com/bash/chmod.html
 
# change to rwxr-xr-x
chmod 755 file-or-folder

# all(a) get added(+) permissions rw to file
chmod a+rw file-or-folder

# others(o) get removed(-) permissions rw to file
chmod o-rw file-or-folder

# owner(u) get added(+) permissions x to file
chmod u+x file-or-folder

# change ownership
sudo chown username:group file-or-folder

#! - specifies a shell
#!/bin/bash

# Ubuntu version
lsb_release -r

--------------------------------------------------------
bash commands
--------------------------------------------------------
ps            //lists what shell prompt you are using
cd           //goes to home
cd ~/folder-at-home       //this ~/ refers to home location

pwd        //shows which directory you are working in

mv f1 f2        //moves file f1 to file f2 - can be used to rename file
cp f1 f2        //copies file f1 to file f2.  If f2 exists, then it is overwritten without warning
cp -i f1 f2        //asks before copying and overwriting f2

ls            //lists files in a directory

ls -lt        // order the list by creation date
ls -ltr       // order the list by creation date in reverse order
ls -l            //lists files in more detail
ls -a            //lists hidden files that start with '.'
ls -F        //lets you identify directories with a slash

cat text-file-name    //outputs contents of the file to the terminal
cat file1 file2     //outputs contents of file1 and then prints out file2

tail -f file-name   //outputs the logs that are getting generated, live

less text-file-name    //outputs contents page wise - use :q to quit to terminal
more text-file-name    //outputs contents page wise and quits to terminal just like cat

rm            //removes file
rm -r            //removes a directory along with the files in it
rm -i            //asks before deleting a file
hostname        //displays the name of the system you are working on



//searches recursively in /directory/ for 
//file or folder named item
$find /directory/ -name item -print 
 

//searches and prints all lines that contain string in filename
grep 'string' filename   
 

// searches for specified text in 
// all files in the current directory
grep -lir "some text" *
grep -lir "some text" /this/folder/location  // searches for specified text in the specified directory location
    -l - files that match
    -i - ignore case
    -r - read all files under each directory recursively
head filename        //displays first 10 lines of text in file
head -3 filename    //displays first 3 lines of text in file
tail filename        //displays last 10 lines of text in file
tail -6 filename    //displays last 6 lines of text in file
sort            //displays contents of file with lines sorted according to alphabetical order

:q            //quits the man page and goes to terminal

 |            //pipe - output of one process will become the input to another process
echo            //send messages from scipts to screen, and more
whereis    java        //lets you know where all java executable is installed
which java        //locates utility copy that you are going to use

bzip2 file.txt        //binary-sorting file compressor
bunzup2 file.txt.bz2    //uncompress

gzip file.txt        //GNU zip compressor
gunzip file.txt.gz    //GNU uncompressor

#tar             //tape archive
tar -czvf allmyfiles.tar.gz *    //gunzips all the files in the current folder

tar -czvf folder.tar.gz foldername/      //gunzips a folder
tar -zxvf allmyfiles.tar.gz    //this will extract the zip file in the current folder
tar -ztvf file.tar.gz         //list files that is zipped in this tar file

clear            //clears the screen
fg            //brings the background process to foreground


#redirecting standard output
command options > destination_file    //output of command will be put in destination_file, by erasing the previous contents.
command options >> destination_file    //output of command added to the end of destination_file.

#redirecting standard input
command options < filename    //command's input to come from file with the provided options

#pipes
command options | command options     // output of one command is input to another command
#same as
command options > tempfile
command options < tempfile

jobs            //lists the current running jobs with process ids
kill %process_id    //kills the process with the process

diff --brief --recursive folder1 folder2 > difference.txt  // gets the difference between two folders

--------------------------------------------------------
chmod - chaning file permissions
--------------------------------------------------------
File permissions read(r), write(w) and execute(x) have numbers.
r = 4
w = 2
x = 1

0 - ---
1 - --x
2 - -w-
3 - -wx
4 - r--
5 - r-x
6 - rw-
7 - rwx

chmod 644 filename // user = -rw-, usergroup = -r--, all = -r--
chmod 777 filename // user = -rwx, usergroup = -rwx, all = -rwx

--------------------------------------------------------
short-cut keys
--------------------------------------------------------
ctrl+U        //clears your typed matter at the terminal
ctrl+Z        //kills the current running program
\q        //escapes current view
Ctrl+D        //quit or exit