Linux - Cut Lines
Saturday, January 3rd, 2009Very useful when you pipe it, for example, for easily reading a file containing very long quries
cat queries.sql | cut -b -10
Very useful when you pipe it, for example, for easily reading a file containing very long quries
cat queries.sql | cut -b -10
ls *.sql | awk ‘{printf(”mysql -u [username] -p[password] [databasename] < %s\n”, $1) | “/bin/sh” }’
Add this row into your /etc/fstab:
/dev/hda6 /mnt/data auto rw,uid=1000,utf8 0 0
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
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
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:
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
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]
alias ls=’ls –color’
If you want to make this changes permanent, append the line to the file:
/home/username/.bashrc
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