User Tools

Site Tools


wiki:make2016

Making & Removing

Making Directories

We can make a new directory using the command mkdir (MaKe DIRectory):

mkdir <newdirectoryname> …

;-) Quick Tip: The … means that you can add either one or more newdirectorynames.

:!: Exercise: Let's make a new directory called “mynewdir”:

$mkdir mynewdir
$ls

Removing Directories

We can remove empty directories using rmdir (ReMove DIRectory):

rmdir <directoryname> …

:!: Exercise: Let's remove the “mynewdir” directory:

$ls
$rmdir mynewdir
$ls

:!: More Exercises:

  1. Through the terminal, navigate to the place in your computer where you want to store files for this class. If you haven't already made a directory specifically for this class, use mkdir to make that directory.
  2. Navigate inside your new course directory.
  3. Make some subdirectories for the course like “Assignments”, “Notes”, “Exercises”.
  4. Check out your subdirectories using ls
  5. Hmm, maybe those subdirectories don't sort the way you want them to. Delete the directories “Assignments”, “Notes', and “Exercises”.
  6. You can add multiple directories by listing new directory names after mkdir separated by spaces:
  7. mkdir dir1 dir2 dir3
  8. Using one mkdir command, make the directories “01_Notes”, “02_Exercises”, “03_Assignments”.
  9. Now ls to see the subdirectories.

Making Files

Files are documents that live within directories. All files in the Linux environment should follow some naming standards…

  • NO spaces
  • NO weird characters (A-Z, 0-9, _, - are OK)
  • Should contain a file extension (.txt, .docx, .png, etc)

There are many ways to make a new file. We'll cover just a few:

Making files with touch

We can make a new file with touch:

touch <filename.txt>

:!: Exercise: Let's navigate into your directory 01_Notes and make the file quick_tips.txt.

$touch quick_tips.txt
$ls
$ls -alh
$less quick_tips.txt #peek into the file quick_tips.txt

Well, that's a pretty boring file. Let's add some content to it using the linux text editor called nano. This command will be different than previous commands we've executed. Instead of spitting something out to the screen below the prompt, nano will take us to a little text editor app within the terminal where we can type in some text. To exit out of nano, type CTRL+X. To save, type y.

:!: Exercise: Let's add content to quick_tips.txt using nano:

$nano quick_tips.txt
  • Type in some content.
  • Remember, to exit out of nano, CTRL+X.
  • To see what happened, let's look in the file using less.
$less quick_tips.txt

Making files with nano

We can also make new files by skipping touch and just starting up nano directly.

:!: Exercise: Let's make the new file common_pitfalls.txt.

$nano common_pitfalls.txt

We will learn other ways to create new files in future lessons, too.


Removing Files

We can remove files using rm (ReMove).

rm [-i] <filename.txt>

-i is an option for to run rm interactively. It requests a confirmation to remove. Please get in the habit of using this option.

m( Common pitfall: THERE IS NO UNDO IN LINUX. Yep, that's right. If you remove a file, it's gone. There's no trash can or recycle bin you can pull that file out of. It is gone-gone.

;-) Quick tip: Always make sure you have a good backup system in place. A good back up system is automatic.

Let's say we don't want the file common_pitfalls.txt anymore.

$ls
$rm -i common_pitfalls.txt
$ls

:!: More Exercises:

  1. Navigate to the course directory. If you execute the ls command, you should see the directories “01_Notes”, “02_Exercises”, “03_Assignments”
  2. Navigate into 02_Exercises.
  3. Create a new file with today's date somewhere in the filename and a '.txt' file extension.
  4. Copy and paste the instructions for this exercise into the new text file.
  5. Use less to peek into your new text file.
  6. Use rm -i to delete your newly made text file.

;-) Quick tip: When using less to browse a short file, the prompt will return immediately. However, if your file is long, use spacebar to scroll through the pages and q to escape back to the prompt.


Removing directories and their contents with rm

We can also remove directories AND all their contents using rm:

rm [-ir] <directoryname>

  • -i interactive. request confirmation to remove.
  • -r recursive. remove entire directory and contents.
  • <directoryname> the name of the directory you want to remove

Always specify -i because there is the potential for deleting more than you bargained for.

m( Common pitfall: Until you are at Linux ninja status, please use -i with all your rm commands. Please use caution when using rm. Also, please have a good backup strategy in place as well.

Continue to Copy & Move

wiki/make2016.txt · Last modified: 2016/08/24 20:32 by erin