Posts Tagged ‘linux’

Color in prompt command line

In your .bashrc file there’s a line :
usernameColor=”\e[34;1m”
Over there 34 means blue. Other colors:
34 : blue
31 : red

How to hide system file in Krusader?

Ctrl + . to toggle show/hide system files or hidden files.

How to make .bashrc take effect immediately?

Call source .bashrc

Unix tips 2 (from wikipal)

Edit a simple text file

Without windows, you can use:

vi myFile.txt

With X-Windows you can use:

gedit myFile.txt

Editing Software

There are several possible editor to use:

* vim: is a simple editor that can be interesting because it does not use any windowing system. The difficulty is to learn the commands because there is no menu, of course. If you learn some command it is in fact a powerful editor with syntax highlighting.

* gedit: is a simple editor from gnome but it can recognize the syntax of most languages.

* kedit: is the editor of the KDE environment. It is less interesting than gedit for software because ther is no syntax highlighting.

You can also use integrated environment:

  • kate: seems a nice environment
  • kdevelop: the environment from KDE
  • eclipse: initially developed for Java. Now work with C and C++. Seems well integrated and advanced.

UML diagrams can also be created and edited:

  • umbrello: seems to be a nice software for UML diagram edition.

Cut and Paste under X-Windows

Selecting is equivalent to copy. To paste use the middle button.

Login to a Unix machine enabling X-Windows

The usual command for connecting to a unix machine is ssh. Example:

ssh ipal-server2

If you need to login in with a different default name just put it before the machine name. Example:

ssh user@ipal-server2

If you need a graphical environment for your remote program, you need an X-Windows server to be up and runing. It depends on the system you are using:

  • Unix: an X server is up by defaut. Nothing special needed.
  • MacOs: an X server is available but must be manually lauched (X11).
  • Windows: no X server are available by default. One must download the software X-WIN32 from core web site.

Once your X windows server is up and running you can add the ”-X” option to the ssh login command so to have access to windowed software. Be careful: display are thought the network and hence are slower than on the server screen. Example:

ssh -X ipal-server2

Then you can lauche a gnome program like:

gnome-system-monitor&

NB: the character ”&” is used to put this program on the background of your shell.

Copy between two Unix machines

The command to use is normally:

scp

This means “secure copy”. For copying directories uses the option ”-r”. See manual for more details. Important: one must have the right to read the source and to write the destination. Example: copy the whole SHARE directory from one machine to location /home/ipal on ipal-server2 machine, under the user name “chevalet”.

scp -r SHARE chevalet@ipal-server2:/home/ipal

WARNING: progress meter is display for each file and can slowdown the transfer of a lot of small files.

An automatic backup

Backing up a server is mandatory for a safety usage. Nowadays as disks are not expensive, using external disk is a simple backup solution. This external disk should have the exact size of the disk to be backup.

In a Unix system on need two command:

  1. an incremental copy command to save the changed files.
  2. an daemon that execute the backup automatically.

The command to use for the incremental copy is “rsync”. The deamon that execute any script is called “cron”.

This is a simple example of script that do a backup. This script should be put in an accessible location like ”/root/bin”.

#/bin/sh

# This is the path where the backup disk should be mounted
dest=/media/usbdisk1
# email adress to send in case of error
email="chevalet"
# Check if the disk is mounted
mounted=`df -a|grep $dest`
if [ "$mounted" ]
then
# Do the backup
/usr/bin/rsync -a --delete /home/ipal/ $dest/ipal/
else
# Disk not mounted: send an email
now=`date`
/bin/mail -s "Cannot do backup" $email <<-EOF Message from $0
The disk $dest is not mounted in $HOSTNAME.
Backup not done at $now.
EOF
fi

Then to activate this script, use the command:

crontab -e

Then one can add the automatic launch of the script every day at 1AM using the line below, providing the script is named “backup.sh” and is located at ”/root/bin”.

00 01 * * * /root/bin/backup.sh>>/var/log/backup.log

The output is automatically added to the log file backup.log.

Change the system hostname

The hostname is managed by the network administrator. It enable to automatically associate the name of the machine to an IP address and alleviates the necessity to remember the IP number. On the machine the hostname has to be changed manually by modifiying the file /etc/hosts.

See also : http://www.cpqlinux.com/hostname.html

Unix tips (contains duplicated)

A right terminal console when launching SSH

When run SSH, the terminal they give us may sick us due to the ugly interface. To have a proper one, as if run directly on the Unix server, is gnome-terminal.

Some helpful GUI application

Run a console and call the following commands :

  • A GUI-explorer : nautilus
  • Text editor with syntax highlight: kate
  • File manager – a very powerful & useful tool to work with your files: krusader
  • A great IDE application: eclipse

If you want to continue on your current terminal-console, you should run the application in background by adding the & at the end of the program name. E.g.:

vgnam@/home/vgnam> krusader&

“.bashrc” tips

The followings format the prompt of the terminal to the form of username@workingFolder and also colorize it.

usernameColor="\e[34;1m"
pathColor="\e[34m"
defaultColor="\e[0m"
export PS1="\[$usernameColor\]\u\[$default$pathColor\] \w $ \[$defaultColor\]"

Colorful ls command

alias ls='ls --color'

Note: The above scripts should be put in the .bashrc file in your home folder, e.g. /home/vgnam/.bashrc

Search and replace in bash command.

google:bash perl replace -> One-line shell script for find and replace

Using perl-like command:

perl -pi -e 's/find/replace/g' *.txt

Used with multiple files:

find . -name '*.txt' |xargs perl -pi -e 's/find/replace/g'

Another way using sed Use the sed command with inline option and regular-expression as :

sed -i -e 's/source/destination/g' *.html

In program //kate//, how to execute the current open file?

Simply press Ctrl-Shift-X

Install new font in Unix system?

Firts, extract and copy fonts to:

/usr/share/fonts/yourFontsFolder

Login as root user. Then, continue to use the command:

fc-cache /usr/share/fonts/yourFontsFolder

Print elapsed time from bash command ?

time1=`date +%s`
# your program that need to measure time runs here
time2=`date +%s`
elapsed=`echo $time2-$time1 | bc`
echo "Elapsed time: $elapsed seconds."

Delete recursive files, e.g. the “name.ext~” – How to?

find . -type f -name "yourPatter" -delete

How to get numbers of lines of a huge file? Call the wc command with -l option :

wc -l

How to get memory (RAM) size in Linux OS system by command line?

I need to know the memory size, RAM size, of the server which is a Linux OS. I look on the web and found this quite not easy since the input keyword must be precise.

cat /proc/meminfo

There u can see the line that says: MemFree : ### kB (or mB) is where the free memory is showed. For a brief information output let’s used :

cat /proc/meminfo | grep MemFree

This will show only the MemFree line! What a command!

My precious .bashrc

#The followings set the prompt to show username workingFolder with color
usernameColor=”\e[34;1m”
pathColor=”\e[34m”
default=”\e[0m”
export PS1=”\[$usernameColor\]\u\[$default$pathColor\] \w $ \[$default\]”

#XIOTA project
export XIOTAPath=”/home/vgnam/Desktop/XIOTA”
export PATH=”.:$XIOTAPath/bin:$PATH”

#variables used for CLEF experiments
export ORG_IAPR07=”/home/ipal/SHARE/DATA/COLLECTIONS/CLEF-2007/imageclef07/IAPR_TC-12_Benchmark_2007″
export SHARE_CLEF08=”/home/ipal/SHARE/DATA/RESULTS/2008/CLEF”
export EXPE_CLEF08=”/home/ipal/EXPE/2008/CLEF”

#Subversion tool
export SVN_EDITOR=kate

#colorful ls command
alias ls=’ls –color’

Unix Linux OS using tips – logged questions/problems of mine when working with it

Check if null value?
Check if null

if [ -z $variable ]; then
echo “Null”
else
echo “Not null”
fi

Search and replace in bash command?
google: unix bash search and replace ->One-line shell script for find and replace [unix] [bash] [perl] [shell]

perl -pi -e 's/find/replace/g' *.txt 


How to know the file system’s block-size?
google: file system + minimum block size -> obtain filesystem block size
Block-size = the minimum size of storage unit used to store file content.
Call the command :

/sbin/dumpe2fs /dev/ | grep “Block size”
where could be :

  • hda, hda1, hda2, …
  • sda, sda1, sda2, ….

In “kate”, how to execute the current open file?
Simply press Ctrl-Shift-X

If command – condition command ?
google: unix bash command condition -> http://www.panix.com/~elflord/unix/bash-tute.html

Syntax : (noted exact space must be absolutely respected )

if [ string1 = string2 ]; then
command1
command2
fi


if [ -eq string1 string2 ]; then
command1
command2
else
command3
command4

fi

Check if defined or not

X=""
if [ -n "$X" ]; then # -n tests to see if the argument is non empty
echo "the variable X is not the empty string"
fi

Sort a file numerically and remove duplicated values?
google: unix command remove duplicated value -> Unix Toolbox

Sort numerically:

sort -n file2sort > output

Remove duplicated lines:

uniq file2RemoveDup > output

Install new font in Unix system?
google: linux install new font -> Installing new fonts – The UNIX Forums

Firts, extract and copy fonts to:

/usr/share/fonts/directory_you_put_fonts

Login as root user.
Then, continue to use the command:

fc-cache /usr/share/fonts/local/directory_you_put_fonts

Print elapsed time from bash command ?
google: bash command elapsed time -> Using bc in bash script

time1=`date +%s`
# your program that need to measure time runs here
time2=`date +%s`
elapsed=`echo $time2-$time1 | bc`
echo “Elapsed time: $elapsed seconds.”


Delete recursive files – How to?

find . -type f -name “yourPatter” -delete
source: http://en.tuxero.com/2007/09/how-to-delete-useless-windows-files-in.html

How to call explorer GUI in Linux?

Run a console a call the command nautilus.


How to remote copy files/folder between Linux computers?

google: linux + remote copy -> http://linux.about.com/b/2007/10/08/rcp-scp-ftp-commands-for-copying-files-between-computers.htm

Use scp: (more simple)
scp file2Copy username@hostIP:targetPath
after calling the above command, u’ll be asked for username/password.
scp -r folder2Copy username@hostIP:targetPath

Use rcp:
There it said u need to have .rhosts file in home folder that have lines where each line is :
(lost information here -> empty! Sorry!)

Then, call rcp to copy files as sample :
rcp myLocalFile hostname:targetFile


How to change shell-prompt ‘s color?

google: lprocess + view memory used + linux -> Process memory usage – LinuxQuestions.org

ps -AH v


How to change shell-prompt ‘s color?

google: linux + command line -> some begginer site to input keyword-space to my knowledge -> shell prompt
google: linux + shell prompt + color -> Tip: Prompt magic

Prompt basics

Under bash, you can set your prompt by changing the value of the PS1 environment variable, as follows:

$ export PS1="> "
>

Changes take effect immediately, and can be made permanent by placing the “export” definition in your ~/.bashrc file. PS1 can contain any amount of plain text that you’d like:

$ export PS1="This is my super prompt > "
This is my super prompt >

While this is, um, interesting, it’s not exactly useful to have a prompt that contains lots of static text. Most custom prompts contain information like the current username, working directory, or hostname. These tidbits of information can help you to navigate in your shell universe. For example, the following prompt will display your username and hostname:

$ export PS1="\u@\H > "
drobbins@freebox >

Sequence Description

\u Your username

\w Current working directory (such as “/home/drobbins”)

Colorization

Colors are selected by adding special sequences to PS1 — basically sandwiching numeric values between a “\e[” (escape open-bracket) and an “m”. If we specify more than one numeric code, we separate each code with a semicolon. Here’s an example color code:

"\e[0m"

When we specify a zero as a numeric code, it tells the terminal to reset foreground, background, and boldness settings to their default values. You’ll want to use this code at the end of your prompt, so that the text that you type in is not colorized. Now, let’s take a look at the color codes. Check out this screenshoot:
Color chart
Color chartaa


export PS1="\w> "

becomes:

export PS1="\e[32;40m\w> "

So far, so good, but it’s not perfect yet. After bash prints the working directory, we need to set the color back to normal with a “\e[0m” sequence:

export PS1="\e[32;40m\w> \e[0m"

This definition will give you a nice, green prompt, but we still need to add a few finishing touches. We don’t need to include the background color setting of 40, since that sets the background to black which is the default color anyway. Also, the green color is quite dim; we can fix this by adding a “1” color code, which enables brighter, bold text. In addition to this change, we need to surround all non-printing characters with special bash escape sequences, “\[” and “\]”. These sequences will tell bash that the enclosed characters don’t take up any space on the line, which will allow word-wrapping to continue to work properly. Without them, you’ll end up with a nice-looking prompt that will mess up the screen if you happen to type in a command that approaches the extreme right of the terminal. Here’s our final prompt:

export PS1="\[\e[32;1m\]\w> \[\e[0m\]"

Don’t be afraid to use several colors in the same prompt, like so:

export PS1="\[\e[36;1m\]\u@\[\e[32;1m\]\H> \[\e[0m\]"
Bash allows these prompt strings  to  be  customized by inserting a number of backslash-escaped special
characters that are decoded as follows:
\a an ASCII bell character (07)
\d the date in "Weekday Month Date" format
(e.g., "Tue May 26")
\e an ASCII escape character (033)
\h the hostname up to the first `.'
\H the hostname
\j the number of jobs currently managed by the
shell
\l the basename of the shell's terminal device
name
\n newline
\r carriage return
\s the name of the shell, the basename of $0
(the portion following the final slash)
\t the current time in 24-hour HH:MM:SS format
\T the current time in 12-hour HH:MM:SS format
\@ the current time in 12-hour am/pm format
\u the username of the current user
\v the version of bash (e.g., 2.00)
\V the release of bash, version + patchlevel
(e.g., 2.00.0)
\w the current working directory
\W the basename of the current working directory
\! the history number of this command
\# the command number of this command
\$ if the effective UID is 0, a #, otherwise a
$
\nnn the character corresponding to the octal
number nnn
\\ a backslash
\[ begin a sequence of non-printing characters,
which could be used to embed a terminal control sequence into the prompt
\] end a sequence of non-printing characters

To have this prompt permanently, put the command : export PS1=”blabla” in .bashrc file (located in your home folder)
My extracted script:

#The followings set the prompt to show username workingFolder with color
boldGreen=”\e[32;1m”
green=”\e[32m”
default=”\e[0m”
export PS1=”\[$boldGreen\]\u\[$default$green\] \w $ \[$default\]”

Enjoy!


How to search & replace on multiple files on Linux OS ?

google:Linux + search and replace multiple files -> Search and replace over file(s) with Perl [linux] [perl] [replace] [search]

They use sed command with inline option and using expression (regular expr.) as :
sed
i e s/source/destination/g *.html

More detail on sed?

google:Linux + sed command -> sed – Linux Command – Unix Command

How to rename multiple files on Linux OS ?

Source: google: linux rename multiple files -> Howto Linux rename multiple files at a shell prompt

Syntax :

rename oldText newText *.files

Eg:
For example rename all *.bak file as *.txt, enter:

$ rename .bak .txt *.bak

Remove all blank space with rename command:
$ rename "s/ *//g" *.mp3

To remove .jpg file extension, you write command as follows:

$ rename ‘s/\.jpg$//’ *.jpg

To convert all uppercase filenames to lowercase:
$ rename 'y/A-Z/a-z/' *


How to get numbers of lines of a huge file?

Source: google: linux shell command + “concatenate string” -> concatenate strings – Linux Forums
There, u can see that we can easily concatenate string by putting variables/strings next to each other.

How to get numbers of lines of a huge file?

Source: my professor Jean Pierre Chevallet -> wc -l

Call the wc command with -l option : wc -l

How to get memory (RAM) size in Linux OS system by command line?

I need to know the memory size, RAM size, of the server which is a Linux OS. I look on the web and found this quite not easy since the input keyword must be precise.

cat /proc/meminfo
There u can see the line that says: MemFree : ### kB (or mB) is where the free memory is showed.

For a brief information output let’s used :

cat /proc/meminfo | grep MemFree
This will show only the MemFree line! What a command!

Next, we can output the result of free memory size :

  • to a file by cat /proc/meminfo | grep MemFree > outputFile
  • to output it into a Perl varible see it here.
Enjoy!


How to start a program, e.g Kate, in background ?

Source: google:unix + background + start -> Working With the Unix Shell

Just put an & after ur command will make the ‘kate’ run background.

What is the useful programs used on Linux ?

I myself use :

  • Kate as the code/text(e.g Perl) editor
  • and Krusader as the files browser.
  • More over, I used the SSH Secure Shell from www.ssh.com to transfer file between Windows XP and Linux and reversely.