Archive for the ‘Linux Command Line’ Category

Linux - Cut Lines

Saturday, January 3rd, 2009

Very useful when you pipe it, for example, for easily reading a file containing very long quries
cat queries.sql | cut -b -10

MySQL - Importing Many SQL Dump Files in a Database

Tuesday, December 16th, 2008

ls *.sql | awk ‘{printf(”mysql -u [username] -p[password] [databasename] < %s\n”, $1) | “/bin/sh” }’

Add An Entry In Fstab to Mount a Common Data Partition

Sunday, December 14th, 2008

Add this row into your /etc/fstab:
/dev/hda6 /mnt/data auto rw,uid=1000,utf8 0 0

Linux - How To Split a Big File Into Several Small Files

Wednesday, November 12th, 2008
split –bytes=1m /path/to/large/file /path/to/output/file/prefix

You can change the output file size by changing the –bytes=1m to your preference. You can use b, k, or m. b represent bytes, k represent kilobytes, m represent megabytes.

To restore the original file:

cat prefix* > newfilename

To split a file using a regular expression:
csplit filename separator ‘{*}’ (the last bit is to split not just once but as many times as possible)
To restore the original files:
cat $(ls -L) >> a

Rsync - Deploy And Increment Backup

Friday, November 7th, 2008

It’s good for deploy as well.
It can be used also to do backups locally.
Very useful to move big amount of data over the Internet because of its incremental feature.

Backing up from local machine to remove backup server (with the same user):

#!/bin/bash
export RSYNC_RSH=/usr/bin/ssh
dest=backup1
user=$(whoami)
cd || exit 1
rsync -aHPvz . "${user}@${dest}:."

[-z -> to compress, not necessary in LAN]
[-b -> backup of the already exisitng destination files]
SSH is useful just if you send data over the Internet.

Listing the files on the backup server:
#!/bin/bash
dest=server1
user=$(whoami)
cd || exit 1
rsync “${user}@${dest}:.” | more

Restoring (the script runs locally):
#!/bin/bash
dest=server1
user=$(whoami)
cd || exit 1
for file in “$@” ; do
rsync -aHPvz “${user}@${dest}:./${file}” “./${file}”
done

For restoring, you simply run the script, passing the names of the files to be restored as arguments on the command line.
We can also restore all the files at once by using a dot as the filename

Linux Command Line - Bash Scripting - Some tricks

Wednesday, November 5th, 2008

wc - print newline, word, and byte counts for each file (program for count statistics from a file)

dt = $(date)

ls -l > files.txt 2>error.txt
Redirects standard error to a file

ls -l > files.txt 2>&1
Redirects standard error and standard output to the same file

Some ‘magic’ variables:

  • $? is a variable that contains the return status of the most recent command executed.
  • $$ is a variable that contains the process ID. This can be useful to create a unique temporary filename.
  • $0 is the name of the script itself
  • $* contains all the arguments as one string value
  • $@ contains all the arguments as an array -> you can use:
    for file in “$@”; do
    rm file
    done

Linux Bash - Find Out Info About Your CPU

Friday, October 3rd, 2008

less /proc/cpuinfo

uname -a
x86_64 GNU/Linux indicates I have 64bit kernel running. If you use see i386/i486/i586/i686 it is a 32 bit kernel.

grep flags /proc/cpuinfo
* lm means Long mode - 64 bit CPU
* Real mode 16 bit CPU
* Protected Mode is 32-bit CPU

Linux Bash - Change The Prompt

Friday, October 3rd, 2008

To print your current prompt:
echo $PS1

To set a new prompt:
PS1=”[\u@\h \w]\$”
That’s very useful as it display the absolute working directory path

If you want to keep this change append the previous line to this file (using the export command):
/home/username/.bashrc OR /ect/bashrc (it aplies to all users)

Here you can find a complete list of placeholders:
http://www.cyberciti.biz/tips/howto-linux-unix-bash-shell-setup-prompt.html

You can also apply some colours:

 export PS1="\[\e[31;1m\]\u@\h \w]\$ \[\e[0m\]"   [better]
 export PS1="\[\e[31;1m\]\u@\[\e[34;1m\]\H> \[\e[0m\]"   [other example]

Linux Bash - Colour the Name of Directory and Files

Friday, October 3rd, 2008

alias ls=’ls –color’

If you want to make this changes permanent, append the line to the file:
/home/username/.bashrc

Linux - Download Automatically an Entire Website

Thursday, July 17th, 2008

wget -k –wait=20 –limit-rate=20K –no-parent -r -p -U Mozilla http://www.symfony-project.org/api/1_1/

-k -> make the links work in the local version (very very very cool)
–wait=20 –limit-rate=20K -> make our requests not too fast and band consuming…that to be fair and to avoid to be banned
–no-parent -> permits to restrict the download to the starting directory
-U Mozilla -> permit to be considered a Mozilla browser