Tuesday, August 5, 2008

Useful Unix Commands

Using A Usb Drive

  • Insert the drive into a usb port
  • Mount the drive % mount /media/usb-drive
  • Drive will appear on the desktop and is ready for use
  • Once you have finished using the usb drive don't forget to unmount before removing % umount /media/usb-drive

Unix Commands Basic

Command Description
cp fileA fileB copy fileA to fileB either new location or different name
mv fileA fileB Move or rename a file
ls -options list files from current location
cd ..\.. change direectory
rm -options fileA Remove file (use rmdir for directory)
vi fileA Edit a fileA
man command Help page for the command
pwd Show current path
cat fileA List contents of a fileA
su Login as a "Super User" with full rights
diff fileA fileB Show differences between fileA and file B
chmod -options fileA Change permissions for a file / directory
wc fileA List the number of lines/words/characters in fileA
ff Find a file anywhere on the system
ps -options List all processes running
kill PID Kill a process with ProcessID? specified
alias Create a new function with a new alias
bg send process to the background
fg Bring a process to the foreground
ps Report process status
cal Display the calander
date Print or set the date
clear Clears the terminal screen
find file finds the file from the current directory onwards
jobs Display a list of processes in foreground and background
ln Creat a link between files
lpq, lpr, lprm Printer commands if a printer exists

Examples of Unix Commands

Example: Find all files from current directory and below with extension .html
$find . -name "*.html" -print

Example: Tar Up Files and Zip them

$tar -cvzf filename.tar.gz file1 file2 ; where file1 and file2 can be directories

Example: Untar a tar.gz file

$tar -xvzf filename.tar.gz

Example: List contents of a tar.gz file

$tar -tvzf filename.tar.gz

Example: vi search and replace

:%s/search string/replacement string/g ; where g -globally

Example: Check disk usage in a Linux PC

$df -h

The following examples show how you can combine Unix commands to handle complex queries

Example: Remove all .gz files from current directory and below which are more than a months old

$find . -name "*.gz" -mtime +30 -exec rm '{}' \; 

Example: Remove all files with extension .html from current directory and below

$find . -name "*.html" -exec rm '{ }'\;

Example: Rename all .html files to .shtml from the current directory and below

$find . -name "*.html" -print| sed "s/\(.*\)\.html$/mv '&' '\1.shtml'/" 

Example: Find the number of lines in the files test.txt that do not begin with #

$cat test.txt

#
This
is
#a
Test
# End of Test

$sed 's/^#.*//' test.txt gives you

This
is

Test

ie.. it has replaced any line beginning with # with a blank line. Piping with the grep command to search for non empty lines gives

$sed 's/^#.*//' test.txt | grep -v '^$' or

$sed 's/^#.*//' test.txt | grep '[^\s+]'

This
is
Test

Now to get number of lines, use wc on the result above

$sed 's/^#.*//' test.txt | grep -v '^$'|wc -l



Ref : Jyotirmoy Sharma - 30 Jan 2008

No comments: