Linux Essentials for Windows Admins – Navigation and File Manipulation

There’s no such thing as a homogeneous server environment. Windows administrators must learn basic Linux commands and navigation to support a contemporary network fully.

In Part 1 of this series, you learned some Unix/Linux (*nix) basics covering general security, filesystem layout, comparable DOS commands, and directory structure. In Part 2, I take you deeper into the *nix filesystem, filesystem navigation, and file manipulation.

Filesystem Differences

In the past few years in the *nix space, filesystems have gone through somewhat of a standardization or convergence. That’s a good thing for cross-platform system administrators who must work on a variety of systems without becoming an expert on every one. By convergence, I mean that there is more of a standard look and feel for *nix filesystems from one “flavor” to another.

If you’re familiar with the layout of a Linux system, you can easily navigate and find critical files on just about any other modern *nix system. A number of directories are common to all *nix types (e.g., /, /etc, /var, /tmp, /usr, /bin, and a few others); however, one particular directory is not consistent from one flavor to another, and that is where the kernel files reside.

For example, in Linux, you’ll find those files under /boot, but in HP-UX, they’re under /stand. In other *nix types, the kernel files reside in /kernel, /vmunix, or under multiple subdirectories of /usr.

The takeaway of this is that you need to recognize that there are going to be subtle differences in the layout of *nix systems. The important configuration files are always under /etc. For some applications, you might also have to look in /var or in /opt. As you become more familiar with Linux or another Unix flavor, you’ll learn that each system has its own quirks and idiosyncrasies.

Filesystem Navigation

Figure 1 reviews what the Linux filesystem looks like from the / (root) directory.

Figure 1: The Linux root filesystem and its many subdirectories.

From this overview, you can do some spelunking on your own. First, you’ll need to know some basic commands and the functions they perform. As at the Windows CMD prompt, you traverse directories and subdirectories with the cd command, with a few subtle differences that you’ll have to get used to along the way.

For example, typing cd by itself returns you to your home directory. In Windows, the system returns your current working directory. The equivalent command to do that in Linux is pwd (print working directory). No matter where you are on a *nix filesystem, you can use the cd command to return you to your home directory.

Another subtle difference when wandering around a *nix filesystem is that you must explicitly specify the directory to which you want to cd from the root (/) directory. For example, if you want to cd to the /etc directory, you have to type the command:

$ cd /etc

If you were to leave out the slash, you’d receive the error

-bash: cd: etc: No such file or directory

if your current directory does not have a subdirectory named etc. Your *nix shell assumes that if you do not specify a leading slash before a directory name, it is a subdirectory of your current directory. To illustrate more explicitly, the command

$ cd etc

would put me in the etc directory under my current directory, which assuming it is my home directory, would put me in

/home/khess/etc

which is now my new current working directory. Of course, on your system, your home directory will have your username, not mine.

If you’re looking for a file, you don’t have to cd to it first. You can display a directory’s contents from anywhere by explicitly telling the shell where you want to look:

$ ls /usr/share/ssh
blacklist.DSA-1024  blacklist.RSA-2048

If you decide to cd into a subdirectory, you can navigate to any other directory or subdirectory without first going to your home directory:

$ cd /usr/share/ssh
$ cd /etc/network

Now that you know how to cd from one directory to another, it’s time to look at the command I just used above to look at the contents of a directory: the ls command. The ls command is equivalent to the DIR command on your Windows systems. It displays the contents and other file attributes, if specified. The following is a partial directory listing of my Windows 7 home directory with DIR:

07/10/2011  04:28 PM              Citrix
07/11/2012  03:17 PM              Contacts
07/28/2012  02:38 PM              Desktop
07/27/2010  08:15 AM              Diskeeper
07/11/2012  03:17 PM              Documents
07/29/2012  03:51 PM              Downloads
07/11/2012  03:17 PM              Favorites
04/16/2012  12:00 PM            60,304 g2mdlhlpx.exe
07/11/2012  03:17 PM              Links
07/11/2012  03:17 PM              Music
07/29/2012  03:56 PM        18,874,368 NTUSER.DAT
07/11/2012  03:17 PM              Pictures
01/11/2012  06:44 PM              Pictures - Ken’s iPhone
07/11/2012  03:17 PM              Saved Games
07/11/2012  03:17 PM              Searches
08/04/2010  01:03 PM              SecurityScans

As you can see, the default DIR listing displays the modification date, the modification time, the file type, the file size, and the file or the directory name. Compare that with the minimal information divulged by the ls command:

