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

36 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!

  22. proposed 28th amendment on April 27th, 2012 1:24 pm

    very useful article for Mac user. Here I know more about copping, moving, and removing files. proposed 28th amendment I just want to delete the whole thing files and subdirecotries and all

  23. term limits for congress on May 26th, 2012 8:15 am

    Thank you! this post is true cool.Its been a while have waiting for this content material. term limits for congress

  24. Lazaro Greenough on June 22nd, 2012 3:08 pm

    of course like your website but you need to test the spelling on several of your posts. Many of them are rife with spelling problems and I to find it very bothersome to inform the reality however I will certainly come back again.

  25. chemise boss homme on April 11th, 2013 7:42 pm

    Hello i am kavin, its my first occasion to commenting anywhere, when i read this paragraph
    i thought i could also create comment due to this sensible piece of writing.

    Also visit my web page :: chemise boss homme

  26. chemise lacoste on April 11th, 2013 8:43 pm

    Hey there! This is my first comment here so I just wanted
    to give a quick shout out and say I genuinely enjoy reading through your blog posts.

    Can you recommend any other blogs/websites/forums
    that cover the same topics? Thank you so much!

    Look at my site – chemise lacoste

  27. chemise boss homme on April 12th, 2013 12:36 pm

    Thank you, I have recently been looking for information
    about this subject for a long time and yours is the greatest I’ve found out till now. However, what about the bottom line? Are you sure about the source?

    My blog: chemise boss homme

  28. chemise dolce gabbana homme on April 12th, 2013 2:59 pm

    Magnificent beat ! I would like to apprentice whilst you amend
    your site, how could i subscribe for a weblog web site?
    The account helped me a appropriate deal. I have been tiny bit acquainted of this
    your broadcast offered vibrant transparent concept

    Look into my web site :: chemise dolce gabbana homme

  29. ralph lauren homme on April 17th, 2013 1:26 am

    Hello there! This post could not be written any better! Reading through this post reminds me
    of my previous room mate! He always kept chatting about this.
    I will forward this write-up to him. Fairly certain he will
    have a good read. Thanks for sharing!

    Look at my web page: ralph lauren homme

  30. chemise ralph lauren outlet on April 18th, 2013 4:59 pm

    This page definitely has all of the information
    and facts I needed about this subject and didn’t know who to ask.

    Here is my webpage chemise ralph lauren outlet

  31. Custom-Fit Polo Ralph Lauren Femme on April 18th, 2013 6:02 pm

    I go to see day-to-day a few websites and blogs to read articles or reviews, but this website provides feature based
    content.

    Here is my web page :: Custom-Fit Polo Ralph Lauren Femme

  32. chemise boss on April 23rd, 2013 7:18 pm

    Hey there, You have done a fantastic job. I’ll certainly digg it and personally suggest to my friends. I’m sure they will be benefited from
    this web site.

    My web-site; chemise boss

  33. chemise gucci homme on April 28th, 2013 6:07 pm

    What i don’t understood is in fact how you’re no longer actually much more
    well-appreciated than you may be right now.
    You are so intelligent. You know thus significantly relating to this subject, made me individually consider it from
    a lot of varied angles. Its like women and men are not fascinated until it’s one thing to accomplish with Woman gaga! Your individual stuffs great. At all times take care of it up!

    Here is my web page … chemise gucci homme

  34. chemise dolce gabbana homme on May 1st, 2013 10:44 am

    Great blog here! Also your web site loads up very fast!
    What host are you using? Can I get your affiliate link to your host?
    I wish my web site loaded up as quickly as yours
    lol

    Here is my blog post chemise dolce gabbana homme

  35. used cars value on June 17th, 2013 4:01 pm

    You are so cool! I do not think I have read through a single thing like this before.
    So nice to find somebody with some genuine thoughts on this
    issue. Really.. many thanks for starting this up.
    This site is one thing that’s needed on the internet, someone with some originality!

  36. ????? - www.77mscore.com on February 18th, 2014 8:22 am

    Every weekend i used to pay a quick visit this web page, for
    the reason that i want enjoyment, since this this
    site conations truly pleasant funny stuff too.

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