choose language

Custom Search

Saturday 19 February 2011

unix tips, convert man pages to text files, linux tips, solaris tips, How to replace text in multiple files, execute commands at specified times

 Hi
Below some important general unix tips:
  • How to run a command to a set of files (The while read command can handle names with spaces but you need to quote the variable after):
    [unix]$ for i in `ls *mp3`; do cp $i $i.bak; done
    [unix]$ ls *mp3 | while read i; do cp "$i" "$i with more spaces"; done
  • How to rename part of a file:
    [unix]$ echo filename | sed 's/file/another/'
  • How to replace text in multiple files:
    [unix]$ sed -e 's/oldtext/newtext/' -i file1 file2 file3 ...
  • How to delete lines matching a pattern in some files:
    [unix]$ sed -e '/pattern/ d' -i file1 file2 file3 ...
  • How to change all letters to lowercase:
    [unix]$ echo FilENamE | tr A-Z a-z
  • A good way to change all filenames in a directory to lowercase:
    This handles a colored ls (hence the quotes around ls, but you can use any file list output, i.e. find) and names with spaces (hence the while read and not the for).
    [unix]$ "ls" | while read name; do mv "$name" "`echo $name | tr A-Z a-z`"; done
  • How to convert man pages to text files:
    [unix]$ man man | col -b
  • How to list all the open ports and programs:
    [linux]$ netstat -apne --inet
    [freebsd]$ sockstat
  • How to list all the open files and programs:
    [linux]$ lsof
    [freebsd]$ fstat
  • How to find information about an ip (i.e. 10.0.0.0):
    [unix]$ host 10.0.0.0
    [unix]$ dig -x 10.0.0.0
  • How to find information about an ip owner (i.e. 10.0.0.0):
    [unix]$ whois -h whois.arin.net 10.0.0.0
  • How to find out the mailserver for an ip:
    [unix]$ dig mx some.domain.name.net
  • How to generate a filename with yesterdays date (i.e. for logfiles/backups):
    [linux]$ touch logfile-`date -dyesterday '+%Y-%m-%d'`
    [freebsd]$ touch logfile-`date -v-1d '+%Y-%m-%d'`
    
    
Commands/options you should know about, read the man pages
  • tail - display the end of a file
  • head - display the beggining of a file
  • xargs - convert stdin to command line arguments
  • nmap - network scanner
  • watch (linux) - run a command periodically and watch the output
  • watch (freebsd) - monitor a tty
  • lsof - lists the open files
  • strace (linux) - trace the system calls of a program
  • truss (freebsd) - trace the system calls of a program
  • calc - command line calculator program
  • bc - another command line calculator program
  • dc - command line RPN calculator program
  • du - how much disk space is being used by something
  • df - disk free, how much free space there is
  • touch - creates an epty file or updates the timestamp on a file
  • diff - show the difference between 2 files
  • strings - show the strings in a file (useful for binaries)
  • file - tells you what type a file is
  • finger - find out information about users
  • w / who - who is currently on the machine and what are they doing
  • id - tells you who you are
  • uname - tells you basic information about the system.
  • whois - information about hosts
  • crontab - execute commands at specified times
  • last - last users to log into the system
  • mail - very basic mailer, useful for redirection
  • nice/renice - set the niceness ("priority") level of a program
  • nohup - leave a program running after logging off
  • screen - virtual terminal for leaving programs running
  • psnup - print multiple pages in one page
  • time - time the execution of a command
  • wc - word count, tells you the size in bytes, words and lines
  • which / whereis - locates commands
  • locate - locates files
  • whatis - gives brief summary of a comm
  • and
  • wget / fetch - get files off the net
  • talk - chat with other users on the system
  • write / wall - send messages to users of the system
  • zcat - cat gzipped files
  • ln - link files
  • sleep - wait a specified amount of time
  • md5sum (linux) - calculate the md5 signature of a file
  • md5 (freebsd) - calculate the md5 signature of a file
  • enscript - convert text files to ps
  • awk, sed - useful scripting programs
  • cat, echo, more/less, find, grep - you need to know these, it's not an option.

Resource: http://sluglug.ucsc.edu/~isolis/knowledge/unix-tips/

No comments: