Linux and Unix Commands

My collection of handy Linux, Unix, and Mac OS terminal commands.

Change file permissions

Command Usage
chmod +x filename.ext Give execute access to a file.
sudo chmod -R 777 workspace -R means RECURSIVE
sudo chown -R basejump workspace Take ownership of a file. basejump (the user) and workspace (the directory)

Compress or extract files

Command Usage
tar -zxvf file.tar.gz To extract one or more members from an archive:
tar -czvf name-of-archive.tar.gz /path/to/directory-or-file

Compress an entire directory or single file.

Note: Be sure to add the .tar.gz in the archive file name yourself; the command doesn't do it for you.

Here's what the switches mean: -c: Create an archive. -z: Compress the archive with gzip. -v: Display progress in the terminal
while creating the archive, also known as "verbose" mode. The v is always optional in these commands, but
it's helpful. -f: Allows you to specify the filename of the archive.

Create an alias for a common command

Command Usage
alias p="<command>"

Create shortcut aliases to common commands.

For example:

alias p="open /x/yx/z" to open a particular directory in Finder

Directory commands

Command Usage
pwd Print working directory (the directory you're currently in).
mv <oldDirName> <newDirName> Rename a directory. Be aware: this is actually a move command. So, you’re just moving
the directory from one name to another in the same path.
rmdir Remove an empty directory
rm -rf <dirName> Forcefully remove a directory recursively.

Remove the f switch to be prompted for each sub-directory and file inside.

cp -rf present/directory /desire/directory Copy an entire directory, its subdirectories, and files.

Remove the f switch to be prompted for each sub-directory and file inside.

Execute command as root user

Command Usage
sudo <command> Execute command as the root user.
sudo !! Execute the last command as root user. (!!) represents the last command
you just tried to run, but couldn't because of permission issues.

Execute task in background

Command Usage
./ & Start the execution in the background, which will allow you to kill your SSH terminal
without killing the process itself.

Find large files

Command Usage
cd / find . -size +10000000c -print Prints out the names of all files with size > 10mb.
cd / sudo du -sm * Examine the size of the directories under /. You can then navigate into
any given subdirectory and execute du -sm * again to see which subdirectories are the largest.

Inspect disk space and usage

Command Usage
df -h Inspect disk space and usage (in MB or GB)

Work with environment variables

Command Usage
printenv Prints all currently set environment variables and values.

Remove a file

Command Usage
mv Rename a file
rm Remove a file
rm -f Forcefully remove a file
rm -i Interactively remove a file

If you are not certain about removing files that match a pattern you supply, it is always good to run rm interactively (rm –i) to prompt before every removal.

Switch user

Command Usage
su – <username> Switch to the given user, loading their profile. You may have to use sudo su – <username>.
su Without a username means to just switch to root user.
whoami "Who Am I?" Prints the current user.

View a file

Command Usage
cat Used for viewing files that are not very long; it does not provide any scroll-back.
tac Used to look at a file backwards, starting with the last line.
less Used to view larger files because it is a paging program; it pauses at each screenful of text, provides scroll-back capabilities, and lets you search and navigate within the file. Note: Use / to search for a pattern in the forward direction and ? for a pattern in the backward direction. Press Q to quit.
tail Used to print the last 10 lines of a file by default. You can change the
number of lines by doing -n 15 or just -15 if you wanted to look at the last 15 lines instead of the default.
head The opposite of tail; by default it prints the first 10 lines of a file.

Update the OS packages

sudo apt-get update sudo apt-get upgrade sudo apt-get dist-upgrade sudo reboot

Use pipes

The UNIX/Linux philosophy is to have many simple and short programs (or commands) cooperate together to produce quite complex results, rather than have one complex program with many possible options and modes of operation. In order to accomplish this, extensive use of pipes is made; you can pipe the output of one command or program into another as its input. In order to do this we use the vertical-bar, |, (pipe symbol) between commands as in: $ command1 | command2 | command3 This represents what we often call a pipeline and allows Linux to combine the actions of several commands into one. This is extraordinarily efficient because command2 and command3 do not have to wait for the previous pipeline commands to complete before they can begin hacking at the data in their input streams; on multiple CPU or core systems the available computing power is much better utilized and things get done quicker. In addition there is no need to save output in (temporary) files between the stages in the pipeline, which saves disk space and reduces reading and writing from disk, which is often the slowest bottleneck in getting something done.

Split a large text file into smaller parts

The Linux split command can help you break up really large text files into smaller, more manageable parts.

split -l <line-numbers> <source-file> <destination-file-prefix>

For example:

split -l 5000000 data3.trig segment

Fast string replace using stream editor (sed)

If you need to replace strings in a very large file or many files, you can use sed, the streaming editor. It's very fast because it uses streaming. This sure beats opening individual files in some editor and then using a find and replace function. The string positions expect regex, so be careful fo special characters.

sed 's/<original-string>/<revised-string>/' <source-file> > <new-file>

For example:

sed 's/' segment-aa > segmentaa

Change default shell (on Mac OS X)

You can change the default shell through the command line chsh command, which is shorthand for ‘change shell’. You’ll need to authenticate each change as well, the command will ask directly or you can prefix it with sudo. Here’s how to set the default user shell to zsh, bash, tcsh, ksh, sh, or any other shell for that matter.

  • zsh: chsh -s /bin/zsh
  • ksh: chsh -s /bin/ksh
  • tcsh: chsh -s /bin/tcsh
  • bash (default): chsh -s /bin/bash
  • sh: chsh -s /bin/sh
  • other shells: chsh -s /path/to/alternate/shell
Related Web Resources