Author: Joe Barr
Why the focus on files? Because files are key to your overall understanding of Linux. Your keyboard? That’s a special file. Monitor? Same. Disk drive? Ditto. Get the picture? Everything is a file here.
What kind of file
is this?
The first file-related command we’ll talk about is file
. The basic format of the file
command is:
file filename
In response, file
tells you everything it can about what type of file “filename” is. This response, by the way, is not based on anything as simplistic as a file type or extension. If you name a graphics image “image.txt,” for example, file
won’t mistakenly tell you that it’s a text file. Or if you rename an executable trojan “image.jpg,” file
won’t tell you it’s a graphic image. This is a good thing. Lots of bad things happen in the land of DOS/Windows because people are fooled by a file extension.
Let’s look at an example. I have a lot digital images on my desktop system that I’ve taken with my digital camera. If I use file on one of them, like this:
file picture.jpg
It tells me this:
picture.jpg: JPEG image data, EXIF standard 0.73, 10752 x 2048
If I ask about a GIF image, it tells me:
picture.gif: GIF image data, version 89a, 220 x 246
Pretty neat. But what if I copy those files and swap their file extensions? To do that test we must first learn to use the cp
command. Obviously cp
is secret cryptic Linux-speak for “copy.” That’s because many of the original Clan of Unix Geeks were members of the Vowel Preservation Society. They had a strong aversion to the indiscriminate use of vowels in program names. “Copy” became “cp,” “move” became “mv,” “list” was cut to “ls,” and so on.
Houston, do you cp
?
The basic format of the cp
command is:
cp from-filename to-filename
Easy as pie. Let’s copy that GIF file so that it has a file extension indicating that it’s a JPEG image instead of GIF and then see what file
has to say about it. The command you enter to do that is:
cp picture.gif trick-picture.jpg
Then to query file
about trick-picture.jpg, you enter:
file trick-picture.jpg
The response is identical to that shown the first time: file
is not fooled by the phony extension.
By the way, this is probably a good time to mention that you can stack commands at the CLI simply by ending them with a semicolon. The same results as the two commands we’ve just entered gave us could also have been obtained like this:
cp picture.gif trick-picture.jpg; file trick-picture.jpg
I feel the earth mv
under my feet
Let’s say we decide to keep all of our JPGs in one directory and all of our GIFs in another. Our task now is to remove the clutter from the directory we’ve been working in by putting those files where they belong — all except for the trick-picture.jpg
, that is. We’ll just delete that.
The mv
command follows the same basic format as cp
:
mv from-filename to-filename
Our cleanup chore requires us to move the files to different directories, however, so we need to add the directory information to the “to-filename” to get it in the right place. For file operations in the local directory, of course, we don’t need to specify the full directory. The directory path is assumed to be the same unless otherwise specified. This is the otherwise case.
I’ve been working in my home directory, /home/warthawg. To get the JPEG put up in its proper place, I will need to move it to the /home/warthawg/jpg directory. One way to use the mv
command to do that looks like this:
mv picture.jpg /home/warthawg/jpg/picture.jpg
I could have also used:
mv picture.jpg jpg/picture.jpg
That one works because by not putting a “/” in front of the “jpg” directory name I have indicated that it resides in the current path. So the shell provides the “/home/warthawg/” portion of the path without me having to mention it. And speaking of assumptions, I could also have simply specified the to-path and not the file name at all, allowing mv
to puzzle out for itself that I intended the name to remain the same, like this:
mv picture.jpg jpg
Let’s rm
the evidence
After putting the JPEG in the jpg directory and the GIF in the gif directory, we have only one more thing left to do in this lesson — get rid of trick-picture.jpg. The rm
command is exceedingly easy to use. This is a good thing, but it can also be a very bad thing. Always use extreme caution when using rm
. You can erase entire directories with it, and that’s something you generally don’t want to do by mistake.
The basic format for the rm
command is:
rm filename
So to complete our in-class assignment all we need to do is:
rm trick-picture.jpg
It’s gone. End of assignment for today.
Another day we’ll take a look at two other important file related commands, find
and locate
. Of course, you don’t have to wait. You can always get a head-start by venturing out to the command line and typing man find
or man locate
.