Bash

(c) 2005 - Alexandre Brillant

Process And Stream | Useful Commands | Essential Commands | Scripts | .bashrc


Here some useful commands. We suppose you are using the bash shell, if not, simply type: bash.

Process And Stream :

On bash, the .bashrc file is used to configure your environnment.

Process Management :

Stream management :

It exists 3 main streams :

Here a standard way to use these streams :

A little Tip on the A command :

Useful Commands

Command Role Example
alias SHORTCUT=FULL COMMAND Build a shortcut name for a command. Use unalias for removing it. On bash, this command is often stored in the ~/.bashrc file. Note if you use alias without parameters, it shows all current alias. alias ll="ls -la"

ll

bc Enter a computing mode. User can insert mathematical expression. Use the -l option for supporting floating computing.

# Show the result of the computing

echo "2.4+3.6" | bc -l
cat FILE1 FILE2... Show file content. Use the v option for displaying all caracters. cat readme.txt
chown USER FILE Change the owner of a file or directory. File permission (read, write, execution) applies for this owner. Look at the rights section for more information.

# Make the user 'brillant' the owner of all the directory dir1

chown -R brillant dir1
clear Clear your consol screen. You can obtain the same result with the Control-l key.

# Clean the screen

clear
cp SOURCE DESTINATION Copy a file or a set of file to a destination. The -r option will include subdirectories.

# targetDirectory will include myDirectory.

cp -r myDirectory targetDirectory
df Show all available free space for all disk unit

# List all the free space and sort it by unit name

df | soft
echo STRING As you guess, print STRING on the console. \n for carriage return \t for horizontal tab. -n will disable a carriage return. You can include shell command. If you don't want to let the shell interpret the STRING, put it between quote caracter ('). echo hello `whoami` | banner | less
find PATH Find an element path. -name to specify which file. -print to show the result. -type for specifying the file type (d for directory or f for file). echo Search all .java file of the current path
find . -name *.java -type f -print
for ITEM in LIST Important command that repeats some actions. User must specify a LIST of items, for each one, the for will set the ITEM parameter and will call commands inside the for.

# Walk through the current directory and extract all file containing the 'AA' string

      for file in *; do
if ( grep 'AA' $file ); then
echo $file contains the AA string
fi
done
free Show the memory usage. free
grep PATTERN Useful command showing line matching the PATTERN in a stream or in a set of files. -i to ignore lowercase/uppercase, -c to count pattern matching # Search for a PATTERN 'AA' in all file of the current directory

# Note that find is used to get all file from the current directory, xargs converts the result

# into a list of parameter that grep use

find . -type f | xargs grep 'AA'
head Output x lines starting by the beginning for a file. The -n option can change the default line number (10). The contrary command is tail. .

# Show the 10 first lines of index.htm and inverse the result

head index.htm | tac
kill SIGNAL PID Kill send a signal to a process. The -9 option is a KILL signal (a process terminaison). The process PID is obtained by the ps command. Be careful, a PID is a random value, it will depend on a running context. # Extract all 'java process' and kill each one
 
for PID in $(ps); do
if ( echo $PID | grep 'java' ); then
toKill=$( echo $PID | cut -f 2 -d ' ' );
echo Kill the process $toKill
kill -9 $toKill
fi
done
less Bufferize a result for consultation like for the more command. Less is more practiced than more because you navigate as you want in the result. Use the '/' key for searcing a motif. Use less --help for shortcut.

cat longFile.txt | less

# You can do the same thing with less longFile.txt
ls List the current directory content. Use -la for looking at all file (starting by . for instance). ls -la ~/.bashrc
more The same thing than less without a buffer (meaning you can't navigate as you want). cat shortFile.txt | more
mount Mount a drive. Link a device to a directory. A device is a hard disk, a floppy disk... The contrary command is umount. If you use the single mount command, it show all available mount point. Witht the -t option, you can specify the type of the device (msdos, iso9660, ext, ext2...)

# mount the floppy disk and list the content

mount -t msdos /dev/fd0 /mnt/floppy && ls -la /mnt/floppy
ps List process. -A for all the processes. # Extract all java process ps -A | grep "java" | less
pwd Show the current path

# Convert current path to msdos format

  • pwd | tr '/' '\\'
  • sed The stream editor. Mainly interesting for searching and replacing text

    Search/Replace : s/regular expression/result/

    Delete line : /regular expression/d

    # Replacing the li expression by the u expression and the ux expression by the ix expression

    echo "linux" | sed s/^li/u/ | sed s/ux$/ix/
    sort Sort your stdin (standard input). -f to ignore lowercase/uppercase, -r to reverse result. ls | sort
    split Split a file in several files. -b to define output size file. Split is useful when you must send very large file by mail or by floppy disk. echo this example procudes xaa / xab and xac files
    echo "Linux is great" | split -b6
    tail

    Output x lines starting by the end of a file. The -n option can change the default line number (10). The contrary command is head.

    tail -n 15 index.htm
    top Show the current activities. press 'a' to quit.
    touch FILENAME Create an empty file FILENAME. There's several all tips for doing it like cp /dev/null > filename. touch index.htm
    tr set1 [set1] Translate characters of set1 to characters of set2. -d to delete any characters of set1 from stdin

    # Remove all '-' echo "linux- is- great" | tr -d '-'

    unzip ZIPFILE Unzip any ZIPFILE (.zip)

    # see zip section for the contrary operation

    unzip `whoami`.zip
    which APPLICATIONNAME Show a complete application path

    # where is the javac command ?

    which javac
    who Users information. -H for header

    # print a message only if root is connected

    ( who | grep 'root' ) && echo "root is here"
    whoami Current user name echo "hello `whoami`"
    tar Build (and compress) a path. Uses 'czvf' option for building your archive file (following by a path or a set of file to archive) or 'xzvf' for uncompressing your archive. Note that common extension for archive file is 'tar.gz'. Morever, the .tar.gz format maintains file attribute (read, write, execution).

    # Compressing aPath

    tar czvf archive.tar.gz aPath

    echo Uncompressing an archive

    tar xzvf archive.tar.gz

    xargs COMMAND Convert the input stream to a parameter for the COMMAND

    # Search content from the myPath directory

    echo "myPath" | xargs finds
    zip ZIPFILE DATAFILE Compress a file to the pkzip format (compatible the winzip utility). -r to recursive add file.

    # compress the user path

    zip -r $HOME/`whoami`.zip $HOME

    Essential Commands

    a. Find a MOTIF in all file from the current directory

    find . -type f | xargs grep MOTIF
    

    b. Search and replace a MOTIF in all file from the current directory

    # Be careful with such command
    # It creates a temporary file with the good motif and
    # replace the old file by this one.
    # It is really dangerous to use the same stream for reading and writing
    # That's why the temporary file is used

    for file in $(find . -type f); do
    sed 's/OLDMOTIF/NEWMOTIF/g' < $file > $file.tmp
    rm -f $file
    mv $file.tmp $file
    done

    c. Count the number of file containing a MOTIF from the current directory

    # Walk through the current directory and 
    # update the count variable each time the
    # MOTIF is found

    declare -i count=0;
    for file in $(find . -type f); do
    if ( grep MOTIF <$file >/dev/null 2>&1 ); then
    count=$((count+1));
    fi
    done

    echo $count files for MOTIF

    .bashrc :

    The .bashrc is a file that contains your configuration preference (for the bash shell only !). It is located at your personnal home directory.

    Here's a sample :

    # .bashrc
    # User specific aliases and functions
    alias rm='rm -i'
    alias mv='mv -i'
    alias cp='cp -i'
    alias s='cd ..'
    alias d='ls'
    alias p='cd -'
    alias ll='ls -la'
    # Source global definitions
    if [ -f /etc/bashrc ]; then
    ./etc/bashrc
    fi
    export EDITOR=emacsclient
    export CVS_RSH=ssh
    export PATH=~/bin:$PATH:.