Sunday, October 3, 2010

30+ useful Linux commands list

This collection of commands are some of the commands I have learned in these years. Of course I am not the author of them, as no one is the author of any command in Linux. I have found some of them in differente pages, and modify some of them, others I have "created" by myself. Reading books, or man pages. Disclaimer: Try any of these commands, in non-production servers, some of them may break something Add yours in the comments.
  1. Purge configuration files of Debian's removed packages

  2. There are different ways to do this, these are some options.sudo aptitude purge '~c' sudo dpkg -l | awk '/^rc/ {print $2}' | xargs dpkg -P
  3. Create movies from jpg files

  4. mencoder "mf://*.jpg" -mf fps=10 -o test.avi -ovc lavc -lavcopts vcodec=msmpeg4v2:vbitrate=800
  5. Empty (erase contents) of a file

  6. Once again there are different ways to do this::> file truncate -s0 file
  7. Find duplicate files, based on size and MD5

  8. find -not -empty -type f -printf "%s\n" | sort -rn | uniq -d | xargs -I{} -n1 find -type f -size {}c -print0 | xargs -0 md5sum | sort | uniq -w32 --all-repeated=separate
  9. Replace space on file names with underscores

  10. #!/bin/bash
    
    ls | while read -r FILE
    do
        mv -v "$FILE" `echo $FILE | tr ' ' '_' `
    done
    
  11. Convert a man page to txt file

  12. man cp | col -b > /tmp/man-cp.txt change cp for any command you want to have its man page in text format.
  13. List the most used commands in your history

  14. history | awk '{print $2}' | sort | uniq -c | sort -rn | head
  15. Execute a command, and do not save it in history

  16. command
  17. Attach a clock to the command line prompt

  18. export PS1="${PS1%\\\$*}"' \t \$ ' The output will look like this:
    [ggarron@arch ~] 16:13:14 $
    
  19. Force file system check

  20. sudo touch /forcefsck orsudo shutdown -rF now
  21. Create and mount a RAM disk

  22. mount -t tmpfs tmpfs /mnt -o size=1024m That will create a 1 Gig RAM disk, that you can use to have fast access to files there, just remember is a RAM disk, and all its contents will be deleted when the system is rebooted.
  23. Call commands from History

  24. You may call command from history by numbers:class="codigo">history
    963  du -h
      964  ls
      965  df
      966  htop
      967  df -h
      968  df -h 
      969  history
    
    Then using the number call a command:!963 Or call it by using the first letters:!df This last method, is somehow dangerous, as you do not know what arguments and options the command is going to use, so, you can pause it. Using this way is safer:!df:p It shows you the command but do not run, it, then, you can run it without the :p and it will run, but this time you know the options.
  25. Count lines and words in a text document

  26. wc -l -w mytextfile.txt
  27. Mount a remote file system via ssh

  28. sshfs remote-user@remote.server:/remote/directory /mnt/remote-fs/ More info: here
  29. Copy ssh public key to a server to access it with no ssh password

  30. cat ~/.ssh/id_rsa.pub | ssh user@remote.machine "mkdir ~/.ssh; cat >> ~/.ssh/authorized_keys" More info: here
  31. Generate random passwords

  32. dd if=/dev/urandom bs=16 count=1 2>/dev/null | base64 With 16 characters, if you want only 8.dd if=/dev/urandom bs=8 count=1 2>/dev/null | base64
  33. Update Twitter from the command line

  34. curl --basic --user "user:pass" --data-ascii "status=Arriving the office" "http://twitter.com/statuses/update.json"
  35. Replace words in a document using sed

  36. sed -i 's/OLD/NEW/g' FILE More info here
  37. List type of network connections active and the number of them

  38. netstat -ant | awk '{print $NF}' | grep -vE '[:upper:]'| sort | uniq -c | sort -rn Output:
    35 TIME_WAIT
         27 ESTABLISHED
          2 LISTEN
          2 CLOSE_WAIT
    
  39. Mount an iso image as disk

  40. mount -t iso9660 -o loop file.iso /media/disk
  41. Reset the terminal, when it get screwed, and you can only see garbage on it

  42. reset
  43. List all processes with stablished connections

  44. lsof -i -n | grep ESTABLISHED More info here
  45. Backup a disk or partition to an image

  46. dd if=/dev/sda of=~/backup-disk-YY-MM-DD.img More info here
  47. Simple HTTP server to share files

  48. python -m SimpleHTTPServer
  49. Change the layout of you keyboard on the fly

  50. setxkbmap -layout us
  51. Read a compressed file on the fly, without uncompressing it

  52. zcat file.gz
  53. Use lynx to store web pages as text

  54. lynx -dump http://www.go2linux.org > $HOME/go2linux.txt
  55. Determining the type of file

  56. file *
  57. make commands keep running after leaving a terminal session or ssh session

  58. nohup nice -n 4 [command] > output.txt & More info here
  59. Recreate fluxbox menu

  60. fluxbox-generate_menu

No comments: