A cheatsheet for common Linux / Unix commands.
Change File Permissions
|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 (user) and workspace (directory)
Compress or Extract Files
|tar -zxvf file.tar.gz||To extract one or more members from an archive:|
Create an Alias for a Common Command
|alias p=<command>||Create shortcut aliases to common commands.|
alias p="open /x/yx/z" to open a particular directory in Finder
Execute Command as Root User
|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
|./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
find . -size +10000000c -print
|Prints out the names of all files with size > 10mb.|
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
|df -h||Inspect disk space and usage (in MB or GB)|
Remove a File
|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.
Rename or Remove a Directory
|mv||Rename a directory|
|rmdir||Remove an empty directory|
|rm -rf||Forcefully remove a directory recursively|
|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
|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.|
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.
An example SPARQL query filtering for resources within a given date range (between two given dates).