Chapter 2: Working with files and directories

2.1 Reading Files

2.1.1 nano

nano filename.txt

most people use nano to read files in terminal. However there are other programs that offer greater utility when reading files .

2.1.2 cat

cat filename1.txt filename2.txt

cat is short for concatenation. You can use it to concatenate files but most people use it to read files. If you give it a list of files as arguments it will read and output file after file to the terminal window. Or, if you give it one file as an argument, it will read just that file.

2.1.3 another type of cat

A Bangkok Cat!

Figure 2.1 A Bangkok Cat!

2.1.4 more

more prints a file's contents to the screen with pagination.

more file1.txt

Hit the spacebar to read more. But wait! There's no way to go backward in the document!

2.1.5 less

less is the preferred reader for pagination. You can go backward and forward in the document. less takes the filename as an argument.

less -MN file1.txt

You can use options with less.

options
-M nicer prompt
-N shows the line number

You can use keyboard shortcuts to navigate a document opened with less.

Keyboard shortcuts
f forward
b backward
q quit
shift g go to end of document

2.2 Reading portions of a file

Displays lines from the beginning of the file. Good for a quick look inside.

2.2.2 tail

Displays lines from the end of the file. So in log files where the latest line is added to the end of the file you can use tail to monitor a log file as it updates.

tail -f file.txt

the -f option will cause tail to follow file changes and update the screen as changes happen.

2.3 Creating Directories

2.3.1 mkdir

You can pass in the folder name for the folder you want to create or the path and the new folder name together.

mkdir existingfolder/newfolder

If the path to a file doesn't exist you can create all the parent directories in the path to the new folder you want to create by using the -p option.

mkdir -p one/two/three/newfolder

use the -v option to turn on verbose output, to see some feedback on what was done.

2.4 Moving and renaming files and directories

A nice move!

Figure 2.2 A nice move!

2.4.1 mv

To move a file simply state the file name and the path to the new location

mv filename path

You can use ../ in the path to move up a level in the directory tree.

If a file of the same name exists in the destination folder mv will overwrite it!

So, it follows that you can use mv for renaming a file as well...

mv oldname newname

Move and rename at the same time

mv oldname newpath/newname

Options for mv:

option effect
-n no override
-f force overide
-i interactive gives a choice if there is a conflict
-v verbose

Be carefull! By default override is on. So moving a file without specifying a new name will override the existing file.

It may be best to use the interactive option, in which case the move program will ask if you want to overwrite an existing file.

mv -i

2.5 Copying files and directories

2.5.1 cp

cp oldfile.txt newfile.txt

Options for cp:

option effect
-n no override
-f force override
-i interactive
-v verbose

By default cp won't work on directories!

Use -R to recurrsively copy everything inside the directory

cp -R folderFullOfStuff

2.6 Deleting files and directories

2.6.1 rm

To delete files use rm

rm filename

rm doesn't work for directories so you can use...

2.6.2 rmdir

rmdir dirname

But that only removes empty directories...

To delete directories and all their contents recursively use rm and the -R option

rm -R dirname

2.7 Finder aliases in UNIX

OS X aliases and not the same as UNIX aliases.

OS X aliases are not useful in UNIX.

Unix uses something called links.

2.8.1 ln

ln target linkname

ln creates a link to file "target" and the name of that link is: "linkname".

If "linkname" is omitted, a link to "target" is created in the current directory, using the name: "target" as the "linkname".

A link is an entry in your file system which connects a filename to the actual bytes of data on the disk. More than one filename can "link" to the same data.

Here's an example. Let's create a file named file1.txt:

echo "This is a file." > file1.txt

This command echoes the string "This is a file".

Normally this would simply echo to our terminal, but the > operator redirects the string's text to a file, in this case file1.txt.

We can check that it worked by using cat to display the contents of the file:

cat file1.txt

When this file was created, the operating system wrote the bytes to a location on the disk and also linked that data to a filename, file1.txt so that we can refer to the data in commands and arguments using the filename.

If you rename the file, the contents of the file are not altered; only the information that points to it. The filename and the file's data are two separate entities.

What the link command does is allow us to manually create a link to file data that already exists. So, let's use link to create our own link to the file data we just created. In essence, we'll create another file name for the data that already exists.

Let's call our new link file2.txt. How do we create it?

The general form of the link command is: ln filename linkname. Our first argument is the name of the file whose data we're linking to; the second argument is the name of the new link we're creating.

ln file1.txt file2.txt

Now both file1.txt and file2.txt point to the same data on the disk:

cat file2.txt

The important thing to realize is that we did not make a copy of this data. Both filenames point to the same bytes of data on the disk.

If we change the contents of the data pointed to by either one of these filenames, the other filename will point to the same contents.

Let's append a line to one of the files using the >> operator:

