Listing Files and Going Deep Into Filesystems

The ls Command

ls = List of objects in the current working directory.

Typing:

ls

will bring up a list of files and subdirectories. It may also be flagged to provide some different ways of looking at the list of files, such as

ls -l

which will provide a long directory listing (similar to a DOS dir), and

ls -a

which will show all of the files in the directory, including hidden ones and

ls | more

which will provide a directory listing one screen at a time. The ‘|’ is called a ‘pipe’, and passes data from one command to another.

ls -F

gives you a list of files with special notation of their type:

* means “executable”

/ means “directory”

= means “socket”

| means “pipe”

man ls

dir

Columns in the ls –la listing:

permissions

number of hard links

owner

group

size (in bytes)

last-modified date

file name

The permissions column:

directory, link or file?

r = read

w = write

x = execute

user triplet

group triplet

others triplet

The hard links column:

What the heck is a hard link? You can always:

man linkinfo coreutils link

man link

info coreutils link

But we’ll go in-depth below in More About File Systems

The Owner column:

The owner and the author are the same in most *nix, but not in all *nix. The owner is referenced in the owner triad of the permissions. Generally, files under your ownership are in your home directory, but not necessarily (see /var/mail/username).

The Group column:

When you are created as a user, a group with the same name is created, with you as its only member. You may be assigned to other groups, for instance, the wheel group. Your file permissions are set upon file creation. These defaults can be changed.

The Size column:

Size is displayed in bytes, though there are options for showing it in blocks (often 4096 byte blocks).

The Last-Modified column:

Month, Day, Year (if not current year)

Time in “military” format

The File Name column:

Regular files can be named almost anything within this keyspace:

a-z A-Z 0-9 _ – ~ .

You will run into certain special characters that you can’t use.

Unix does NOT use file extensions to identify file type; Gnome and KDE however DO.

Hidden files start with a dot: .

Backup files end with a tilde: ~

Some programs like emacs create backups automatically, creating near-duplicate file names.

Soft links (symlinks) are designated by an l. (That’s the letter l, not the number 1.) Both these and hard links are discussed below in More About File Systems

Hidden files:

Start with a dot

Contain system configuration

Usually text files

Can be directly modified

Now is a good time to become familiar with another useful web site: LinuxCommand.org:

http://linuxcommand.org/man_pages/ls1.html. Take a look at some other useful options. Do you notice if any of them are set by default?

Some Common Options to ls
-a
–all
List all files, even hidden la -a
-A
–almost-all
List all files, except the . and .. usually listed at the top ls -A
-C List in columns ls -C
–color=always Controls whether color is used to distinguish file types: ‘never’, ‘always’, or ‘auto’. ls –color=never
-d
–directory
List directory names, rather than their contents ls -d
-f List without sorting ls -f
-F
–classify
List filenames with markers to indicate type: executable* directory/ socket= or pipe| ls -F
–full-time Gets a long listing with full modification time ls –full-time
-l Long listing ls -l
-lh
-l –human-readable
Long listing with human-readable (simple) file sizes ls -lh
-lG
-l –no-group
-o
Long listing omitting group information ls -lG
-r
–reverse
Lists files in reverse ASCII-betic order ls -r
-R
–recursive
List files in directory, and in subdirectories ls -R
-s List files and size in kilobytes (KB) ls -s
-S List files sorted by size ls -S
-t List files sorted by modification time ls -t
-U Lists files without sorting ls -U
-x List files in rows rather than columns ls -x

Mix and match! Any of the above may be used together.

You can use an initial capital letter to name your directories. Because files are listed “ASCII-betically,” capital letters come before lower-case letters.
The command ll is an alias, on many systems, for ls -l.

Research the file command. What does it do? How does it compare to ls?

 

*You create one with a command like this:

ln /bin/bash ~/mybash

where the syntax is:

ln target_of_link link_file_name

So you choose a target file (like the bash binary above) and then a location for the hard link (in this case, my home directory, with the name “mybash”).

More About File Systems: Inodes, hard links and symlinks

At the very root of the file system is the superblock. The superblock is the physical location of the inode table. And the inode table in turn is a tabular list of inodes (information nodes), each of which is uniquely numbered, and holds the master information for a file: file size, data block location, last modification time and date; ownership; and permissions. The inode more-or-less “is” the file; when a file is deleted (unlinked), what is actually deleted is its inode. The file’s data blocks are only overwritten when they are needed. And we do need them; in most systems there is a limit of 64K inodes. Yes, we can run out of “file space” even when there is still space on the hard disk. (Want more explanation? Search for “inode” when you get to that page.)

Nothing Prevents A File From Being Listed In Two Different Directories
(or even twice in one directory)

With the above concept clearly in mind, now think of a file that appears in two different directories. What the directory listing is really referring to is an inode. There are not two copies of the file; there is only one. It just shows up in two different places. You can verify this by creating a link, then using the -i option to the ls command:

touch myfile ln ./myfile ./myfile2

ln -li

In this listing, note the inode number on the left. Both myfile and myfile2 have the same inode.

Can You Hard Link Directories?

Normal users can’t. Root can. Generally it’s a bad idea.

