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.
Command | Description | Example |
---|---|---|
pwd | Print 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.
alex@imac desktop % pwd /Users/alex/desktop alex@imac desktop %
alex@imac desktop % ls blog led portfolio.pem programs alex@imac desktop %
/programs
directory.alex@imac desktop % ls programs main.cpp alex@imac desktop %
home
directory.alex@imac desktop % cd alex@imac ~ %
alex@imac ~ % pwd /Users/alex alex@imac ~ %
programs
directory contained in the desktop
directory.alex@imac ~ % cd desktop/programs alex@imac programs %
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:
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 %
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 %
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 %
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 %
main.cpp
.alex@imac programs % head main.cpp #include <iostream> using namespace std; int main() { cout << "Hello " << "Alex's " << "Blog" << endl; alex@imac programs %
main.cpp
.alex@imac programs % tail main.cpp int main() { cout << "Hello " << "Alex's " << "Blog" << endl; return 0; } alex@imac programs %
programs
directory to it's parent directory.alex@imac programs % cd .. alex@imac desktop % ls blog led portfolio.pem programs alex@imac desktop %
/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 %
/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 %