echo "It points to data on the disk." >> file1.txt

Now let's look at the contents of file1.txt:

cat file1.txt

... and now let's look at the second file, the one we created with the ln command:

cat file2.txt

Both files show the change because they share the same data on the disk. Changes to the data of either one of these files will change the contents of the other.

But what if we delete one of the files? Will both files be deleted?

No. If we delete one of the files, we're simply deleting one of the links to the data.

Because we created another link manually, we still have a pointer to that data; we still have a way, at the user-level, to access the data we put in there. So if we use the rm command to remove our first file:

rm file1.txt

...it no longer exists as a file with that name:

cat file1.txt

...but the link to the data we manually created still exists, and still points to the data:

cat file2.txt

As you can see, the data stays on the disk even after the "file" (which is actually just a link to the data) is removed.

We can still access that data as long as there is a link to it. This is important to know when you're removing files — "removing" a file just makes the data inaccessible by unlinking it.

The data still exists on the storage media, somewhere, inaccessible to the system, and that space on disk is marked as being available for future use.

The type of link we've been working with here is sometimes called a "hard" link.

A hard link and the data it links to must always exist on the same filesystem; you can't, for instance, create a hard link on one partition to file data stored on another partition. You also can't create a hard link to a directory.

Symbolic links, sometimes called "soft" links, are different than "hard" links. Instead of linking to the data of a file, they link to another link.

This has several potential benefits. For one thing, symbolic links (also called "symlinks" for short) can link to directories. Also, symbolic links can cross file system boundaries, so a symbolic link to data on one drive or partition can exist on another drive or partition.

We can also use ln to create symbolic links with the -s option. So the command:

ln -s file1.txt file2.txt

Will create a symbolic link to file1.txt and that symbolic link will be named file2.txt.

In directory listings generated using the ls command, a symlink in unix is shown as:

linkname -> filename.txt

99% of the time you use symlinks to quickly go from a directory to a distant location.

Also symlinks work in the finder.

You should also be aware that, unlike hard links, removing the file (or directory) that a symlink points to will break the link.

You cannot move the alias around. You cannot move the original file around.

So if we create file1.txt:

echo "This is a file." > file1.txt

...and create a symbolic link to it:

ln -s file1.txt file2.txt

...we can cat either one of these to see the contents:

cat file1.txt

Returns: This is a file.

cat file2.txt

Returns: This is a file.

...but if we remove file1.txt:

rm file1.txt

...we can no longer access the data it contained with our symlink:

cat file2.txt

Leads to a "No such file or directory" message.

This error message might be confusing at first, because file2.txt still exists in your directory.

It's a broken symlink, however — a symbolic link which points to something that no longer exists.

The operating system tries to follow the symlink to the file that's supposed to be there (in this case: file1.txt), but finds nothing, and so it returns the error message.

While hard links are an essential component of how the operating system works, symbolic links are generally more of a convenience. You can use them to refer, in any way you'd like, to information already on the disk somewhere else.

To create a symbolic link to a directory, simply specify the directory name as the target. For instance, let's say we have a directory named documents, which contains one file, named file.txt.

Let's create a symbolic link to documents named dok. This command will do the trick:

ln -s documents/ dok

We now have a symlink named dok which we can refer to as if it is the directory documents.

For instance, if we use ls to list the contents of the directory, and then to list the contents of the symlinked directory, they will both show the same file:

ls documents

Returns: file.txt

ls dok

Returns: file.txt

When we work in the directory dok now, we will actually be working in documents, but we will see the word dok instead of documents in all pathnames.

Symbolic links are a useful way to make shortcuts to long, complicated pathnames. For instance, this command:

ln -s documents/work/budgets/Entertainment/2014/April aprbudget

This will save us a lot of typing. Now, instead of cd with the following command:

cd documents/work/budgets/Entertainment/2014/April aprbudget

...we can do this, instead:

cd aprbudget

Normally, you remove directories (once they're empty) with the rmdir command. But our symbolic link is not actually a directory: it's a file that points to a directory. So to remove our symlink, we just use the rm command:

rm aprbudge

This will remove the symlink, but the original directory and all its files are not affected.

2.10 Searching for files and directories

2.10.1 find

To implement a search from the command line follow the pattern:

find path expression

the path limits the search to a given location, for example:

find ~/Documents -name "somename.txt"

The expression argument here is: -name "somename.txt"

Use wildcards to introduce variability

find ~/Documents -name "some*.txt"

Use ? to indicate zero or more characters of any type

?

Use [] to use any one character from the list of characters in the brackets

[ ]

For example:

find ~/Sites -name index[123].html

Returns: index1.html and index2.html...

You can add modifiers to the search expression:

-and -not -path *QuickTime*

which means don't show anything whose path contains "QuickTime"

2.10.2 find: operators

Operators
-and
-not
-or