anthonyjpalumbo.net

Unix Commands




Common unix commands I have used and put together for quick reference.


Navigation

Command/Flag Explanation
cd ~ Changes the directory to the home.
cd directory-name Changes the directory to the specified one.
cd .. Moves up one directory.
cd - Switched back to the previous directory.
ls Lists the contents of the current directory.
ls -a Lists the contents of the current directory including invisible files/directories.
ls -l Lists the contents of the current directory vertically with file size, permissions, etc.
ls -F Lists the contents of the current directory with "/" after sub-directories.
ls -X Lists the contents of the current directory sorted by file extension, then alphabetically.
ls *.ext Lists only the files with the extension ".ext", works with any extension.
mkdir name Creates a new directory with the specified name.
vim filename Opens a text file with the vim editor (":q!" to exit if unfamiliar with vim).
pwd Prints the current directory path.
chmod +x filename Adds executable permission to a file.
chmod +r filename Adds read permission to a file.
chmod +w filename Adds write permission to a file.
cat filename Displays a text file in the terminal.
grep "expression" Searches for all instances of the expression in the current directory.
head filename Displays the first 10 lines of a file.
tail filename Displays the last 10 lines of a file.


Moving, removing, and copying files.

Command/Flag Explanation
mv oldname newname Rename the old file to the new filename.
mv filename /path/to/new/location Moves the file to the specified path.
rm filename Removes the file.
rm * Removes everything in the directory.
rm -r directory-name Removes everything inside a directory, then removes the directory.
rmdir directory-name Removes an empty directory.
cp oldfile newfile Copies the old file into one with the new name.
cp file destination/to/go Copies the file to the new destination.


SSH and SCP

Command/Flag Explanation
ssh username@target-host Connects to the target host using the secure shell.
scp username@target-host:~/path/to/file . Copies the file from the remote server to the current directory.
scp -r username@target-host:~/path/to/directory . Copies all the contents of the directory on the remote server to the current directory.
scp username@target1:~/path/to/source/file username@target2:~/path/to/copy/location Copies the file from target 1 to the directory in target 2.


Git

Command/Flag Explanation
git init project-name Initializes a directory with a .git folder inside it.
git init --bare project-name Initializes a bare repository with the project name.
git clone link-to-repository Creates a git repository copy from a remote source.
git status Shows the status of the local copy of the repository.
git diff Shows the changes that have not been added to the index, i.e. not staged.
git add/stage filename Adds the file changes to your index.
git add/stage * Adds all file changes to your index.
git commit -m "Message" Commits the changes in the index with the message given.
git commit --amend Adds the current staged changes to the last commit for the repository.
git stash Saves the local changes while reverting the working tree back to the HEAD.
git stash apply Restores the changes from the last stash, without removing them from the stash list.
git stash pop Restores the changes from the last stash, removing them from the stash list.
git stash clear Deletes all current stashes.
git reset Unstages files after they have been added/staged.
git reset HEAD~ Cancels the last commit, but keeps the changes in the working tree so your work is not lost.
git clean Removes all untracked files and cleans the working tree.
git push Pushes the latest commit to the remote repository.
git pull Gets changes from the remote repository.
git log Shows the list of commits on the branch.
git checkout newbranch Checks out a new branch.
git merge new-version Merges branches into your current version.


C Programming

Command/Flag Explanation
gcc main.c Compiles main.c to make an executable, default name is a.out .
gcc main.c -o name Compiles main.c to make an executable with the given name.
gcc -E main.c -o main.i Preprocessess main.c to main.i .
gcc -S main.c -o main.s Compiles main.c to assembly instructions in main.s .
gcc -c main.c -o main.o Assembles main.c into machine code, creating an object file, main.o .
gcc main.o -o main.out Links the object file main.o into an executable.
-Wall Flag used to show major warnings when compiling.
-Wextra Flag used to show minor warnings when compiling.
-std=c99 Flag used to specify the C99 standards.
-pedantic Flag that is picky about syntax in source files.
valgrind --leak-check=full ./program Runs valgrind to analyze memory usage, good for finding memory leaks.

C++ Programming

Command/Flag Explanation
g++ main.cpp Compiles main.cpp to make an executable, default name is a.out .
g++ main.cpp -o main.out Compiles main.cpp to make an executable named main.out .
g++ -E main.cpp -o main.i Preprocesses main.c to main.i .
g++ -S main.cpp -o main.s Compiles main.c to assembly instructions in main.s .
g++ -c main.cpp -o main.o Assembles main.cpp into machine code, creating an object file, main.o .
g++ main.o -o main.out Links the object file main.o into an executable.


Java Programming

Command/Flag Explanation
javac file.java Compiles file.java to file.class .
java file Runs file.class through the jvm.
javac -sourcepath src src/file.java -d out Compiles file.java in the src directory into file.class in the out directory.
java -cp out file Runs file.class, which is in the out/ directory, through the jvm.
jar cvfm MyJarFile.jar Manifest.txt *.class Makes a .jar executable file which combines all the .class files in the directory.
jar xvf MyJarFile.jar Expands the jar file to all the individual files it contained.
java -jar MyJarFile.jar Runs the .jar executable using the jvm.


Gradle Commands

Command/Flag Explanation
<gradle> is equivalent to <./gradlew> and <gradlew.bat> If gradle is installed you may use the command "gradle", if not use "./gradlew" on Linux and OSX or "gradlew" to run the gradlew.bat file on Windows.
gradle build Compiles the .java files into .class files, creates a .jar if enabled, and runs all junit tests that are written.
gradle classes Compiles the .java files into .class files.
gradle clean Removes .class, .jar, and any build files that can be re-generated.
gradle run Compiles the java files and runs the program.
gradle test Runs the unit tests for the project.


Python Programming

Command/Flag Explanation
python file.py Runs file.py using the default version of python.
python2 file.py Runs file.py using the version of python 2 installed.
python3 file.py Runs file.py using the version of python 3 installed.
pip install [package-name] Installs a python package using pip.


Miscellaneous

Command/Flag Explanation
unzip file.zip Extracts the zip file in the current directory.
zip new.zip file1 file2 directory1 Creates a zip file by compressing all the files and directories named.
tar -cvf file.tar file1 file2 file3 Creates a tarball of the 3 files.
tar -xvf file.tar Expands the tarball in the current directory.
tar -zcvf archive.tar.gz directory Creates a compressed tarball of the directory specified.
tar -zxvf archive.tar.gz Extracts and expands the compressed tarball.
sh script.sh Runs the shell script in the terminal.
wc -w filename Counts the number of words in a file.
wc -l filename Counts the number of lines in a file.
ln -s target-file-path link-name Creates a symlink to a file.
du -sh directory/* Displays the disk space used by all the files and sub-directories of the file/directory specified.
top Displays information about CPU and memory usage.
free Displays information about used and unused memory in kilobytes.
free -m Displays information about used and unused memory in megabytes.
free -g Displays information about used and unused memory in gigabytes.
watch free Displays information about used and unused memory and updates the display every two seconds.
sensible-browser Opens a new window in the default web browser.
sensible-browser filename.html Opens the local html file in a new tab in the default web browser.
time some-command Runs a command and displays the time it took to run.