Data Operations in UNIX
Learning Outcomes
Navigate and operate UNIX-based command-line operations.
Chapter Learning Outcomes
- Use Unix commands such as
cp, scp, rm, mv, including with options and arguments. - Use
grepwith options to answer file search questions. - Connect to a remote system using
sshand describe its purpose in secure communication - Use useful shortcuts and keyboard commands to manipulate the terminal more quickly.
- Troubleshoot errors when using Unix commands by interpreting terminal output and correcting syntax
Working with Files in the NFS
In E 115, you work in an environment that uses the NFS (NCSU File System). You have full access to your own files but only read access to course-provided files, like those in the E115 Course Locker. That means you must copy these files into your personal space before you can modify them. This section introduces three essential commands for managing files: cp (copy), rm (remove), and mv (move or rename).
This page has examples specifically for the NCSU File System, however, cp, rm, and mv are common commands that you can use from any UNIX-based terminal. You can copy, remove, and move files from a file explorer, too, using the GUI…but one of the reasons to learn these commands is that you can use it to automate copying or renaming hundreds of files. Computers are great at aiding in repetitive tasks! However, far too often, we forget that this applies just as much to our use of the computer as it does to the computations we want our programs to perform. We have a vast range of tools available at our fingertips that enable us to be more productive and solve more complex problems when working on any computer-related problem.
Sections
Commands Quick Reference
| Command | Purpose | Syntax |
|---|---|---|
pwd |
Shows the absolute pathname of the user’s current directory | pwd |
ls |
List contents of a directory | |
cd |
Change directory | |
mkdir |
Create a new directory | |
cp |
Copy a file or folder | |
mv |
Move or rename a file | |
rm |
Remove a file or directory | |
clear |
Clear the terminal screen | clear |
man |
View manual page for a command | |
script |
Start a terminal log file | |
Copy (cp)
eos$ cp source_path destination_path
- Copies a file or folder from one location to another
- Also used to rename files and keeps the original
- Use
-rto copy directories
Example: Copy a file named example.txt from my HOME directory to my ~/MyE115 directory…
- I am in
~/cp example.txt MyE115
orcp example.txt MyE115/newName.txt
- I am elsewhere in the file tree
cp ~/example.txt ~/MyE115
orcp ~/example.txt ~/MyE115/newName.txt
eos$ cp example.txt MyE115/
Example: Copy from the E115 Course Locker
eos$ cp /mnt/coe/workspace/csc/admin/e115/common/lab-3/inlab3.txt ~/
Copy multiple files at once:
eos$ cp source_path1 source_path2 source_path3 destination_path
eos$ cp file1.txt file2.txt ~/destination_folder/
Copy entire directories ( must use -r):
eos$ cp -r directory_path destination_path
eos$ cp -r folder_name/ ~/destination_folder/
Example: Copying a File in the E115 Course Locker, while in your Home Directory
To copy from the /common/assignments/basicunix directory of the E115 Course Locker to your home directory, while you are in your home directory. Reminder that the pathname for the E115 Course Locker is: /mnt/coe/workspace/csc/admin/e115
eos$ cp /mnt/coe/workspace/csc/admin/e115/common/assignments/dracula.txt ~/
OR
eos$ cp /mnt/coe/workspace/csc/admin/e115/common/assignments/dracula.txt ./
Example: Copying Multiple Files
To copy alice.txt and dracula.txt from the basicunix directory to your home directory while you are in the basicunix directory:
eos$ cp ./alice.txt ./dracula.txt ~/

Should I expect an output from the copy command?
No. A successful copy produces no output. If you are receiving output or an error message, you have either typed the directory path incorrectly, or the file does not exist in the specified location.
Secure Copy (scp)
You can transfer files securely between a local machine and a remote host over a network using the scp command, ensuring data integrity and confidentiality through ssh (Secure Shell) encryption.
Note: You’ll want to be on your terminal, but in the local directory (not ssh’d in).
Like cp, the order in scp is scp from to. Without options, scp copies the specified file to the given remote host and directory, or vice versa.
eos$ scp username@remotehost:path localfile
You can also use it to upload files to the remote.
eos$ scp localfile username@remotehost:path
-r is required for copying directories. Using the -r option allows you to recursively copy entire directories, preserving the directory structure.
eos$ scp -r username@remotehost:path localdir
Or from local to remote.
eos$ scp -r localdir username@remotehost:path
After executing the command, the file will be present in both the source and destination locations.
Output of scp
file.txt 100% 1234KB 2.3MB/s 00:00
directory/file.txt 100% 1234KB 2.3MB/s 00:00
Example (with a Terminal)
This method may be faster than using FileZilla/Cyberduck if you are having trouble using those programs. Note that FileZilla uses sftp, which is a secure way to transfer files and other file operations, like creating directories, removing files, and changing file permissions, which scp does not support.
But for E 115 we mostly need to copy files over. Here’s how it works on eos. Let’s suppose I have a file called file2.txt in my MyE115 directory on the NFS. I would like to copy it to my local (so I can then upload it to Moodle or Gradescope!).
Then you can copy the file into your local Documents like this:
eos$ scp unityid@remote.eos.ncsu.edu:/mnt/ncsudrive/u/unityid/MyE115/file2.txt ~/Documents/.
Note that this won’t work with PuTTY, since you don’t have a “local” terminal in the same sense as on Mac/Linux. You may try PuTTY pscp (instructions here).
Note: These instructions are only provided as an alternative to using FileZilla and are not required for the class!
Removing Files with rm
Use the rm (remove) command to delete files or folders from your NFS space. Deleted files are permanently removed, so be careful when using this command!
Remove files or folders: rm
Command:rm
- Used to delete files or folders from the system
Removing files and folders: an example
I am in ~/ and so is file.txt and folder
Files
rm file.txtorrm -f file.txt
Folders
rm -rf folder
I am elsewhere in the file tree and file.txt and folder are in ~/
Files
rm ~/file.txtorrm -f ~/file.txt
Folders
rm -r ~/folder
Depending on the permissions of the file, rm does not ask for confirmation before deleting. You can force deletion without prompting for confirmation by using -f. Be careful when using –f!
eos$ rm [options] filename
Common Options
| Option | Description |
|---|---|
-r | Remove directories recursively |
-f | Force deletion without confirmation |
-rf | Delete folders and contents without prompts |
To delete a folder and all its contents:
eos$ rm -rf old_folder
Moving or Renaming with mv
Command:mv
eos$ mv source destination
- Used to move a file from one location to another
- Also used to rename files and deletes the original
- Cannot be used within the E 115 course locker. Students lack the permission for it.
Examples
I want to move a file named example.txt from my HOME directory to my ~/MyE115 directory…
- I am in
~/mv example.txt MyE115/newName.txtdo an ls after to ensure proper moving
- I am elsewhere in the file tree
mv ~/example.txt ~/MyE115/newName.txtdo an ls after to ensure proper moving
Example: Rename a File
How would you rename the file oldstuff.txt to junk.txt while in your home directory?
eos$ mv oldstuff.txt Junk.txt
Example: Move a File
How would you move the file assignment from your home directory to a directory called MyE115?
eos$ mv hw3.txt MyE115/
Exercises
- Here are a few useful tools:
ping: Test whether a computer is reachable on the network.curl: Download a file or webpage from a URL.find: Search for files by name, type, or modified time.
These aren’t required for this course, but you may use them in your other courses.
2. Moving and copying
If the hypothetical file books.dat is the only one in your current directory, what is the output of the ls command?
