The Mac Terminal II: Simple File Operations
In 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.





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.
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
[...] The Mac Terminal II: Simple File Operations | MacApper (tags: OSX terminal tutorial Mac) [...]
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…
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.
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,
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
como recupero el password del terminal
Hey. Got a question here. How do i remove a directory instead of just one little document?
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.
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,
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?
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
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?
I want some tutorial on that particular topic because I am not able to catch your coding.
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.
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!
Cool post it’s truly. Friend on mine has long been awaiting just for this content material.
it’s easy … use “\” just before rm -rf
Good post! It’s been awhile since I’ve used this.
Nice info – thanks for sharing this Mac Terminal info!