Basic Unix Commands

Tue Jan 28 2025
Updated: Thu Jun 26 2025
Facebook share linkTwitter/X share linkLinkedIn share linkReddit share linkReddit share link

Unix commands are used to perform very specific tasks. There are many commands used to navigate/use unix file systems with command line interpreters. Lists of most of the commands available can be found in sections 3.2/5.1/5.2/5.3/5.4 of An Introduction to the Linux Command Shell For Beginners. These lists are quite extensive. The following table contains the most commonly used commands.

CommandDescriptionExample
pwdPrint working directory - Prints the file path to the current working directory.pwd
ls / ls <path>List - When used alone displays the contents of the current working directory. When used with <path> displays the contents of the directory found at <path>.ls
cd / cd <path>Change directory - When used alone switches to the root directory. When used with <path> changes from the current working directory to <path>.cd desktop
touch <path>/<file>Create File - Creates the <file> under <path>. This command will not create directories. This command only works on Unix/Linux/Mac.touch file.txt
rm <path>/<file>Remove File - Removes the <file> under <path>. This command will not remove directories.rm file.txt
cat <path>Concatenate - Displays the contents of the file at <path>.cat log.txt
head <path>Display beginning of file - Displays first 10 lines of the file located at <path>.head log.txt
tail <path>Display end of file - Displays last 10 lines of the file located at <path>.tail log.txt
mkdir <path>Make directory - Makes a new directory at <path>.mkdir new
cp <path> <destPath>Copy - Copies the file at <path> to <destPath>. cp file.txt new/file.txt
mv <path> <destPath>Move - Moves the file from <path> to <destPath>.mv file.txt new/file.txt

Note: <file>/<path>/<destPath> must be valid files/paths from the current working directory.



For the following examples use the following desktop as a reference for the questions:

Each of the following examples is a continuation from the previous example.

  1. Use a Unix command to display the path to the desktop from the root directory.
alex@imac desktop % pwd /Users/alex/desktop alex@imac desktop %
  1. Use a Unix command to display all of the contents of the desktop.
alex@imac desktop % ls blog led portfolio.pem programs alex@imac desktop %
  1. Use a Unix command to display all of the contents of the /programs directory.
alex@imac desktop % ls programs main.cpp alex@imac desktop %
  1. Use a Unix command to the home directory.
alex@imac desktop % cd alex@imac ~ %
  1. Use a Unix command to display the current working directory.
alex@imac ~ % pwd /Users/alex alex@imac ~ %
  1. Use a Unix command to change to the programs directory contained in the desktop directory.
alex@imac ~ % cd desktop/programs alex@imac programs %
  1. Use a Unix command to create a new file named new
alex@imac programs % ls main.cpp alex@imac programs % touch new alex@imac programs % ls main.cpp new alex@imac programs %

This will cause a new file to display in the GUI:

  1. Use a Unix command to make a new directory newDir with the file new in it.
alex@imac programs % touch newDir/new touch: newDir/new: No such file or directory alex@imac programs % mkdir newDir alex@imac programs % ls main.cpp new newDir alex@imac programs % touch newDir/new alex@imac programs % ls newDir new alex@imac programs %
  1. Use a Unix command to remove the new file from the current directory (/programs):
alex@imac programs % ls main.cpp new newDir alex@imac programs % rm new alex@imac programs % ls main.cpp newDir alex@imac programs %
  1. Use a Unix command to delete the newDir and all of it's contents:
alex@imac programs % ls main.cpp newDir alex@imac programs % rm newDir rm: newDir: is a directory

NOTE: DO NOT PERFORM THIS STEP ON ANY DIRECTORY UNLESS YOU WANT TO LOSE ALL OF THE DATA IN THE DIRECTORY PERMANENTLY. THIS DELETE IS UNRECOVERABLE

alex@imac programs % rm -rf newDir alex@imac programs % ls main.cpp alex@imac programs %
  1. Use a Unix command to display all of the contents of main.cpp
alex@imac programs % cat main.cpp #include <iostream> using namespace std; int main() { cout << "Hello " << "Alex's " << "Blog" << endl; return 0; } alex@imac programs %
  1. Use a Unix command to display the first 10 lines of main.cpp.
alex@imac programs % head main.cpp #include <iostream> using namespace std; int main() { cout << "Hello " << "Alex's " << "Blog" << endl; alex@imac programs %
  1. Use a Unix command to display the last 10 lines of main.cpp.
alex@imac programs % tail main.cpp int main() { cout << "Hello " << "Alex's " << "Blog" << endl; return 0; } alex@imac programs %
  1. Use a Unix command to move from the programs directory to it's parent directory.
alex@imac programs % cd .. alex@imac desktop % ls blog led portfolio.pem programs alex@imac desktop %
  1. Use a Unix command to copy the contents of /programs/main.cpp to the current directory and name the new file new.cpp.
alex@imac desktop % cp programs/main.cpp new.cpp alex@imac desktop % ls blog led portfolio.pem programs new.cpp alex@imac desktop %
  1. Use a Unix command to move /programs/main.cpp to the current directory and keep the original name.
alex@imac desktop % mv programs/main.cpp main.cpp alex@imac desktop % ls programs alex@imac desktop % ls blog led portfolio.pem programs new.cpp main.cpp alex@imac desktop %