The Terminal: Messin’ With Files and Directories

I’d like to firstly apologise for neglecting this blog a bit. I know I had some posts up, left for a while, came back , posted a few more, and left again. I’m going to really start trying to work more steadily on this blog.

I know that my last post was talking about my belated first impression of Vista, but I’ve decided to not go in that direction. The web is filled with talk of Vista. I’m not a Vista user, and my use of the OS is mainly from that one night’s use of it. It was ok, but nothing to write home about. I’ve heard that Vista really got a lot of it’s major issues taken care of in Service Pack 1, but I can’t vouch for it personally. I won’t rip on it because there are numerous other posts on blogs and forums across the web bashing Vista. I will have some talk of Windows 7 in the future though. I’ve played around with the Beta, downloaded the RC, but haven’t really delved into playing with it yet. From what I’ve seen with the beta, it was very nice, easy to use, and would probably be very popular. Most everyone I’ve talked to that’s used it likes it, so that’s a good sign. It truly is what Vista should’ve been. Either way, I’m not here to talk Windows, or even talk desktop stuff right now. Right now we’re in to our Terminal tutorial series, so let’s keep that rolling, shall we?

Today, we’ll be looking at manipulating files and directories. We’ll be working with cp, mv, rm, and mkdir, with a special bit on wild cards.

Let’s get started with the cp command. It’s a very basic, very fundemental tool, but it’s very useful too. In it’s simplist form, the cp command will copy the contents of a file into another file. For my example, I created an empty file using the touch command. The file testfile has the word data. With my example, I’ll use cp to create a new identical copy of testfile called testfile2.

andrew@main-machine-ubuntu:~$ cat testfile
data
andrew@main-machine-ubuntu:~$ cp testfile testfile2
andrew@main-machine-ubuntu:~$ cat testfile2
data

I can also use the cp command to copy multiple files into a seperate directory.

andrew@main-machine-ubuntu:~$ cp testfile testfile2 dir1
andrew@main-machine-ubuntu:~$ ls dir1
testfile testfile2

The cp command also uses certain flags, like numerous other commands. This flag, -i, basically stands for “interactive”. If we replace “cp -i” with cp from our original example, we’ll be asked by the shell if we’d like the data in the file we’re copying to to be overwritten.

andrew@main-machine-ubuntu:~$ cp -i testfile testfile2
cp: overwrite `testfile2′?

Our next stop on this train is the mv command. It’s short for move, which is obviously used to move files around the system. The mv command can be used in two different manners. It can either move files and directories to a different directory, or it can rename a file or directory. If you’re just trying to move a file, it’s pretty straight forward.

andrew@main-machine-ubuntu:~$ mv testfile3 dir1
andrew@main-machine-ubuntu:~$ ls dir1
testfile testfile2 testfile3

Now, lets use the mv command to rename testfile3 to testfile4.

andrew@main-machine-ubuntu:~/dir1$ mv testfile3 testfile4
andrew@main-machine-ubuntu:~/dir1$ ls
testfile testfile2 testfile4

You can also move directories, but you have to create the destination dirctory before moving the moving directory. For instance, if you try to move dir1 to dir2, and dir2 doesn’t exist, then dir1 will be renamed dir2. The mv command also uses the -i tag, just like cp.

Another simple tool is rm. This little command is very powerful and should be used with caution. It’s used to remove files and/or directories. If used carelessly, you could single-commandedly (you get my point) hose your system. It’s really simple to use too. Just a simple rm, the filename, and BAM. Done.

andrew@main-machine-ubuntu:~$ rm testfile

Like many other commands, rm has various flags too. The main two I’ll talk about today are -i and -r. The -i flag is just like it is for cp. It’s interactive. If used, the system will ask you if you’re sure you really want to delete this file. It’s a good idea to use this because rm doesn’t send your file to a trash bin or waste basket. When you tell rm to delete a file, it’s gone FOREVER!

andrew@main-machine-ubuntu:~$ rm -i testfile
rm: remove regular empty file `testfile’?

The -r flag basically means recursive. It’s used when deleting entire non-empty directories. The rm command will not delete a non-empty directory by default, even with the -i flag. You’d otherwise have to go through and delete each file, then back out of that directory, and then you could delete it. Tedious, right? Not with rm -r.

andrew@main-machine-ubuntu:~$ ls dir2
testfile testfile2 testfile4
andrew@main-machine-ubuntu:~$ rm -r dir2/
andrew@main-machine-ubuntu:~$ ls dir2
ls: cannot access dir2: No such file or directory

One last command I’ll mention is the mkdir command. It’s used to create new directories. It’s very easy to use with no flags or anything.

andrew@main-machine-ubuntu:~$ mkdir newdirectory
andrew@main-machine-ubuntu:~$ ls
Desktop Firefox_wallpaper.png Public testfile8
Desktop Background.bmp IO redirection.odt Templates testfile9
Documents Music testfile Videos
examples.desktop newdirectory testfile2 Windows
filelist Pictures testfile7

The last topic for this post as mentioned earlier, are wildcards. These can be very useful for working with multiple files and directories. Let’s say, you want to move a bunch of files that all start with the word “test”. For this, we could do something like

andrew@main-machine-ubuntu:~$ mv test* /somewhere

What this would do is move every file beginning with “test” to the /somewhere directory. There are other wildcards and each do the same thing similarly. The * wildcard works for unlimited characters while putting a simple “?” would only count for one character. To make things even more interesting, there are [] and [!].

If you use the ls command to search for [abcdefg], than this wildcard will tell ls to show only those files that have the characters a, b, c, d, e, f, or g. Conversely, if you use the same scenario with with the [!abcdefg], this will tell ls to show only those files that do not have those characters. To make this even more powerful, you can also use the [] and [!] wildcards to specify not only alphabetic characters, but also alphanumerical, numerical, upper-case and lower-case characters too. Respectively, that would look like [[:alpha:]], [[:alnum:]], [[:digit:]], [[:upper:]], and [[:lower:]].

Advertisements

One Response to The Terminal: Messin’ With Files and Directories

  1. Chrissie says:

    andrew@main-machine-ubuntu:~$ mv Andrew* /Chrissie’s Heart

    Fixed.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: