Unix Commands

Useful Unix Admin & Networking Commands

hostname

Prints the name of the current host system.

ls useful options

  • "ls -F" appends a forward slash to the subdirectory names so you can easily distinguish them from file names.
  • "ls -a" it will show all "hidden files".
  • "ls -l" it will show detailed information about each file and directory, including permissions, ownership, file size, and when the file was last modified.
  • "ls -d" If an argument is a directory, lists only its name (not its contents); often used with -l to get the status of a directory.

netstat

Displays a list of active sockets for each protocol.
netstat -an | grep 80 searches for port 80.

top

Top displays the top 15 processes on the system and periodically updates this information.

ps

The ps command prints information about active processes. Without options, ps prints information about processes that have the same effective user ID and the same controlling terminal as the invoker.
Use 'ps -ae' to display all the processes. To search if a particular process is running; for e.g. to see if telnet is running, run 'ps -ae | grep telnet'.

SCP

The OpenSSH Secure Copy (SCP) is a secure replacements for the CP commmand. Here is how you run.

To scp local files to a different machine,
cd /source_machine/directory/
scp localfiles [email protected]:/path/to/upload

In this example, your files are located on the client machine in /source_machine/directory/, you want to upload them to /path/to/upload. So to break this command down we have a few sections:

  • scp - The command itself
  • localfiles - The file or files you want to upload
  • [email protected] - the username and the destination host address.
  • :/path/to/upload - The path on the server to upload the files.

SSH

Secure Shell (SSH) is often used as a stealthy alternative to TELNET. You can read more about SSH here. If SSH is running on the remote machine on a different port, use the following command to login to a remote host via ssh:
ssh -p portNumber remoteUserName@DestinationHost

Rsync

rsync is an open source utility that provides fast incremental file transfer. It is a better alternative to scp. SCP copies all the files, rsync only copies the files that has changed.
To first do a dry run to see what would have transferred, run the following:
rsync -nav --progress --stats --rsh=ssh directoryToSync mailto:remoteUserName@DestinationRemoteHost:%7E/RemoteHostDirectory/ 

  • "rsync" means "run the rsync program"
  • "-a" means "Backup all sub-directories maintaining all permissions, groups, users, times, and devices."
  • the "v" after the "a" means "Be more verbose. Tell me more about what's going on."
  • the "n" after the "v" means "Don't actually do anything, just tell me what you're going to do.
  • the "--progress" means "Give me progress updates while you're backing things up."
  • the "--stats" means "Let me know exactly what you did after it's all done."

Now to actually sync, run the following command, which is the same command, but without the n option:
rsync -av --progress --stats --rsh=ssh directoryToSync remoteUserName@DestinationRemoteHost:~/RemoteHostDirectory/

RPM

The RPM Package Manager (RPM) is a powerful command line driven package management system capable of installing, uninstalling, verifying, querying, and updating computer software packages. To find your own RPM package, visit http://rpmfind.net/linux/RPM/. Here is how to use the command:

  • Install an RPM: rpm -i file.rpm
  • Remove an RPM: rpm -e name_of_rpm.rpm.
  • Upgrade an RPM: rpm -Uvh rpmname.rpm
  • List all rpms in the system: rpm -qa
  • List all files in an installed rpm: rpm -ql rpmname
  • List all the files in an rpm file that has not been installed on your system: rpm -qp rpmname.rpm -ql
  • Find out which rpm does the specified file belong to: rpm -q --whatprovides `which `

Common options:

--test - mock install/remove/etc. Don't really do it, but see if it works.
-v --hash - draw a progress bar of the install/remove/etc
--force - force an installation (can really mess up the database)
--nodeps - Don't check for dependencies (can really mess up the database)

Recursive grep on files

find . -type f -print | xargs grep -i [PATTERN]