Introduction To Unix and Linux File and Directory Operations
Introduction To Unix and Linux File and Directory Operations
info
Notice the command prompt has changed. Your username is the same
but the unique identifier for your Codio server has changed. Each
assignment has its own server address.
Creating directories and files
info
mkdir test
Now if you type ls or ls -a you will see the directory you created:
touch file1.txt
Notice that you can have multiple file names on the same command line.
The same is true for mkdir, you can make multiple directories in the
current directory at the same time.
If you type ls or ls -a you will see the new files you created.
touch test/file4.txt
ls -R
The -R means recursive so it will list out everything in the current directory
and everything in the directories below the current directory:
Deleting files and directories
There are two main commands for deleting files and directories.
rmdir test
mkdir test2
ls
rmdir test2
ls
In the two printed contents from the ls commands, you should be able to
see the appearance of test2 and then the disappearance of test2.
rm test
Your result should look like this:
Let’s take a closer look at the usage information for the rm command.
man rm
Read about the -r flag and the -i flag. Remember, you close the manual by
pressing the q key.
info
Let’s try that again, we’ll use the recursive option and also add the -i
parameter to prompt us. When you do a recursive delete, you are in danger
of deleting unexpected items.
Type:
rm -R -i test
The results should look as follows, a somewhat tedious, but safe, deletion of
a directory and all its contents:
Wildcards
We’ll take a brief pause from our discussion about Bash file commands to
talk about wildcards since they are useful in conjunction with file
commands.
We’ll cover the main three wildcards (see table below), but if you would
like to learn more, you can take a look in the GNU/Linux Command-Line
Tools Summary.
Wildcard Description
* represents any number of characters
? represents any single character
[ ] represents a range, can be [1-3] or [1,2,3]
mkdir test
mkdir test1
mkdir test2
mkdir test3
mkdir testrandom
ls test?
You only get the files that start with test and are followed by a single
character.
Try these variations:
Solution
Use the ? wild card to ensure there is at least one character after test.
ls test?*
Solution
ls test[1,2]
ls test[1-2]
Solution
Since it’s not a range, this one needs the two values you are looking for
separated by a comma.
ls test[1,3]
If you were viewing a directory for the first time, you might first check to
see if there are any readme or text files.
ls readme*
ls *.txt
ls *.c
Moving, copying and viewing
In addition to creating and deleting files and directories, you might need to
move, rename, or copy them.
mkdir code
man mv
mv *.c code
We’ll use the tree command to see if everything is where we want it.
tree
As a last step, we’ll clean up some of the clutter in this workspace directory:
rm *.obj
rm *.exe
tree
You should see something like the image below:
mv readme.txt README.txt
man cp
Let’s see how the cp command differs from the mv command. Earlier we
renamed the file readme.txt to README.txt.
We’ll start with an lsto see what’s in the directory right now and end with
an ls to see what has changed.
ls
cp README.txt readme.txt
ls
Now you have two copies of the readme file, each with their own name.
Try this:
cp -R code backup
tree
A lot happened! The backup directory was created, and the contents of the
code directory was copied to the backup directory.
Summary
Commands we covered in this assignment:
| Command | Description |
|———|———-|
| cp source destination | Makes a copy of a file or directory |
| mkdir name | Creates a directory |
| mv source destination | Moves a file or directory |
| rm | Removes files and directories |
| rmdir name | Removes empty directories |
| touch name | Creates a new empty file named name |
| tree | Displays the contents of a directory in a tree structure |