Desktop    goo.txt   Music       Pictures  Templates
Documents  list.txt  newbie.txt  Public    Videos

The basic ls command displays very little information about the directory’s contents except for file names. On most Linux systems, the ls command shows you a color-coded file listing, as shown in Figure 2.

Figure 2: PuTTY Window displaying a file listing in my home directory.

Most *nix systems don’t have the advantage of a color-coded file list. Although Linux displays directories in blue, text files in white, compressed files in red, and graphics files in green, if your system doesn’t color code, you have to rely on ls command “switches.” Switches are options that you pass to a command to elicit some nondefault behavior. Windows commands have switches, too, but they’re used less often than on *nix systems.

$ ls -F
Desktop/    goo.txt   Music/      Pictures/  Templates/
Documents/  list.txt  newbie.txt  Public/    Videos/

The -F switch means that you want ls to display each file’s “classification.” Directories are shown with a trailing slash. Text files have no designation. Links have a trailing @ symbol.

To nearly mimic the default DIR command of Windows, try the ls command with the -al switch combination.

$ ls -al

drwxr-xr-x  2 khess khess 4096 2012-07-03 15:59 Desktop
drwxr-xr-x  2 khess khess 4096 2012-07-03 15:58 Documents
-rw-r--r--  1 root  root     0 2012-07-07 15:04 goo.txt
-rw-r--r--  1 khess khess   49 2012-07-07 17:07 list.txt
drwxr-xr-x  2 khess khess 4096 2012-07-03 15:58 Music
-rw-r--r--  1 khess khess    0 2012-07-07 14:55 newbie.txt
drwxr-xr-x  2 khess khess 4096 2012-07-29 16:13 Pictures
drwxr-xr-x  2 khess khess 4096 2012-07-03 15:58 Public
drwxr-xr-x  2 khess khess 4096 2012-07-03 15:58 Templates
drwxr-xr-x  2 khess khess 4096 2012-07-03 15:58 Videos

This partial listing is only a few of the files in my home directory. When you enter the same command on your system, you might see files that begin with a dot (.), which indicates they are hidden files. If you want to hide a file in *nix, name it with a leading dot. However, the file is only hidden from standard ls commands.

In fact, you’ll probably see at least these three files at the top of your directory listing:

-rw-------  1 khess khess 3944 2012-07-28 19:06 .bash_history
-rw-r--r--  1 khess khess  220 2012-05-25 22:11 .bash_logout
-rw-r--r--  1 khess khess 2940 2012-05-25 22:11 .bashrc

To show files in date-sorted order, try the following commands on Windows and Linux:

C:\> DIR /OD

and

$ ls -ltr

Both systems should list files in order of modification date, with newer files shown at the bottom of the listings.

Note: On many Linux systems, dir is an “alias” for ls. Try it on your system to see if it works. I’ll discuss aliases in detail in a later article.

File Manipulation

By file manipulation, I’m referring to creating, copying, removing (deleting), moving, renaming, and editing files. A directory is also a file in *nix. In fact, everything in *nix is a file. For the purposes of this lesson, file refers to plain text or a directory.

The quickest way to create an empty file is to use the touch command,

$ touch file1.txt

which creates the empty file file1.txt in your home directory. If you can’t create this file, use the cd command to return to your home directory and try again.

$ ls -al file1.txt
-rw-r--r-- 1 khess khess 0 2012-07-29 16:56 file1.txt

An easy way to enter content into your new file is to echo it there. The echo command works similarly in Windows by taking input from your keyboard and placing it into a file via output redirection,

$ echo “This is one way to put text into a file.” > file1.txt

which puts the sentence, “This is one way to put text into a file.” into file1.txt. In fact, you overwrote file1.txt with a new file with the same name because you used the > operator. If you want to append the sentence to the file, use >> as the redirection operator instead.

To view the contents of the file you created, use the cat command. Remember, from Table 1 in Part 1, cat is equivalent to the TYPE command.

$ cat file1.txt
This is one way to put text into a file.

The echo command created or overwrote file1.txt. Now append data to the file with the >> redirection operator:

$ echo “This is the way to append a file and put text into it.” >> file1.txt

When you look at the file with the cat command, you'll see your appended line:

$ cat file1.txt
This is one way to create a file and put text into it.
This is the way to append a file and put text into it.

I’m going to warn you now that, when you start scripting, you will forget to append to the file, and you’ll spend hours debugging very simple scripts because you forgot to use the >>. Everyone does it, so be prepared.

