Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Friday, May 16, 2014

List files in sorted order using 'du'

Listing of all the files (and subdirectories) within a directory along with their sizes in sorted and readable format.

$ sudo du /backup/ -h | sort -h

Monday, December 12, 2011

Linux Commands - Funny

cat, cut, find,free, head,kill, man,nice, paste,sleep,tail, touch, watch , who - What is this!!
Linux is a Unix-like computer operating system assembled under the model of free and open source software development and distribution. Nowadays, Linux commonly using in Desktop machines as well as Servers.  Lots of commands are available in Linux for sysem use. Most of them can easily remember to a normal user. Some of the interesting commands in Linux as shown here.
CommandDescription
catConcatenate files and print on the standard output
clearClear terminal screen
cutDivide a file into several parts
dateDisplay or change the date & time
echoDisplay message on screen
ejectEject CD-ROM
expandConvert tabs to spaces
factorPrint prime factors
findSearch for files that meet a desired criteria
foldWrap text to fit a specified width
formatFormat disks or tapes
freeDisplay memory usage
headOutput the first part of file(s)
joinJoin lines on a common field
killStop a process from running
lessDisplay output one screen at a time
manHelp manual
niceSet the priority of a command or job
pasteMerge lines of files
screenTerminal window manager
selectAccept keyboard input
sleepDelay for a specified time
sortSort text files
splitSplit a file into fixed-size pieces
tailOutput the last part of files
testEvaluate a conditional expression
timeMeasure Program Resource Use
touchChange file timestamps
topList processes running on the system
watchExecute/display a program periodically
whereisReport all known instances of a command
whichLocate a program file in the user's path
whoPrint all usernames currently logged in
whoamiPrint the current user id and name (`id -un')
yesPrint a string until interrupted

Friday, December 2, 2011

vi Commands

vi is a Visual Editor (hence the name -- vi for VIsual). vi is a widely-used and popular UNIX-based text editor. Like most UNIX system interfaces and other text editors, it lets you control the system by using the keyboard rather than a combination of mouse selections and keystrokes.

Edit a File
Edit a file from command prompt vi
Edit a file from command prompt for reading only vi -R
Edit a file from within vi :e
Edit a new file from within vi, discard changes to current file :e!
Reload current file, discarding changes :e!
Go forwards a page Ctrl+F (or PgDn)
Go backwards a page Ctrl+B (or PgUp)
Move around single lines or characters Arrow keys
Save changes :w
Save changes and override protected (read-only) files :w!
Save changes and exit vi ZZ
Quit :q
Quit and discard changes :q!
Get general help :help
Get help on a command (eg. :set) :help set
Go to lines, find matching text 
Go to line 1234 (do not see typing) 1234G
Go to line 1234 (see typing) :1234
Go to start of file 1G
Go to end of file G
Find (forwards) a line containing "swordfish" /swordfish
Find (forwards) a line using a regular expression /you see .* here
Repeat last search n
Repeat last search in opposite direction N
Find (backwards) a line containing "swordfish" ?swordfish
Find (backwards) a line using a regular expression ?you see .* here
Search case insensitive (Ignore Case) :set ic
Search with case sensitivity :set noic
Wrap searches back to start of file :set wrapscan
Do not wrap searches :set nowrapscan
Insert Text
Insert after cursor i
Insert before cursor a
Insert at beginning of line I
Insert at end of line (append) A
Open (start) new line below cursor o
Open (start) new line above cursor O
Advance Deletion
Delete next 5 lines 5dd
Delete to end of line d$
Delete to start of line d0
Delete to word "swordfish" d/swordfish
Delete to the letter "x" dfx
Delete lines 10 to 20 :10,20d
Delete current line and another 5 lines :.,+5d
Delete all lines :%d
Delete current word dw
Find line containing pattern, delete it :/pattern/d
Find line containing "dog", delete until line containing "cat" :/dog/,/cat/d
Delete from current line to line containing "foo" :.,/foo/d

Friday, May 6, 2011

Ubuntu Linux: How do I install .deb packages?

dpkg is a tool to install, build, remove and manage packages. dpkg itself is controlled entirely via command line parameters. For example -i use to install .deb file.

How do I install .deb file?

To install package called package.deb type the following command:
Go to directory where package.deb is kept. For example if it is in /tmp directory:

$ cd /tmp

Type the following command:

$ sudo dpkg -i package.deb

Monday, January 3, 2011

Find out a file which contains specific string

To find out a file which contains specific string, execute the following command from shell


# find /var/www/html/mysite-iname '*php' | xargs grep 'about' -s1

Here, searchesfor file which contains string 'about' .

Sunday, November 14, 2010

rsync - The Linux Backup Command

rsync stands for remote sync.

rsync is used to perform the backup operation in UNIX / Linux.

rsync utility is used to synchronize the files and directories from one location to another in an effective way. Backup location could be on local server or on remote server.

Important features of rsync

  • Speed: First time, rsync replicates the whole content between the source and destination directories. Next time, rsync transfers only the changed blocks or bytes to the destination location, which makes the transfer really fast.
  • Security: rsync allows encryption of data using ssh protocol during transfer.
  • Less Bandwidth: rsync uses compression and decompression of data block by block at the sending and receiving end respectively. So the bandwidth used by rsync will be always less compared to other file transfer protocols.
  • Privileges: No special privileges are required to install and execute rsync

Syntax

$ rsync options source destination

Source and destination could be either local or remote. In case of remote, specify the login name, remote server name and location.

Example . Synchronize Two Directories in a Local Server

To sync two directories in a local computer, use the following rsync -zvr command.

$ rsync -zvr /opt/lampp/htdocs/site1/ /backup1/site1bkp
building file list ... done
sva.xml
svB.xml
.
sent 26385 bytes received 1098 bytes 54966.00 bytes/sec
total size is 44867 speedup is 1.63
$

In the above rsync example:

  • -z is to enable compression
  • -v verbose
  • -r indicates recursive

Tuesday, October 12, 2010

What is Crontab ?

What is Crontab?
Crontab (CRON TABle) is a program that manipulates the CRON daemon, making it easy for users to schedule task and run programs/commands at pre determined periods of time. Crontab can also be considered a file witch contains commands that will be run by the system as the user that owns the crontab file.

What is the purpose of Crontab?
Cron is designed to maintain a list of commands that the system needs to run at a given time interval.

For example if you have a script that generates statistics and needs to be run every couple of hours or everyday cron can do it for you. Or for example if you have a script that sends a newsletter every month you can use cron to run the script that sends the newsletter every month or week.

Crontab commands
When your logged in to your server you can use program cron using the following commands:

crontab -l
Lists the current cron jobs
crontab -e
Edit your current crontab file and ad/remove/edit crontab tasks.
crontab -r
Remove the crontab file.
crontab -v
Displays the last time you edited your crontab file.

The crontab file - components of crontab

When you enter the edit mode (crontab -e) and start adding tasks to your cron file you need to consider the following syntax:

The asterisk (*) symbolizes that every instance of that field (i.e. every minute, hour, day, month, weekday) will be used in the command.

Example on how to setup your first crontab
Lets say you have a script named run-me.sh located in /home/your_username you want to run every day at 13:30. You will need to login your server console and input the following commands:

crontab -e

This will open the crontab file and let you edit it. By default this file will be opened with the VI editor and you will need to press the "Insert" key on your keyboard to be able to write in that file.

30 13 * * * /home/your_username/run-me.sh >/dev/null 2>&1

The first character you see is "30" this means that crontab will run the script every time the clock hits the 30 minutes mark. Next "13" this means that crontab will run the script when the clock hits 13. The next three * tell crontab to run the script every day, of every month of every weekday. Combining these fields crontab will run the script every day at exactly 13:30. You may notice that we added the ">/dev/null 2>&1" string at the end of the command. The default cron job will always send and e-mail to the root account when ever a command is executed. Now you don't want to be notified every day that your crontab job has been executed. If you don't want to receive e-mails every day notifying you about your job's execution place this ">/dev/null 2>&1" at the end of every instance of every crontab command.

Now to list your crontab job just issue the following command: crontab -l
If you need to ad another crontab job or even more all you need to do is follow the same steps as above and just ad another line to the file.

REMEMBER: Every crontab job needs to be placed on its own line in the file and after the last line you need to insert a non-braking character (press Enter).

The crontab syntax goes a little beyond its boundaries and has more advance meaning for some users.
For example if you wish to use more then one instance of one column you will separate those instances by a comma "," or if you wish to use a time period lets say for example from Sunday till Tuesday you will use dash "-".

10,20,30 13 * * 0-2 /home/your_username/run-me.sh >/dev/null 2>&1

This crontab job will run your scrip "run-me.sh" on from Sunday until Tuesday (Sunday, Monday, Tuesday) at 13:10, 13:20 and 13:30. Remember there are no spaces between values separated by commas "," and neither in dashes "-". There is also an operator that some versions of cron support called the slash operator "/" that can be used to skip a given number of values in your jobs.

Crontab examples - cron examples
The syntax of crontab is not very easy to understand from the start and the best way of understanding is from examples:

Run the script every day at 12:00.
0 12 * * * /home/your_username/run-me.sh >/dev/null 2>&1

Run the script every day at 12:00, 14:00 and 16:00.
0 12,14,16 * * * /home/your_username/run-me.sh >/dev/null 2>&1

Run the script every Sunday at 13:30.
30 13 * * 0 /home/your_username/run-me.sh >/dev/null 2>&1

Run the script every Saturday at 12:00, 14:00 and 16:00.
0 12,14,16 * * 6 /home/your_username/run-me.sh >/dev/null 2>&1

Run the script on the first, fifteenth and twentieth of every month.
0 0 1,15,20 * * /home/your_username/run-me.sh >/dev/null 2>&1

Run the script every day from 3 to 3 hours: 00:00, 03:00, 06:00 etc.
0 */3 * * * /home/your_username/run-me.sh >/dev/null 2>&1