Modifying the file tree¶
Learning objectives
Questions
- How do create and remove files and directories?
- How do I copy/rename files and directories?
Learning objectives
- Learn how to navigate the Linux file system
- Learn about files and directories
- Learn about paths
- Be able to create and delete files and directories, as well as rename and copy them
Create and remove directories/files¶
This section will show how to work with files and directories through command line interface.
Directories¶
- mkdir DIR: Create a directory DIR
- mkdir -p DIR/SUBDIR: create a directory DIR with the subdirectory SUBDIR
- rm -r DIR: Remove a directory DIR. The flag “-r” means recursively
- You can also add “-f”. This means ignore nonexistent files and arguments, and never prompt.
- You can add the option “-i”. This means it will prompt before every removal.
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
Files¶
To create files, you would normally use an editor (nano
, vim
, emacs
, etc.), but it is also possible to create an empty file with the command touch
.
You can remove files with rm
. Again, you can use the flag/option -i
to prompt before removing a file.
Warning
If you do not add the flag/option “-i” the file will be deleted without prompting. Be careful!
Examples¶
Reminder
- mkdir DIR: Create a directory DIR
- rm -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 DIR
- cd ..: Change directory to the parent directory of the current directory
- cd -: go back to the previous directory
- touch FILE: create an empty file with the name FILE
- rm FILE: remove the file with the name FILE
- The command
pwd
tells you the current directory path.
Creating directories, changing directories, removing directory and file
This example will test some of the things we just learned, as well as the command cd
from the previous section.
HINT: Code-along!
[x_birbr@tetralith1 ~]$ mkdir myowntestdir
[x_birbr@tetralith1 ~]$ cd myowntestdir/
[x_birbr@tetralith1 myowntestdir]$ mkdir testdir1
[x_birbr@tetralith1 myowntestdir]$ mkdir testdir2
[x_birbr@tetralith1 myowntestdir]$ mkdir testdir3
[x_birbr@tetralith1 myowntestdir]$ rm -rf testdir3
[x_birbr@tetralith1 myowntestdir]$ cd testdir1
[x_birbr@tetralith1 testdir1]$ touch file1.txt
[x_birbr@tetralith1 testdir1]$ touch file2.sh
[x_birbr@tetralith1 testdir1]$ touch file3.c
[x_birbr@tetralith1 testdir1]$ touch file4.dat
[x_birbr@tetralith1 testdir1]$ touch file5.txt
[x_birbr@tetralith1 testdir1]$ rm file5.txt
[x_birbr@tetralith1 testdir1]$
[x_birbr@tetralith1 testdir1]$ cd ..
[x_birbr@tetralith1 myowntestdir]$ cd testdir2/
[x_birbr@tetralith1 testdir2]$
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 DIR/: copy the file “myfile.txt” to the directory DIR
- cp DIR1/ DIR2/: copy the directory DIR1 into the directory DIR2 (Note: overwrites existing files with same name)
- 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 an existing file, if it is named the same.
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:
b) Copy will standing inside testdir1
5. 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. It can also be used to move a file or directory to another location.
- mv file1.txt file2.txt: renames
file1.txt
tofile2.txt
- mv DIR1/ DIR2/: renames directory
DIR1
to directoryDIR2/
- mv file1.txt DIR1/: moves the file
file1.txt
into the directoryDIR1/
- 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.
Note
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 am randomly naming the files
afile.txt
,bfile.txt
,cfile.txt
- I am naming the directory
newdir
and the subdirectorysubdir
newfile.dat
4. I am naming the file secondfile.txt
5. I will rename the first directory (top-level directory) I created, calling it fancydir
6. I will remove the file afile.txt
while standing “above” the directory fancydir
(previously called newdir
)
7. I am removing the subdirectory subdir
while standing above the directory fancydir
Tip
You can always check with pwd
that you are standing in the directory you think you are!
Symbolic links¶
Symbolic links are also called soft links, or just symlinks. It is a pointer to another file or directory.
- It is useful both for ease
- you avoid using a long path each time you change to a directory, like your project directory
- as well as to avoid changing hard links within other scripts or programs. This is good 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 me, this would look like this:
Keypoints
- You create a directory named DIR with
mkdir DIR
- You 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