Copying files works the same in Windows and in *nix. Simply stated,

$ cp file1.txt file2.txt

This copy (cp) command creates an exact duplicate of file1.txt as file2.txt.

Copying a file is an excellent backup method. You should create a backup copy of any configuration file before you edit as the root user so you can revert back to the original should you make a mistake or alter important formatting.

To backup a file properly before editing, you how to become the root user for limited access. In Part 1, I briefly mentioned the sudo (pronounced soodoo or soodoe) command, which allows you to run commands as root one at a time from your regular user account. Think RunAs.

$ sudo cp -p file1.txt file1_bak.txt
[sudo] password for khess: **********

The file file1.txt is copied, preserving permissions (-p) to file_bak.txt.

Instead of asterisks, when you type in your password, you’ll get no feedback. The sudo you’re prompted for is your password. The system verifies that you are who you say you are; otherwise, someone else could walk up to your screen and type a command as root.

Your power as root with sudo only lasts five minutes, so you’ll have to reauthenticate after that. This ensures that no one else can simply walk up and grant themselves root access to a system into which you’re logged.

Some systems are very restrictive and only grant sudo access for a limited number of specific commands. Other systems are less restrictive and allow you to run anything as root with this method. Access by sudo is configurable by the root user and is managed by a special file and a special editor for that file. I’ll cover this in more detail in a later article.

To rename a file in *nix, you “move” it to a new name. It sounds odd at first, but once you get used to the concept of moving to a new name, it won’t bother you anymore:

$ mv file1.txt filex.txt
$ ls 
Desktop    filex.txt  list.txt  newbie.txt  Public     Videos
Documents  goo.txt    Music     Pictures    Templates

The mv command also physically moves files from one location to another. For example, if you want to move filex.txt to the /tmp directory, issue the first command:

$ mv filex.txt /tmp

$ ls /tmp
filex.txt  orbit-gdm  pulse-PKdhtXMmr18n

You can also rename the file when you physically move it:

$ mv /tmp/filex.txt file0.txt

$ ls
Desktop    file0.txt  list.txt  newbie.txt  Public     Videos
Documents  goo.txt    Music     Pictures    Templates

The system assumes you want to move the file to your current directory if you don’t specify a location.

Removing a file on *nix systems is easy – too easy. Unlike Windows, the system does not ask you if you’re sure. The *nix systems assume (often incorrectly) that you know what you’re doing and that you really want to remove a file. Removal is irreversible. No Recycle Bin preserves deleted files.

$ rm file0.txt

The file is gone.

Creating a directory on *nix systems is exactly like creating them on Windows systems at the CMD prompt. Although Windows systems use an alias for MKDIR (MD), the syntax is the same.

$ mkdir newdir

The copy (cp) command works for empty directories or for directories that you want to copy; however, for those that contain files, you’ll have to perform something close to an XCOPY /S on them. This command copies the directory and all of its contents recursively and preserves all permissions on all files and subdirectories.

$ cp -Rp newdir noodir

If you attempt to copy a directory that contains files to a new directory name without specifying the -R (recursive) switch, you’ll receive this error and the directory will not be created:

cp: omitting directory `newdir’

An ls will show you that the directory has been created. You can place files in it, remove it, move it, or rename it, just as you would any other file – with some exceptions.

You cannot remove a directory if it has files in it.

$ touch newdir/newfile.txt

This touch command places the empty file, newfile.txt, into the newdir directory. Now, try to remove the directory with the standard equivalent to the Windows RMDIR (RM) command, rmdir:

$ rmdir newdir
rmdir: failed to remove `newdir/’: Directory not empty

You have two options: Remove or move all the files inside the directory first, or use a special remove command on the stubborn directory:

$ rm -rf newdir

This command removes the specified file (directory) recursively with the force (-f) switch. The directory newdir and every file contained in it is gone.

Caution: Using the rm -rf command is dangerous, so use it thoughtfully. If you issue the command in the wrong place as the root user, you could remove every file under the directory you specify – even the / (root) directory – and doing so will render your system useless.

The topics covered in this article are relatively simple but they require practice to master adequately. Practice them on your system until you feel comfortable with each one. Remember to back up any system file before you edit it, and preserve its permissions. This one lesson will become increasingly important to you as you become more competent as a *nix system administrator. Start with good practices now, even as an individual user.

Next time, I’ll cover advanced file editing and some advanced file manipulation.

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.