Basic Unix Commands

Tue Jan 28 2025
Updated: Sun Jul 13 2025
Facebook share linkTwitter/X share linkLinkedIn share linkReddit share linkReddit share link

Unix commands are built in instructions that can be invoked from a Unix terminal. Unix commands are built into Unix and Unix-Like operating systems, and are used to perform very specific tasks which operate on the operating system. 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.

CommandDescription
pwdPrint working directory - Prints the file path to the current working directory.
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>.
cd / cd <path>Change directory - When used alone switches to the home directory. When used with <path> changes from the current working directory to <path>.
touch <path>/<file>Create File - Creates the <file> under <path>. This command will not create directories. This command only works on Unix/Linux/Mac.
rm <path>/<file>Remove File - Removes the <file> under <path>. This command will not remove directories.
rm -rf <path>Remove File - USE WITH CAUTION Removes the directory at <path>. This command will permanently delete the directory and all contents within it.
cat <path>Concatenate - Displays the contents of the file at <path>.
head <path>Display beginning of file - Displays first 10 lines of the file located at <path>.
tail <path>Display end of file - Displays last 10 lines of the file located at <path>.
mkdir <path>Make directory - Makes a new directory at <path>.
cp <path> <destPath>Copy - Copies the file at <path> to <destPath>.
mv <path> <destPath>Move - Moves the file from <path> to <destPath>.

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 %


Terms

  1. Unix Commands - Built in instructions that can be invoked from a Unix terminal.

See Above Table For Common Linux Commands



Questions

  1. Write the command to display the full path of your current working directory.

  2. You are not sure which directory you're in. Write a command to find out.

  3. Write a command to list the files in the current directory.

  4. Write a command to list the files in a directory named documents.

  5. Write a command to list the files in /usr/local/bin.

  6. Write the command to go to your home directory.

  7. Write the command to change into a directory called downloads.

  8. Write the command to navigate from the current directory into ../projects/python.

  9. Write a command to create a file named notes.txt in the current directory.

  10. Write a command to create a file named data.csv inside a folder called output.

  11. Write a command to delete a file named temp.txt.

  12. Write a command to delete a file called old_logs.txt from the logs directory.

  13. Write a command to create a folder named backups.

  14. Write a command to create a nested directory structure archive/2025/july.

  15. Write a command to delete a folder named test that is currently empty.

  16. Write a command to remove the empty folder old_drafts.

  17. Write a command to copy draft.txt into the final directory.

  18. Write a command to copy a.txt and rename it as b.txt.

  19. Write a command to rename draft.txt to final.txt.

  20. Write a command to move report.pdf into the archive folder.

  21. Create a file called newfile.txt and then list the contents of the current directory.

  22. Create a folder called docs and move resume.pdf into it.

  23. Create a file named test.py and then rename it to main.py.

  24. Navigate to a folder called projects and then list its contents.

  25. Delete the file old.txt and then print your current directory path.

  26. Make a directory called code, change into it, and create a file named app.js.

  27. Copy image.png into the images folder, then list the contents of that folder, and then move image.png into archive/images.

  28. Create a file log.txt, move it into a folder called logs, and then print the working directory.

  29. Create a folder drafts, make a file inside it called essay.txt, and then remove that file.

  30. Create project, add a file readme.md, rename it to README.md.

  31. Make a directory portfolio, create index.html inside it, list the directory contents, and rename index.html to home.html.

  32. Create temp, create a file temp.txt inside, move it to another folder archive, and then delete the original folder temp.

  33. Change into scripts, create run.sh, copy it to backup, and print your current directory.

  34. Make folder A, move into it, create a file a.txt, and delete it.

  35. Make folder test, create file t.txt, rename to test.txt, move it to done.

  36. Create a folder named logs, create a file called error.log in it, list the contents, rename error.log to err.log, move it to archive/logs, and display your current path.

  37. Change into codebase, create a file main.cpp, copy it to src, rename it to main_v2.cpp, move it to build, and print current directory.

  38. Make a directory backup, move into it, create db.sql, copy it into archives, delete original, remove backup, return to original directory.

  39. Create classwork, create three files: lab1.txt, lab2.txt, lab3.txt, list the directory, delete lab3.txt, move the rest to submitted.

  40. Create a folder projects/2025, go into it, create idea.txt, rename it proposal.txt, copy to submissions, delete original.

  41. Write the command(s) to make three folders a, b, and c, then place an empty file file.txt in each.

  42. Create a new file in your current directory and then move it to a subdirectory two levels down.

  43. Copy all files from old_projects to new_projects, then list the contents of new_projects.

  44. Navigate to the home directory, make a folder called scripts, enter it, and create a shell script file named start.sh.

  45. Make a directory x/y/z in one command, then add a file to the deepest folder.