Skip to end of metadata
Go to start of metadata

A cheatsheet for common Linux / Unix commands.


Contents

Change File Permissions

CommandUsage
chmod +x filename.extGive execute access to a file.
sudo chmod -R 777 workspace-R means RECURSIVE
sudo chown -R basejump workspaceTake ownership of a file.
basejump (user) and workspace (directory) 

Compress or Extract Files


CommandUsage
tar -zxvf file.tar.gzTo 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

CommandUsage
alias p=<command>Create shortcut aliases to common commands.
For example:
alias p="open /x/yx/z" to open a particular directory in Finder

Execute Command as Root User

CommandUsage
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

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

Find Large Files

CommandUsage
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 dm -sm * again to see which subdirectories are the largest. 

Inspect Disk Space and Usage

CommandUsage
df -hInspect disk space and usage (in MB or GB)


Work with Environment Variables

CommandUsage
printenvPrints all currently set environment variables and values.

Remove a File


CommandUsage
mvRename a file 
rmRemove a file 
rm -fForcefully remove a file
rm -iInteractively 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.

Directory Commands


CommandUsage
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.
rmdirRemove 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.


Switch User

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


View a File

CommandUsage
catUsed for viewing files that are not very long; it does not provide any scroll-back.
tacUsed to look at a file backwards, starting with the last line.
lessUsed 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.
tailUsed 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.
headThe opposite of tail; by default it prints the first 10 lines of a file.


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


The above 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.