Six-module walkthrough covering navigation, files, reading/searching, processes/editors, scripting, and advanced tools (ssh, regex, tar, etc.). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
4.7 KiB
CLI Part II: Files and Directories
CHEG 667-013 — Chemical Engineering with Computers
Department of Chemical and Biomolecular Engineering, University of Delaware
Key idea
Create, move, copy, and delete files and directories from the command line.
Key goals
- Create directories with
mkdirand remove them withrmdir - Create files with
touchandcat - Move, copy, and remove files with
mv,cp, andrm - Understand that these operations are permanent — there is no trash can or undo
1. Making and removing directories
If we want to add or remove folders to our file hierarchy, we use the following commands:
mkdir— make a directoryrmdir— remove a directory (it needs to be empty)
WARNING! Removing a directory is permanent. There is no trash can or undo! This will apply to files, too.
Now go back to your home directory (cd ~) and make a new folder called cheg667:
$ mkdir cheg667
Verify that you made the directory by using ls. You can remove this directory with the rmdir command.
2. Moving, copying, and removing files
Now we'll learn how to move, copy, or remove files.
mv— move (or rename) a filecp— copy a filerm— delete or remove a file
WARNING! All of these file operations are permanent! There is no undo! Moving or copying a file on top of another will erase or clobber the original. Be careful!
We need some files to work on. Do this in your home directory:
$ touch foobar
The touch command is a way of updating the modification time of a file. If the file doesn't exist, touch will create one. If we use ls -lt, we should see at the top that we created the file foobar, which has zero bytes.
-rw-rw-r-- 1 ef1j ef1j 0 Mar 30 06:57 foobar
Notice the file permissions. The file can be read by the user, group, and other users. Only the user and the group have permission to write to or otherwise modify the file or erase it.
Next, move foobar to the directory ~/cheg667:
$ mv foobar cheg667
If you type ls now, you will not see it. But if you change directory to cheg667 or type ls cheg667, you will find that it was moved to that directory.
Change directories to cheg667 and type the following:
$ cp foobar foo
This copies the file foobar to a new file, foo:
$ ls
foo foobar
Notice that the file modification times are different:
$ ls -l
total 1
-rw-rw-r-- 1 ef1j ef1j 0 Mar 30 07:08 foo
-rw-rw-r-- 1 ef1j ef1j 0 Mar 30 06:57 foobar
3. Creating files with cat
Create another file, but this time with text using the cat command:
$ cat > bar
Terminal whispers,
cat > file.txt begins
empty lines take shape
^D
When you type cat > bar, it is telling the terminal to redirect the standard input of the terminal to a file. (More on this later.) When you hit enter, the input that you type will be written to the file. You can stop by typing control-D. Verify that the text was written to the file, again using the cat command:
$ cat bar
Terminal whispers,
cat > file.txt begins
empty lines take shape.
Using ls, you will also see that the file has a non-zero size:
-rw-rw-r-- 1 ef1j ef1j 67 Mar 30 07:18 bar
Now copy the file bar. Call it bar2:
$ cp bar bar2
Do you see it in the directory listing?
You can remove bar2 with the command:
$ rm bar2
Check the directory listing again. The file should be gone.
4. Creating files with echo
The echo command prints text to the terminal. It's useful for quick output and for writing short content to files using a redirect:
$ echo "Hello, world!"
Hello, world!
$ echo "This is a test" > test.txt
$ cat test.txt
This is a test
Use >> to append instead of overwriting:
$ echo "Another line" >> test.txt
$ cat test.txt
This is a test
Another line
5. Changing file permissions with chmod
In the previous section, we saw that files have permissions (r, w, x) for the owner, group, and others. The chmod command lets you change these permissions. You'll need this when you want to make a file executable, like a script.
The most common use for us is adding execute permission:
$ chmod u+x myscript.sh
Here, u means the user (owner), +x means add execute permission. You can also set permissions for the group (g) or others (o):
chmod g+r file— give the group read accesschmod o-w file— remove write access from otherschmod a+x file— give everyone execute access (a= all)
Exercise 1: Practice making new text files with the
catandechocommands. Rename a file usingmv. Practice copying files. Usemanto learn more aboutmv,cp,rm, andchmod.