Skip to content

Terminal: make it, move it, lose it, …

Last time we looked at how to move around your file system using terminal with cd and ls. Today we are going to look at how to interact with the content you find using those commands.

On a quick side note: a very important command is man, which is short for manual. Type man <command you are interested in>; and get info on what it does, how it works and what options you have available. So let’s try man ls:

manPage_ls
Now you can explore all the possible options for personalizing your ls command. Type f to advance a page or b to jump back a page, q to quite and return to your terminal session. Try man man for info on the manual itself.

The first thing you might want to do with a file is looking at its content. We already looked at the open command last time to launch an application like TextEditor. There are several other options if you want to stay within the terminal application. Terminal has a built in text editor called nano, which you can use to create as well as edit text files. So let’s look at a simple text file using nano: nano mySimpleText.txt

nanoSimple

If the file doesn’t exist in your pwd, then nano will use this as the default name when you are trying to save. There are two important keyboard short-cuts to know for nano: ctr-o (save current file) and ctr-x (close nano).

nano_commandO

The other option for quickly looking at at text file without the option of editing it is to display it in the terminal window. For this we can use the cat (concatenate) command. Type

cat mySimpleText.txt:

cat_simpleText

The contents of the entire file will be displayed in the terminal. To stop terminal from displaying more than one window’s worth of text we can write:

cat myLongText.txt | less

This will fill a screen, stop and only advance if you use the same keyboard shortcuts as we have seen with the man command (f, b, q). (The | is a pipe, which redirects the output of cat to the input of less… More about that in a future post). Addendum: I realize that you could of course just do less myLongText.txt. I am leaving the version with the pipe from above in just to have an example of it here.less_redirect

cat|less_p1 cat|less_p2 cat|less_p3END

Let’s get back to working with files. Now that we know what’s in them we might want to organize them. For this we have the mv (move) command. This is equivalent to dragging your file in the finder to another location. You are moving it without copying it. The syntax here is: mv current/location/filename new/location/ (I am using the / character to show that a file path might be necessary). Let’s try moving the mySimpleText.txt to the Desktop (I’m using full file paths for demonstration purposes but since my pwd is where the file is currently located, I technically don’t have to):

mv Users/martinritter/mySimpleText.txt Users/martinritter/Desktop/

Now navigate to the desktop using cd and display the content with ls:mv_toDesktop

(You should also confirm that the file was really moved and is no longer present in the previous directory).
It is just as easy to copy a file to a new location using the cp command. As an exercise try and copy the file back to the original location using cp and the same syntax as we saw with mv

By the syntax of the mv command you might have guessed by now that there is another, very powerful feature. What would you say the result of the following should be:

mv myfile.png myBetterNamedFile.png

Since we are moving the file into the same directory as it is already in, and we are providing a different name, we are actually just renaming the file in place. If we were to provide a different path AND a new filename we would move it to a new location and rename it at the same time. A word of caution, be sure you get the file extension correct. There is no check to make sure it is valid…

mv_rename

It might be a good idea to test and practice these commands with files that aren’t necessarily important. Specially with the commands we are about to talk about that let you delete files and directories! (Don’t practice on your only copy of your doctoral thesis document… There is no garbage collection and no undo). If you have paid close attention to the screenshots you have already seen the command…

There are two ways to quickly create files you can use for testing these commands. One is touch, which creates an empty file of the specified name and extension. The other is mkfile, which will do the same but with the difference of allocating space for the file as well. So a file created with touch will always be 0 bytes, whereas mkfile can be any size that will fit on your hard drive.

touch empty.txt

touch_empty

mkfile -n 2g large.txt

mkfile_2g

Since we are only trying out other commands, rather than, let’s say testing connection speeds of a network, we will stick with touch.

Now that we have some files we can play with, let’s delete them. rm large.txt (no need to keep a useless 2gb large file around)

Check the contents of your folder: ls

rm_large

And it’s gone.

How do we make a new directory you ask? mkdir directory-name of course. So let’s create one and add some files to it:

mkdir myAwesomeStuff
cd myAwesomeStuff
pwd
touch bestIdeaEver.txt nextBestIdeaEver.txt stillPrettyAwesomeIdea.txt
ls

mkdir_1

We created a new directory, moved into it, confirmed our location, and created 3 separate text files. Then we confirmed with ls that we really did what we were trying to do.

Awesome! Now let’s assume you need to delete your folder of awesome stuff. The remove directory is your friend. Write rmdir myAwesomeStuff (make sure you are in the parent directory):

rmdir_error

This threw an error, which luckily is pretty self explanatory. You have files in the folder and rmdir only can handle empty folders. Now the hard way of deleting the folder and content would look like this (pwd is myAwesomeStuff/):

rm bestIdeaEver.txt
rm nextBestIdeaEver.txt
rm stillPrettyAwesomeIdea.txt
cd ..
rmdir myAwesomeStuff

cd .. Is a convenient way of moving up one directory in the directory tree. In other words, using cd .. will get you back to the parent where your current folder is located in.

This will work but takes to much effort. Slightly better, but still not ideal is this version (assuming you are in myAwesomeStuff/):

rm *.txt
cd ..
rmdir myAwesomeStuff

The * is a wildcard and anything will match it (more details in a later post). Here we are saying that anything that ends with .txt should be removed. 

The best, and most concise way of removing a directory is the following: make sure you are in the parent directory (when you ls you should see the folder to be removed, or provide a full path):

rm -r myAwesomeStuff

rmdir_correct

The -r option stands for recursive, which means remove all contents in the folder recursively and then delete the empty folder as well (see man page for more info).

 Next time we will look at what an alias is and how we can us them to personalize the terminal experience. 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.