The Mac Terminal II: Simple File Operations

TerminalIn the last Terminal Tutorial, I went over the two commands cd and ls, which just allow you to view your files. In this segment of Terminal Tutorials, I will show you how to do some operations with your files. This article will cover 4 commands, allowing you to copy, move and delete files and directories.

Creating Directories

Creating directories is very easy with the command mkdir. The syntax for this command is mkdir <path of new directory>. For example, if I wanted to create a directory called new inside of the beta directory, I would enter

ibook:~/beta Alex$ mkdir new

And if I wanted to make a new directory somewhere else,

ibook:~/beta Alex$ mkdir /path/to/new/directory

Copying and Moving Files

The commands cp and mv, for copy and move, respectively, have very similar syntax, so we will cover them together. Copying is like copy and paste while moving is like cut and paste. For both cp and mv, the syntax is cp <file or directory to be copied/moved> <destination of file or directory>. If I wanted to copy the file test.txt to my desktop as test2.txt, I would use

ibook:~/beta Alex$ cp test.txt /Users/Alex/Desktop/test2.txt

If I wanted to move the file test.txt to the directory above it, I would use

ibook:~/beta Alex$ mv test.txt ..

Removing files

Removing, or deleting files can be done with the rm command. The syntax for rm is just rm <file to be deleted>. You have to be very careful with rm, because this command doesn’t send files to the Trash, where files can be easily recovered, it removes them completely. You’ve been warned!

The asterisk (*) can be used with many unix commands to signify a wildcard. For example, to delete all the files in the beta directory, I would enter

ibook:~/beta/beta Alex$ rm *

The asterisk can be used in any command in the context of files and directories. I defined the double period (..) in the last tutorial, which signifies the directory above. Another similar shortcut is the single period (.). The single period denotes the current directory, so if I wanted to copy the file test.txt to my current directory, I would enter

ibook:/ Alex$ cp /Users/Alex/Desktop/test.txt .

Permissions

Sometimes when you are trying to delete, copy, or move a file or directory, you will get a permission denied error. This means that you were trying to do an operation on a file or directory that did not belong to your specific user. If this happens, and you are sure you still want to complete the command, just preface your command with sudo. Sudo will complete the command with the root user. Root is the administrator account that has full control of your system. When you use sudo to run a command, it will ask you for a password, which will be your admin password on your Mac.

Watch for the next Terminal Tutorial in about a week.

Comments

6 Responses to “The Mac Terminal II: Simple File Operations”

  1. Mikker on March 21st, 2007 8:26 am

    A good extension to the rm command is -R. Recursive. Let’s say you want to delete a directory filled with some files, using # rm crap-directory will only output an error, saying “crap-directory is a directory, and it is not empty”. Now if you write rm -R crap-directory, everything in the dir will be deleted, and so will the dir itself.

  2. Yohannes Wijaya on March 21st, 2007 8:58 am

    The full command rm -Rf /* is just like atomic bomb, it’s so destructive. speaking of command to remove folder, i think rmdir is the most useless command ever

  3. links for 2007-08-03 | pristina.org | everything design on August 3rd, 2007 5:22 am

    [...] The Mac Terminal II: Simple File Operations | MacApper (tags: OSX terminal tutorial Mac) [...]

  4. kalli on December 14th, 2007 6:35 am

    rm -r is useless when I have to examine every file. rm -Rf gives the same result. How can one bypass the ‘examine files in directory’ thing. I just want to delete the whole thing files and subdirecotries and all. Grrr…

  5. Felipe Bedulli on January 16th, 2009 6:49 pm

    Guys, please help me.
    I excluded my terminal, its not on app folder anymore…

    how can i install this again ? thanks !
    i didn’t find it on the MAC DVD`s.

  6. Aidan on January 20th, 2009 6:42 pm

    Thanks Mikker. I have no idea how to use terminal but I had this pesky widget that would not go away no matter how hard I tried to delete it. Your comment allowed me to finally delete it. You made my day,

Feel free to leave a comment...
and oh, if you want a pic to show with your comment, go get a gravatar!