There Are Also Soft Links (Symlinks)

Symlinks are somewhat similar to Windows shortcuts.Most actions are passed through the link to the target, i.e. opening a program. Only creation and deletion actually act on the link file itself.

How Do You Create A Symlink?

With a command like this:

ln -s /bin/bash ~/mybash

where the syntax is:

ls -s target_of_link link_file_name

Note the -s option, which makes ln create a soft link (symlink) rather than a hard link.

Can You Soft Link Directories?

Absolutely. This is commonly done to simplify navigation:

ln -s /var/tmp /usr/tmp

Further Discussion

See the System V Unix page that discusses links.

 

 

 

 

 

Linux User Profiles and Paths

Examine root’s user characteristics

Enter the command:

ifconfig

as root, and you’ll see your IP address (among other things). Just to be sure, run:

whoami

And while you’re at it, check your (root’s) group memberships:

groups

Now leave root:

logout

Log back in as studenth

At the login prompt, log in as studenth:

login: studenth

password: student

 

Now compare studenth’s characteristics

Type the commands:

ifconfig

whoami

groups

What do you get?

 

A Standard User

You’re currently logged in as studenth.

Your default permissions and environment are MUCH more restrictive than root’s.

And your PATH is different.

 

Finding that command

Enter the command:

whereis ifconfig

What do you get?

 

Running ifconfig as studenth

ifconfig won’t run directly for studenth because the command (actually, the program run by the command) isn’t in the PATH of studenth.

ifconfig WILL run if you specify its location:

/sbin/ifconfig

 

Now try this with useradd

Still logged in as studenth, first find the useradd command (program):

whereis useradd

Now type the command using its full path.

Explain what you get.

Make sure you correctly called the command from its real location.

 

Why it doesn’t work

studenth was not CREATED as a root-group user.

studenth does NOT have the same rights as root.

 

Add to your PATH

Now we’re going to add to studenth’s PATH environment variable:

set PATH=${PATH}:/sbin

Check the result using echo:

echo $PATH

Run this command:

ifconfig

 

Linux User Default Files

When a new user is created, the default files and directories that are created are stored in /etc/skel.

This directory can be modified to fit your needs. Modifications will only affect new users; they do not change anything for existing users.

Group Information

Where group information is stored

Group information is stored in /etc/group. This file should be changed with the usermod, groupadd and groupmod commands. The format of each line is:

Group name

Group password (hardly ever used)

Group ID

User names (separated by commas)

Each field is separated by a colon.

Your Home Directory

When you start a shell session, it opens in your home directory.

Why home?

It keeps your stuff segregated from other peoples’.

It gives you a separate programming (scripting) space for your own work.

It lets you set your own preferences for everything you use: web browsers, text editors, terminals/shells, etc.

It gives you a place to share your web pages.

Changing Passwords

Options:

user’s name (Only required if you are root and want to change another user’s password)

 

To change the password for your currently logged-in user:

passwd
Enter existing password
Enter new password
Enter new password again (to validate)

 

To change the password for the user ‘roger’ (only if you are logged in as root):

passwd roger
Enter new password
Enter new password again (to validate)

Linux Shells

Glenn Norman

Boot, login, and start Secure Telnet/Secure Shell or PuTTY.

Log in with your UNM user name and password.

Check Your Shell

Command:

echo $SHELL

Your answer may be:

/bin/sh # the Bourne shell
/bin/csh # the C shell
/bin/bash # the Bourne Again shell
/bin/ksh # the Korn shell

Definitions

Commands: the instructions you give to the system

Files: globs of data that have been given filenames. These could contain graphics, music, text or programs.

Directories: in Windows we call these Folders. Think of something like a file cabinet drawer that can contain many files. Directories can contain other directories.

Directories are files. A directory file is a list of other files.

Environment: the collection of items that describe your unique computing session. These describe what programs you use, which printers, where you documents are stored by default, the email program you prefer to use, and a LOT more.

Process: a command or application running on the computer. You might want to call this a “program,” but actually an application may contain several programs, each of which may spawn many processes. Processes run in the “user space.”

Thread: an application running in “kernel space.”

Job: this is closer to what you’d think of as a “program.” It’s the sequence of instructions that’s given to the computer to perform a task. When the task is done, the job concludes.

Logging In To A Shell

When you sit down at a Linux workstation or make a connection to a Unix server, you start out at a login prompt. If you only see a flashing cursor, press the Enter key and you’ll be given the prompt:

login:

Your Username

To get any further, you have to have a user name (“username”). If you are registered with UNM, you have one; usually it’s the same as your email address. (This becomes an important issue we’ll explore later.)

Unix is case-sensitive, meaning that “A” is different than “a.” You have to enter your username correctly or this won’t work. Press the Enter key when you’re done:

Your Password

Now you’ll get a password prompt:

password:

Enter the password we supply to you.

Checking Your User Information

Now that you’ve finally logged in and taken care of any password change, you’re operating in your shell. One of the first things you can check is your own user information:

id

whoami

who

w

finger username

groups

These will show your userid number and name, your main group affiliation, and a list of all the groups of which you are a member, among other things.

