$ mkdir [optional arguments] <foldername>
Manipulating Directories
We can now move around a file system in a similar manner to which we would in File Explorer/Finder. But not the only thing we ever want to do with our file system. In reality we are changing items that exist, making new directories and files. This can also be done in command line.
1 Making directories
As our projects grow we want to ensure they are properly organised. We can create a new directory within our current working directory to do so.
The commands are the same in both UNIX-like and Windows flavours.
To create a directory we use the mkdir
command, followed by optional flags and the name of our directory.
If we wanted to create the new folder called “Meetings” we would type:
$ mkdir Meetings
One common option added to this command is the -p
option.
This creates a full path of directories if they do not exist. If we wanted to create the folder “Meetings” with the sub folder “CouldHaveBeenAnEmail” we would use the command:
$ mkdir -p Meetings/CouldHaveBeenAnEmail
To create a directory we use the mkdir
command, followed by optional flags and the name of our directory.
>mkdir [optional arguments] <foldername>
If we wanted to create the new folder called “Meetings” we would type:
>mkdir Meetings
One common option added to this command is the -p
option.
This creates a full path of directories if they do not exist. If we wanted to create the folder “Meetings” with the sub folder “CouldHaveBeenAnEmail” we would use the command:
>mkdir -p Meetings\CouldHaveBeenAnEmail
2 New Files
As well as new folders we can create new files in our directory. This is often useful for making test files. The created files will be empty, but you can choose the file extension (.txt, .csv, .py etc).
To create a file in a UNIX-like CLI within our current directory we use the touch
command, followed by the filename.
$ touch <filename>
The file extension must be included in order to make a functioning file.
For example, if we want to create a new file “material_review.txt” in our current directory we write:
$ touch material_review.txt
We could then check that it has been created in our current directory using:
$ ls
To give us a list of all the folder/files in that directory.
To create a file in a UNIX-like CLI within our current directory we use the echo. >
command, followed by the filename. We will go into more detail as to what the >
part of this command does later. echo
is a versatile command that takes a string input and gives an output. The .
part ensures the file is empty.
>echo. > <filename>
The file extension must be included in order to make a functioning file.
For example, if we want to create a new file “material_review.txt” in our current directory we write:
>echo. > material_review.txt
We could then check that it has been created in our current directory using:
>dir
To give us a list of all the folder/files in that directory.
3 Changing Item Locations
Not only can we create items, we can also change their position, copy them and delete them.
3.1 Move/Rename
The two arguments required to move files from one location to another are the initial filepath and the destination filepath. The filepath includes the name of the file.
This means we can rename files by giving the destination filepath as the same location with a different file name.
Reminder: For relative file paths in UNIX we can either put nothing before the directory name or a ./
, where the .
denotes the current directory.
The command to move a file is mv
. This is followed by the source filepath and then the destination filepath. We can use relative paths for this.
$ mv <source filepath> <destination filepath>
If we were in the root
directory we will create a new file called “supplementaryData.sql”
$ touch supplementaryData.sql
We then want to move it to the ./Projects/DataGathering/
folder.
$ mv supplementaryData.sql ./Projects/DataGathering/supplementaryData.sql
We can see the location of the folder using our structure diagram.
$ find
.
./Documents
./Documents/slides.pptx
./Documents/Tasks
./Documents/Tasks/notes.txt
./Documents/Tasks/plan.txt
./Projects
./Projects/Analysis
./Projects/DataGathering
./Projects/DataGathering/data.csv
./Projects/DataGathering/supplementaryData.sql
If we do not like the name of our file we can change it to extraData.sql
from where we are using:
$ mv Projects/DataGathering/supplementaryData.sql Projects/DataGathering/extraData.sql
Reminder: For relative file paths in Windows we can either put nothing before the directory name or a .\
, where the .
denotes the current directory.
The command to move a file is move
. This is followed by the source filepath and then the destination filepath. We can use relative paths for this.
>move <source filepath> <destination filepath>
If we were in the root
directory we will create a new file called “supplementaryData.sql”
>echo. > supplementaryData.sql
We then want to move it to the .\Projects\DataGathering\
folder.
>move supplementaryData.sql .\Projects\DataGathering\supplementaryData.sql
We can see the location of the folder using our structure diagram.
>tree /F
.
├───Documents
│ │ slides.pptx
│ │
│ └───Tasks
│ notes.txt
│ plan.txt
│
└───Projects
├───Analysis
└───DataGathering
data.csv
supplementaryData.sql
If we do not like the name of our file we can change it to extraData.sql
from where we are using:
>move Projects\DataGathering\supplementaryData.sql Projects\DataGathering\extraData.sql
3.2 Copy
We will at some point want to make copies of files, this can be done in a similar manner to moving files. We will use a copy command followed by the source filepath and then the destination filepath.
The command to copy a file is cp
. This is followed by the source filepath and then the copy destination filepath. We can use relative paths for this.
$ cp <source filepath> <new file destination filepath>
If we were in the root
directory we will create a new file called “new_analysis.py”
$ touch new_analysis.py
We then want make a copy in the ./Projects/Analysis/
folder, we will call it “new_analysis_copy.py”.
$ cp new_analysis.py ./Projects/Analysis/new_analysis_copy.py
We can see the location of the folder using our structure diagram.
$ find
.
./Documents
./Documents/slides.pptx
./Documents/Tasks
./Documents/Tasks/notes.txt
./Documents/Tasks/plan.txt
./new_analysis.py
./Projects
./Projects/Analysis
./Projects/Analysis/new_analysis_copy.py
./Projects/DataGathering
./Projects/DataGathering/data.csv
./Projects/DataGathering/supplementaryData.sql
The command to copy a file is copy
. This is followed by the source filepath and then the copy destination filepath. We can use relative paths for this.
>copy <source filepath> <new file destination filepath>
If we were in the root
directory we will create a new file called “new_analysis.py”
>echo. > new_analysis.py
We then want make a copy in the .\Projects\Analysis\
folder, we will call it “new_analysis_copy.py”.
>copy new_analysis.py .\Projects\Analysis\new_analysis_copy.py
We can see the location of the folder using our structure diagram.
>tree /F
.
│ new_analysis.py
│
├───Documents
│ │ slides.pptx
│ │
│ └───Tasks
│ notes.txt
│ plan.txt
│
└───Projects
├───Analysis
│ new_analysis_copy.py
│
└───DataGathering
data.csv
supplementaryData.sql
3.3 Remove
We will at some point want to delete items, we can do this by specifying the filepath of the file or item we want to remove.
NOTE: data can be lost by using this command, please ensure you have back-ups and are certain about what you want to delete.
The command to remove a file is rm
. This is followed by the filepath of the item to be removed.
$ rm <item filepath>
If we were in the root
directory and we wanted to remove the “slides.pptx” from the “./Documents/ folder.
$ rm slides.pptx
We can see that the file no longer exists in our file structure.
$ find
.
./Documents
./Documents/Tasks
./Documents/Tasks/notes.txt
./Documents/Tasks/plan.txt
./new_analysis.py
./Projects
./Projects/Analysis
./Projects/Analysis/new_analysis_copy.py
./Projects/DataGathering
./Projects/DataGathering/data.csv
./Projects/DataGathering/supplementaryData.sql
By default rm
will not remove directories, but this can be changed by adding in the -r
option, which will recursively remove any files and sub folders from the specified location.
We can remove the folder ./Documents/
and all of it’s contents by executing the following command:
$ rm -r ./Documents/
Warning Data can be lost easily if this is done incorrectly.
We can see this effect.
$ find
.
./new_analysis.py
./Projects
./Projects/Analysis
./Projects/Analysis/new_analysis_copy.py
./Projects/DataGathering
./Projects/DataGathering/data.csv
./Projects/DataGathering/supplementaryData.sql
The command to remove files is del
, and to remove directories is rmdir
. del
can only delete files, whereas rmdir
can remove directories. This is followed by the filepath of the item to be removed.
>del <file filepath>
Without optional arguments rmdir
will only remove empty folders.
>rmdir <folder filepath>
If we were in the root
directory and we wanted to remove the “slides.pptx” from the “." folder.
>del .\Documents\slides.pptx
We can see that the file no longer exists in our file structure.
>tree /F
.
│ new_analysis.py
│
├───Documents
│ └───Tasks
│ notes.txt
│ plan.txt
│
└───Projects
├───Analysis
│ new_analysis_copy.py
│
└───DataGathering
data.csv
supplementaryData.sql
Adding in the /s
option to rmdir
will allow you to recursively remove all files and sub-folders within a specified folder.
We can remove the folder .\Documents\
and all of it’s contents by executing the following command:
>rmdir /s .\Documents\
Which will prompt us with a Yes/No to remove the specified items.
We can suppress this prompt by adding in another option /q
.
>rmdir /s /q .\Documents\
WARNING: Data can be lost easily if this is done incorrectly.
We can see this effect.
>tree /F
.
│ new_analysis.py
│
└───Projects
├───Analysis
│ new_analysis_copy.py
│
└───DataGathering
data.csv
supplementaryData.sql
4 Exercises
The following questions can all be answered with your working directory in “file_system”.
You can check what your command did after each question by writing the find
or tree /F
commands.
Create a new directory within “file_system” called “supp_folder”.
Create a new text file called “additional_info.txt” within “supp_folder”.
Move the file “additional_info” from it’s location to the “Documentation” folder.
Copy “dictionary.json” and paste it within “supp_folder”.
Remove the file “personaldata.sql” from the folders “Data” -> “databases”.
Ensure your working directory is file_system
.
Question 1
Create a new directory within “file_system” called “supp_folder”.
$ mkdir supp_folder
Question 2
Create a new text file called “additional_info.txt” within “supp_folder”.
$ touch supp_folder/additional_info.txt
Question 3
Move the file “additional_info” from it’s location to the “Documentation” folder.
$ mv supp_folder/additional_info.txt Documentation
Question 4
Copy “dictionary.json” and paste it within “supp_folder”.
$ cp Data/text/structured/dictionary.json supp_folder
Question 5
Remove the file “personaldata.sql” from the folders “Data” -> “databases”.
$ rm Data/databases/personaldata.sql
Question 1
Create a new directory within “file_system” called “supp_folder”.
>mkdir supp_folder
Question 2
Create a new text file called “additional_info.txt” within “supp_folder”.
>echo. > supp_folder\additional_info.txt
Question 3
Move the file “additional_info” from it’s location to the “Documentation” folder.
>move supp_folder\additional_info.txt Documentation
Question 4
Copy “dictionary.json” and paste it within “supp_folder”.
>copy Data\text\structured\dictionary.json supp_folder
Question 5
Remove the file “personaldata.sql” from the folders “Data” -> “databases”.
>del Data\databases\personaldata.sql