Command Line Utilities: Difference between revisions

From Elvanör's Technical Wiki
Jump to navigation Jump to search
Line 59: Line 59:
* Something very tricky can happen with ps: ps lists a process, but ps | grep will not. This is because if processes are always spawned and short lived (which means you have a problem on your system), ps when started on a shell will get the time to catch the process but ps | grep will run faster (less text to output) and so won't catch the process. A way to check for signs of this problem is to launch htop and see if the CPU is at 100% usage.
* Something very tricky can happen with ps: ps lists a process, but ps | grep will not. This is because if processes are always spawned and short lived (which means you have a problem on your system), ps when started on a shell will get the time to catch the process but ps | grep will run faster (less text to output) and so won't catch the process. A way to check for signs of this problem is to launch htop and see if the CPU is at 100% usage.
* ps can display the environment of the current running processes, with the e option.
* ps can display the environment of the current running processes, with the e option.
== pstree ==
* A variant of ps to display process in a tree hierarchy. Can be useful.


== detox ==
== detox ==

Revision as of 04:42, 11 December 2009

This is a collection of random useful command line tools.

Network Related

rsync

  • --no-p: this will disable permissions synchronization.
  • --no-o: this will disable owner synchronization.
  • The -C option will exclude default files and directories like .svn, .cvs.
  • By default rsync does not delete absent files on target directory; you must add --delete to do so.
  • The -R (relative path) option can be very useful if you want to sync multiple directories at once. You can use the special syntax "/./" to specify which part of the path should go onto the target directory (read the manual page for more details).

nmap

  • This program allows you to test if a port is open on a remote box.
  • To check an UDP port (TCP is default), add the -SU option.

netstat

  • netstat -altp will list all the open ports on your machine, along with the process that opened it. Very useful!
  • The -n option will print the numeric values of ports, instead of resolving to symbolic names of services.

route

  • The -n option will print IP addresses instead of symbolic server names.

File & I/O

  • To obtain the space available on a HD: df -h
  • To obtain a list of the sizes of all the directories of the current directory: du --max-depth=1 -h

tar

  • Use the -C option to switch to a directory before compressing. Very useful.
  • Use the -h option to follow symbolic links.

cpio

  • This seems to be something similar to tar.
  • Allows you to do nice stuff like
find . -name '*.txt' print0 | cpio -o -0 | ssh elvanor@otherbox 'cd dirB && cpio -iduv -0'

This would copy the *.txt files to another box, preserving the directory hierarchy.

Administration

gpasswd

  • This is the command used on Gentoo to change an user's groups. You can also edit /etc/group, but this is not the right way of doing things. It will usually fail because you also need to change /etc/gshadow. Note that /etc/group- is just a backup of /etc/group.

useradd

  • Command to add a new user.

Other

ps

  • The legend for the status letters of processes can be found in the man page. R basically means running, and S means sleeping.
  • Something very tricky can happen with ps: ps lists a process, but ps | grep will not. This is because if processes are always spawned and short lived (which means you have a problem on your system), ps when started on a shell will get the time to catch the process but ps | grep will run faster (less text to output) and so won't catch the process. A way to check for signs of this problem is to launch htop and see if the CPU is at 100% usage.
  • ps can display the environment of the current running processes, with the e option.

pstree

  • A variant of ps to display process in a tree hierarchy. Can be useful.

detox

  • This small utility can sometimes be useful to rename files with corrupted or strange characters in the file name.

Tricks and tips

  • Redirecting all outputs to a file:
foo &> bar

To redirect only stderr:

foo 2> bar
  • To output to a file in addition to the console, use the tee binary:
echo "Hello World" | tee hello.txt
echo "Hello World" 2>&1 | tee hello.txt # This redirects ALL outputs to tee