There Are Lots Of Shells

Another term for a shell is “command interpreter.” Remember Dennis Richie, inventor of the C programming language? The first thing he needed, once he got his PDP7 “Unics” computer working, was a command interpreter to actually run commands. Thus was born the “C Shell,” csh, one of hundreds of program-name jokes in the Unix world.

Other Shells

Other shells include:

  • the Korn shell (ksh or the Posix shell),
  • the Bourne shell (sh),
  • the Perl shell (perl),
  • csh/tcsh (C shells),
  • The Bourne Again SHell (bash),
  • rsh (remote shell),
  • ssh (secure shell)
  • and more.

Some shells can connect to remote computers.

Some shells encrypt their traffic for security.

Some pass traffic as plain text, like Telnet.

The closely-related Bourne, bash and Korn shells offer lots of nice features like completion, which means if you type the beginning of a file name, for instance, bash will try to complete it for you.

The Bourne Shell

Other programmers needed different features in their shells, so they wrote their own command interpreters. Evolution occurred, survival of the fittest kicked in, and eventually the Bourne shell became the standard.

When this shell made the transition to the Linux world, if became the Bourne Again Shell, or bash. This is the default shell in most Linux distributions, but you can still choose to use a different shell.

On a Unix server, the default login shell will often be the C shell, /bin/csh. This is, unfortunately, a very “old school” choice, but one that’s easy to fix in the short term. Simply command:

bash # on a Linux box
 or
sh # on an AIX box
or
ksh # on an HP-UX box

For now, just be aware that we are studying bash under Linux and Bourne or Korn under Unix, and that this choice of shell is one of your environment variables.

This Is Your Command Line

When you are any user but root, Bourne/bash/Korn will give you this prompt:

$

with a flashing cursor where your text will be entered. If you use csh (or tcsh) your prompt will be:

>

Even more important is the visual cue the shell will give you to let you know you’re logged in as root:

#

Entering Commands

You’re ready to enter commands. You should know that:

  • Unix commands are case-sensitive.
  • Most are lower-case.
  • These commands are only going to work at a shell prompt.
  • You have to press the Enter key to make the command run.

Command Options

Commands often have options after the main command, and these options usually begin with a dash, for instance:

ls -l

You can add more than one option at a time:

ls –la

These “single-dash” options are formally called “System V” options. You will also see “no-dash” options (“BSD” options) with some commands, such as:

ps aux

Then there are the so-called POSIX options that are supposed to be common (at least to some degree of compliance) to any POSIX-compliant shell. They’re distinguished by double-dashes:

ls --reverse

Navigation Review

To move up one directory: cd ..

To move up two directories: cd ../..

To go back to your home, from anywhere: cd or cd ~

Note that the tilde ( ~ ) means “home,” and is relative for every user.

Of course, you can always specify your destination: cd /home/studenth/

Or go back to the immediately previous directory:

cd –

The man Command

You can always get information about a command by calling for a “manual page:”

man ls

will get you a detailed page of information on using the ls command.

The more Command

more = Show the contents of a text file one screen at a time.

Typing

more file.txt

will allow you to view, one page at a time, the contents of the file file.txt.

When viewing the text file, hitting <enter> advances the screen one line, while hitting <spacebar> advances the screen a page.

For help, type the letter h at the prompt while you’re in more.

When you’ve seen the whole file more will dump you to a command prompt.

The less Command

more may be nice for paging down through text files, but just try paging back up!

less, ironically enough, gives you “more.”

Use the same syntax as more:

less file.txt

Use the arrow keys or page up/page down to traverse the file in either direction.
When you want to leave less, type

q

(for “quit”).

The cat Command

There’s another way to view the contents of a text file (yes, really), and this one comes with a bonus.
The command:

cat file.txt

will display a file’s text and dump you to a command prompt.

One very useful option to cat is -n, for line numbering. Try this:

cat -n /etc/passwd

And, oddly, there is a tac command to read files in reverse order. This is especially handy for log files:

tac /var/log/messages

Con(cat)enate

But here’s the bonus: you can concatenate two or more text files with this command:

cat filename1 filename2 filename3

and you’ll see the contents of all three files, chained together (concatenated).
Remember this: this can be very handy later.

The Linux Directory Tree

Glenn Norman

Let’s be clear: the structure of directories in any OS is a directory tree. You, I and everyone will want to call it a “file system.” That’s okay, and the other kids usually won’t laugh at you. But formally, a file system is the formatting that’s applied to a partition, like FAT32 or NTFS in Windows, or EXT3 in Linux.

Linux (and Unix and Mac) use slashes to separate directories:

/

Windows does things backwards, and uses the backward slash, which we won’t call a back(ward) slash. The cool kids call it a “whack.”

\

Think like a developer. We want a root for the file system (it uses a tree metaphor, sort of), which will be / .

Now we need a folder for all our binaries, because that’s what programming is all about, right? Writing executable binaries? So it’s /bin . You can think of this as being similar to the C:\Program Files folder in Windows.

Consider further:

/

/bin

/sbin

/lib

/var

/tmp

/proc

/mnt or /media

/usr

/opt