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

21 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,

  7. weeds on April 26th, 2010 9:25 pm

    Great tips! I haven’t used the mac terminal before and your site has been a great experience for me, I used the mkdir code to make new directories

  8. raul on May 5th, 2010 9:45 pm

    como recupero el password del terminal

  9. Mike on June 1st, 2010 5:32 pm

    Hey. Got a question here. How do i remove a directory instead of just one little document?

  10. Raphael on December 19th, 2010 12:10 am

    I just wanted to remove a directory filed with more than 1,000 files and using rm -dir is so exhaustive because I would need to say Yes for each file in it. Thanks Mikker for your post. Using rm -R deleted everything.

  11. Tech Reviews on December 19th, 2010 1:56 am

    I used the mkdir code to make new directories. The full command rm -Rf /* is just like atomic bomb, it’s so destructive. using # rm crap-directory will only output an error, saying “crap-directory is a directory,

  12. idrive on February 9th, 2011 4:37 pm

    Hi Guys, I just found a pretty nice site for unlock / jailbreak/ iphone 3g 3gs and iphone4 jailbreak.

    http://www.anysim.net

    Thank u Anysim Team for fast Support. :)

    Did everybody now when the update ios 4.3 for iphone4 comes?

  13. loriee on March 7th, 2011 1:17 pm

    The iPad has been presented this week and alongside its introduction, iOS 4.3 made its appearance and will be shipped with the new iPad. Apple also announced that the iOS 4.3 software update will be available on the same day the new iPad will be on Apple Store tables and shelves: March 11. As you may know, iOS 4.3 will present a number of new features such as iTunes Home Sharing and possibly the most anticipated feature, the Personal Hotspot.

    http://privatecams.de.tl
    http://www.bmw-eba.de.tl
    http://www.unlock-com.de.tl

  14. Ronnie on March 17th, 2011 7:55 am

    I have a file on a Time Capsule which starts with 3 dots “…” (don’t know how this happened) – I can’t delete or rename it.

    If I try to use rm on the directory above it says “Directory not empty” even when I use the rm -r command.

    Can anyone help how to delete – maybe there is another good file removal tool?

  15. iPhone Applications Maker on June 9th, 2011 6:32 am

    I want some tutorial on that particular topic because I am not able to catch your coding.

  16. idrive on August 21st, 2011 4:29 pm

    The folks over at Tapbots have released an update to their Twitter client for the iPhone and iPod touch. http://www.itube-stream.de.tl This update adds a couple of useful features. One of these features gives you the ability to add sleep time for its push notifications. This allows you to set a block of time to turn off notifications from Twitter so that you can sleep easy and uninterrupted. Another useful feature is the ability to only receive notifications from people that you follow. Thirdly, have you ever reported a spammer or blocked someone and had to live with the fact that their tweets were still polluting your timeline? http://www.anysim.net Well fret no mo because once you block or spam someone with this new update, their tweets will be automatically removed.

  17. Wanetta Windmeyer on September 20th, 2011 8:26 pm

    Greetings from Colorado! I’m bored at work so I decided to browse your blog on my iphone during lunch break. I love the information you present here and can’t wait to take a look when I get home. I’m shocked at how quick your blog loaded on my mobile .. I’m not even using WIFI, just 3G .. Anyways, superb site! Thank you!

  18. Buying Gold And Silver on October 15th, 2011 4:21 pm

    Cool post it’s truly. Friend on mine has long been awaiting just for this content material.

  19. Farhad on November 16th, 2011 6:46 am

    it’s easy … use “\” just before rm -rf

  20. austin skylight on December 27th, 2011 1:50 pm

    Good post! It’s been awhile since I’ve used this.

  21. security officer San Antonio on December 30th, 2011 5:01 pm

    Nice info – thanks for sharing this Mac Terminal info!

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