03 Working with Files and Directories - ryandkuster/EPP_575_RNA_25 GitHub Wiki
Working with Files and Directories
We now know how to explore files and directories, but how do we create them in the first place?
In this lesson, we will learn about creating and moving files and directories, using the exercise-data/writing
directory as an example.
Let's see where we are and what we already have. Use pwd
to check the current directory. Navigate to the writing
directory (subdirectory of exercise-data
) if not already there.
cd shell-lesson-data/exercise-data/writing
Let's create a new directory called thesis
using the mkdir
command ('make directory'):
mkdir thesis
ls -F
You can also create multiple directories with subdirectories using the -p
flag. Let's say you want to create a project
directory in the previous directory, with two nested subdirectories data
and results
:
mkdir -p ../project/results ../project/data
If we go back a directory (cd ..
), we now have a new project
directory that contains two subdirectories data
and results
.
Tips for naming files and directories
- Do not use spaces. Use
-
or_
instead. - Do not begin names with a
-
, since many command flags start with this. - Try not to use special characters (*,!, etc), as many have special meanings on the command line. Stick with letters, numbers,
.
,-
, and_
.
You typically want to avoid long, convoluted names, but try to be as descriptive as possible.
Create a text file
Let's go back to our thesis directory and use the text editor nano
to create a new file called draft.txt
.
cd ../thesis
nano draft.txt
Write the following lines of text in draft.txt
:
It's not "publish or perish" anymore,
it's "share and thrive".
To save changes, press Ctrl+O+Return. To quit nano
, press Ctrl+X.
If you want to generate a new file, you can also use the touch
command:
touch draft_2.txt
ls -l
Notice that the size of draft_2.txt
is empty (0 bytes) - this command only creates the file, you need to open it in a text editor for data to be incorporated.
Moving files and directories
Let's change the name of draft.txt
to something more informative using the mv
command ('move'):
mv draft.txt quotes.txt
ls
mv
can also be used to move files into different directories. Let's move quotes.txt
to the writing
directory (one directory back).
mv quotes.txt ..
mv
is very powerful, but can be dangerous in that it can overwrite existing files and lead to data loss. There is an additional flag -i
that you can add to the move command that will ask for confirmation before executing the command.
Copying files and directories
The cp
command works similarly to mv
, but it copies a file instead of moving it.
cp quotes.txt thesis/quotations.txt
Now you have two identical files with two different names! You can also copy entire directories using the recursive flag -r
:
cp -r thesis thesis_backup
ls -F
Removing files and directories
Let's remove the quotes.txt
file from the writing
directory using rm
:
rm quotes.txt
DELETING IS FOREVER. Please be very careful when removing files as recovery is not an option!! Use the -i
flag for extra protection/confirmation.
To remove a directory, you need to use the recursive -r
option (similar to cp
):
rm -r thesis
Operations with multiple files and directories
Oftentimes one needs to copy or move several files at once. This can be done by providing a list of individual filenames, or specifying a naming pattern using wildcards. Wildcards are special characters that can be used to represent unknown characters or sets of characters when navigating the Unix file system.
Let's change into the alkanes
directory:
cd ../alkanes
ls -F
This directory contains six files, all containing the file extension .pdb
. Let's use a wildcard (*) to list files that maintain a specific pattern.
List all files that start with 'p':
ls p*
# or
ls p*.pdb
List all files that end in .pdb
:
ls *.pdb
We can also use the ?
wildcard, which represents one character only.
ls ?ethane.pdb
ls ???and.pdb