Modifying the file tree¶
Learning objectives
Questions
- How do I create or remove files and directories?
- How do I copy or rename files and directories? (You will see why these two operations are mentioned together shortly.)
Learning objectives
- Learn how to create and remove files and directories.
- Learn how to copy and rename/move files and directories.
- Learn to avoid a few common pitfalls that could cause files to be deleted or overwritten by mistake.
Create and remove directories/files¶
This section will show how to work with files and directories through the command line interface (CLI).
Files¶
To create files, you would normally use an editor (nano
, vim
, emacs
, etc.), but you can also create an empty file with the command touch
.
You can remove files with rm
. You can use the flag/option -i
to prompt before removing a file. Be aware that files removed with rm
are deleted permanently—they generally cannot be restored (people have gotten lucky with system backup snapshots, but do not assume that those will be available).
Warning
If you do not add the flag/option -i
the file will be deleted without prompting. Be careful!
Be extra careful using rm -rf
with glob patterns (see Wild Cards under The File System )! It is strongly recommended that you always test a pattern with ls
and check that the output is what you expect before using rm -rf
on that pattern.
Directories¶
mkdir DIR
: Creates a directory DIRmkdir -p DIR/SUBDIR
: creates a directory DIR with the subdirectory SUBDIR
rm -r DIR
: Removes a directory DIR. The flag-r
means recursively.- You can also add
-f
(meaning force). This means ignore nonexistent files and arguments, and never ask before deleting the target. - Again, you can add the option
-i
. This means it will prompt before every removal.
- You can also add
Examples: creating and removing directories
Create a directory called mynewdir
Create a directory called cooldir
which has a subdirectory called fancydir
Remove the directory mynewdir
Examples¶
Reminder
mkdir DIR
: Create a directory DIRrm -rf DIR
: Remove a directory DIR. The flag “-r” means recursively and “-f” means do so without asking for each file and subdirectory. Useful, but dangerous. Be careful!cd
: Go to your home directory ($HOME)cd DIR
: Change directory to DIRcd ..
: Change directory to the parent directory of the current directorycd -
: Go back to the previous working directorytouch FILE
: Create an empty file with the name FILE or update the timestamps of an existing file named FILErm FILE
: Remove the file with the name FILEpwd
: print the current working directory path in full.
Creating directories, changing directories, removing directory and file, removing files by pattern
This example sequence will demonstrate some of the things we just learned, as well as the command cd
and glob patterns from the previous section.
HINT: Code-along!
Create and remove directories:
[x_rebpi@tetralith1 ~]$ mkdir mytestdir
[x_rebpi@tetralith1 ~]$ cd mytestdir/
[x_rebpi@tetralith1 mytestdir]$ mkdir testdir1
[x_rebpi@tetralith1 mytestdir]$ mkdir testdir2
[x_rebpi@tetralith1 mytestdir]$ mkdir testdir3
[x_rebpi@tetralith1 mytestdir]$ ls
testdir1 testdir2 testdir3
[x_rebpi@tetralith1 mytestdir]$ rm -rf testdir3
[x_rebpi@tetralith1 mytestdir]$ ls
testdir1 testdir2
Create and remove files:
[x_rebpi@tetralith1 mytestdir]$ cd testdir1
[x_rebpi@tetralith1 testdir1]$ touch file1.txt
[x_rebpi@tetralith1 testdir1]$ touch file2.sh
[x_rebpi@tetralith1 testdir1]$ touch file3.c
[x_rebpi@tetralith1 testdir1]$ touch file4.dat
[x_rebpi@tetralith1 testdir1]$ rm file4.dat
[x_rebpi@tetralith3 testdir1]$ rm -i file3.c
rm: remove regular empty file 'file3.c'? y
[x_rebpi@tetralith3 testdir1]$ ls
file1.txt file2.sh
Removing files by glob pattern (or why to always test a glob pattern with ls
before using it with rm
):
[x_rebpi@tetralith1 testdir1]$ cd ../testdir2
[x_rebpi@tetralith1 testdir2]$ touch meow.txt
[x_rebpi@tetralith1 testdir2]$ touch catsmeow1.dat
[x_rebpi@tetralith1 testdir2]$ touch homeowners_assoc.odt
[x_rebpi@tetralith1 testdir2]$ ls *meow*
catsmeow1.dat homeowners_assoc.odt meow.txt
[x_rebpi@tetralith1 testdir2]$ rm -r *meow{,1}.??t
[x_rebpi@tetralith1 testdir2]$ ls
homeowners_assoc.odt
Note
This was done on Tetralith. You will notice that only the current (subdir) is shown in the prompt. At some other centres all the (sub)dirs would be shown.
Example: HPC2N
cp - copy files/directories¶
This command is used to copy files or directories.
cp myfile.txt myfile2.txt
: make a copy of “myfile.txt” named “myfile2.txt”cp myfile.txt DIR/
: copy the file “myfile.txt” into the directory DIRcp DIR1/ DIR2/
: copy the directory DIR1 into the directory DIR2 (Note: only works if DIR1 contains no subdirectories)cp -r DIR1/ DIR2/
: copy the directory DIR1 and all subdirectories into the directory DIR2.cp -i file.txt DIR/
: Interactive. It will ask before overwriting if there is a file with the same name.
Warning
If you do not add the option “-i”, you risk overwriting any existing file with the same name.
Code-along
Go to the directory mytestdir
under exercises
directory that you got from the downloaded tarball. This is how the structure looks:
- Change to the subdirectory:
myfile.txt
to the subdirectory testdir1
:
3. Create a new directory called testdir3
inside testdir1
4. Copy the new subdirectory testdir3
to the directory testdir2
. Remember, “testdir2” is located outside “testdir1” and at the same “level”. This can be done in more than one way. Remember you need the option -r
(for recursive) when copying directories:
a) "Go up one" and then copy:
```bash
cd ..
cp -r testdir1/testdir3 testdir2/
```
b) Copy from inside ``testdir1``
```bash
cp -r testdir3 ../testdir2
```
- If you give the full path while copying, this can be done from anywhere.
mv - rename files/directories¶
The command mv
is used to rename files and directories, and to move a file or directory to another location.
mv file1.txt file2.txt
: renamesfile1.txt
tofile2.txt
mv DIR1/ DIR2/
: renames directoryDIR1
to directoryDIR2/
mv file1.txt DIR1/
: moves the filefile1.txt
into the directoryDIR1/
mv DIR1 DIR2/
: (note lack of forward slash afterDIR1
) moves directoryDIR1
into directoryDIR2/
.mv -i file1.txt file2.txt
: interactive. Asks before overwriting if there is already a file with the destination name.mv -i DIR1/ DIR2/
: interactive. Asks before overwriting, if there is already a directory with that name.
Tip
mv
complains if there is already a file/directory with the new name. You can force the renaming with “-f” at the cost of the disappearence of the file that previously held the name.
Exercise¶
Exercise
- Create three files (touch)
- Create a directory and then create a subdirectory of that directory (mkdir, cd)
- Create a file in the subdirectory (touch)
- Create another file inside the directory you created and then move it to the subdirectory you created (touch, cd, mv)
- Rename one of the directories (mv)
- Delete/remove a file (rm)
- Delete/remove the subdirectory (rm)
Solution - click to reveal
- I randomly name the files
afile.txt
,bfile.txt
,cfile.txt
- I make the directory
newdir
and the subdirectorysubdir
- I create a file named
newfile.dat
- I name the file
secondfile.txt
and move it intosubdir
- I rename the first directory (top-level directory) I created, calling it
fancydir
- I remove the file
afile.txt
while working in the directory outside offancydir
(previously callednewdir
)
- I remove the subdirectory
subdir
while outside the directoryfancydir
Tip
You can always check with pwd
which directory you are working in!
Symbolic links¶
A symbolic link is a pointer to another file or directory. Symbolic links are also called soft links, or just symlinks.
Symlinks are useful both for ease—you avoid using a long path each time you change to a directory, like your project directory—and to avoid changing hard links within other scripts or programs. It is good to avoid changing hardlinks if you, for instance, install a program or use a script that assumes the library it uses is called libcoolness.a
and not libcoolness.2.0.a
. You can then just update the symlink instead of renaming the library or updating potentially many instances where it is mentioned in the program.
Command:
Example (on Tetralith)
This creates a symbolic link named “myproj” in your home directory, pointing to the location /proj/linux-intro/users/MYUSERNAME. The directory “linux-intro” is the project storage directory for this course project. For user x_rebpi
, it would look like this:
Summary
- You can create a directory named DIR with
mkdir DIR
- You can remove a directory named DIR with
rm -r DIR
- You can create an (empty) file named FILE with
touch FILE
- You can remove a file named FILE with
rm FILE
- The command to copy files and directories is
cp
- The command to rename files and directories is
mv
- Symbolic links are pointers to another file or directory
- Always test glob patterns with
ls
before using the same patterns withrm -r
to remove files in bulk.